// ==UserScript==
// @name         liveleak.com video download link
// @namespace     http://www.digivill.net/~joykillr
// @description   Adds a download link to liveleak.com so videos can be downloaded.
// @include       http://liveleak.com/view*
// @include       http://*.liveleak.com/view*
// ==/UserScript==
//
//v 3.0



function getURL(tokenURL) {
	xmlhttp = PRO_xmlhttpRequest();
	xmlhttp.open("GET", tokenURL, true);
	xmlhttp.setRequestHeader("User-Agent", "Mozilla/5.0 Firefox/2.0.0.3");
	xmlhttp.setRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
	xmlhttp.send(null);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			var URLstring = new String(xmlhttp.responseText);
			URLstring = URLstring.split("file_location=")[1];
			var splitidx = URLstring.indexOf("placeholder_");
			splitidx = splitidx - 1;
			URLstring = URLstring.substring(0, splitidx);
			URLstring = unescape(URLstring);
			if (URLstring) addBox(URLstring);
		}
	}
}
	
function addBox(strR) {
	var URLDLbox = document.createElement("div");
	URLDLbox.setAttribute("style", "display: block !important;");
	URLDLbox.innerHTML = '<table style="width:auto; margin-left: auto; margin-right:auto;"><tbody style="background-color:inherit!important;">' +
		'<tr style="background-color:inherit!important;">' +
		'<td style="text-align:center;background-color:inherit!important;">' +
		'<a href="' + strR + '" style="font-size:108%; line-height:108%; color: #ffffff; background-color: #000000; border: 2px solid red; margin-left: auto; margin-right:auto; text-align:center; font-weight:bold;">Click Here To Download Video</a>' + 
		'<br /></td></tr></tbody></table>';
	var nElem = document.getElementsByTagName("div");
	for (var nn = 0; nn < nElem.length; nn++) {
		if ((nElem[nn].title.search("player.swf"))||(nElem[nn].getAttribute("dataattribute").search("player.swf"))) {
			if (document.getElementById("navMain")) {document.getElementById("navMain").appendChild(URLDLbox);}
			else {nElem[nn].appendChild(URLDLbox);}
			}
		}
	}
	
if (document.body.getElementsByTagName("param")[0]) {
	var tID = document.body.getElementsByTagName("param")[0];
	tID = tID.getAttribute("value");
	tID = tID.split("token=")[1].split("&")[0];
	var token = "http://www.liveleak.com/mi?token=" + tID;
	getURL(token);
}



