JavaScript YouTube Download Tool
Sometimes I want to download a YouTube video, and yes there are plenty of tools on the web to help you doing that, but I really want something that doesn't require installation each time I want to download a YouTube video.
So, I’ve decided to write some pure (don’t want to download JQuery or any other library) JavaScript to help me do that, this script will work locally and you only need to open the html file on your machine.

Download YouTube Download Html (Right Click and Save)
I’ve add a Text input called “txtUrl” that gets a YouTube video Uri for example: http://www.youtube.com/watch?v=t4H_Zoh7G5A
Once you click the “Download Tube” button the GetTubeSource will be invoked and using a basic Ajax call to receive YouTube Uri html response.
function GetTubeSource() {
var uri = document.getElementById("txtUrl").value;
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
ParseResponse(xmlhttp.responseText);
}
}
xmlhttp.open("GET", uri, true);
xmlhttp.send();
}
After we get's the response we pass it to “ParseResponse” method that parse the response and extract the “url_encoded_fmt_stream_map” value and some other values needed in order to download the YouTube video.
I’ve also added the “window.clipboardData.setData” to copy the Download Uri to your Clip Board, you can remove this line if you don’t want too.
function ParseResponse(res) {
try {
response = res;
var mp = "id=\"movie_player\"";
var s1 = response.indexOf(mp);
var s2;
if (navigator.appVersion.match("MSIE") != null) {
var paramflash = "name=\"flashvars\" value=\"";
s2 = response.indexOf(paramflash, s1) + paramflash.length;
}
else {
var flashvar = "flashvars=";
s2 = response.indexOf(flashvar, s1) + flashvar.length;
}
var end = response.indexOf("\"", s2);
var str = response.substring(s2, end);
w = str.split("&");
for (i = 0; i <= w.length - 1; i++)
if (w[i].split("=")[0] == "url_encoded_fmt_stream_map") {
links = unescape(w[i].split("=")[1]);
break;
}
abc = links.split(",url="); for (i = 0; i <= abc.length - 1; i++){
fmt = abc[i].split("|")[0];
if ((fmt.indexOf("flv") > 0) && (fmt.indexOf("large") <= 0)
&& (fmt.indexOf("medium") <= 0)) {
if (fmt.indexOf("rl=") > 0) {
url = fmt.substring(4, fmt.indexOf("fallback_host") - 1);
url = unescape(unescape(url));
break;
}
else {
url = fmt.substring(0, fmt.indexOf("fallback_host") - 1);
url = unescape(unescape(url)); break;
}
}
}
combineurl = url + '&title=' + GetTubeTitle();
window.clipboardData.setData('Text', combineurl);
window.location.href = combineurl;
}
catch (ex) {
alert(ex.Message);
}
finally {
EnableForm();
}
}
There are several simple methods to get data from the response like the video title
function GetTubeTitle() {
try {
var sT = "<meta name=\"title\" content=\"";
var s = response.indexOf(sT) + sT.length;
var e = response.indexOf("\"", s);
var title = response.substring(s, e);
if (title.match(".$"))
title = title.substring(0, title.length - 1);
return title;
}
catch (ex) {
return "";
}
}
Download YouTube Download Html (Right Click and Save)