Source for "Facebook Video"

By sizzlemctwizzle
Has 14 other scripts.


// ==UserScript==
// @name           Facebook Video
// @namespace      sizzlemctwizzle
// @description    Adds links to download or convert videos with Zamzar.com, and provides code to embed videos in other sites.
// @version        2.0.3
// @include        http://*.facebook.com/*
// ==/UserScript==
scr_name='Facebook Video';
scr_version='2.0.3';
scr_id='9789';

// Copyleft Michael Medley <medleymind@gmail.com>, All Wrongs Reserved
function get_values(swfid) {
// Get the FLV and Embed Code
values = new Array();
src = unsafeWindow[swfid].getVariable('video_src');
values[0] = decodeURIComponent(src);
values[1] = "http://www.zamzar.com/url/?u=" + src;
values[2] = unsafeWindow[swfid].getSWFHTML();
return values;
}

function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}

// Add elements to a video page
function add_elements(swfid, values) {
// Download Flv Link
link = document.createElement('a');
link.href = values[0];
txt = document.createTextNode("Download Video");
link.appendChild(txt);
li = document.createElement('li');
li.appendChild(link);

// Convert Link
link = document.createElement('a');
link.href = values[1];
txt = document.createTextNode("Convert Video");
link.appendChild(txt);
li2 = document.createElement('li');
li2.appendChild(link);

// Embed Code
h4 = document.createElement('h4');
text = document.createTextNode("Embed Code");
h4.appendChild(text);
code = document.createElement('input');
code.type = "text";
code.value = values[2];
code.size = 26;
code.setAttribute('id', "code_"+swfid);
code.className = "code_block";
code.addEventListener('click',function () {this.focus();this.select();},false)

// Public Links
pubdiv = document.createElement('div');
pubdiv.id = "public_link_photo";
span = document.createElement('span');
link = document.createTextNode(window.location.href);
text = document.createTextNode("Show people this video by sending them this public link: ");
span.appendChild(link);
pubdiv.appendChild(text);
pubdiv.appendChild(span);

// Wizard link
link = document.createElement('a');
link.href = "#";
txt = document.createTextNode("Customize Code");
link.appendChild(txt);
li3 = document.createElement('li');
li3.appendChild(link);
link.addEventListener('click',function (e) {run_wizard(swfid);e.preventDefault();},false)

// Append Elements
div = document.getElementById("actions_"+swfid);
footer = document.getElementById("content");
footer.appendChild(pubdiv);
var ul = document.evaluate('//ul[@class="actionspro"]', document, null, 
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue
ul.appendChild(li);
ul.appendChild(li2);
ul.appendChild(li3);
div.parentNode.appendChild(h4);
div.parentNode.appendChild(code);
}

// Run the script if video posted somewhere else on site
function posted_videos(swfid, values) {
convert = "http://www.zamzar.com/url/?u=" + src;
para = document.createElement('p');
para.innerHTML = '<a href="' + values[0] + '">Download Video</a> | <a href="' + values[1] + '">Convert Video</a>';
h4 = document.createElement('h4');
text = document.createTextNode("Embed Code");
code = document.createElement('input');
h4.appendChild(text);
code.type = "text";
code.value = values[2];
code.size = 28;
code.className = "code_block";
code.setAttribute('id', "code_"+swfid);
code.addEventListener('click',function () {this.focus();this.select();},false);
// Wizard link
link = document.createElement('a');
link.href = "#";
txt = document.createTextNode("Customize Code");
link.appendChild(txt);
link.addEventListener('click',function (e) {run_wizard(swfid);e.preventDefault();},false)
div = document.getElementById("actions_"+swfid);
div.appendChild(para);
div.appendChild(h4);
div.appendChild(code);
div.appendChild(link);
}

// Respect Aspect Ratio
function fix_height(before, now, height, swfid) {
percent = now/before;
document.getElementById("width_"+swfid).addEventListener('blur',function () {
now = document.getElementById("width_"+swfid).value;
before = unsafeWindow[swfid].getVariable('stage_width');
height = unsafeWindow[swfid].getVariable('stage_height');
document.getElementById("height_"+swfid).value = fix_height(before, now, height, swfid);
},false)
return Math.round(percent*height);
}

function fix_width(before, now, width, swfid) {
percent = now/before;
document.getElementById("height_"+swfid).addEventListener('blur',function () {
now = document.getElementById("height_"+swfid).value;
before = unsafeWindow[swfid].getVariable('stage_height');
width = unsafeWindow[swfid].getVariable('stage_width');
document.getElementById("width_"+swfid).value = fix_width(before, now, width, swfid);
},false)
return Math.round(percent*width);
}

function run_wizard(swfid) {
// Embed Wizard
var dialog = new unsafeWindow.pop_dialog();
var html = '<h2><span>Customize Embed Code</span></h2><div id="embed_options" style="padding:20px;"><p>'+
'<table><tr><td><label>Video URL</label></td>'+
'<td><input type="text" id="file_'+swfid+'" class="code_block" size="40" value="'+decodeURIComponent(unsafeWindow[swfid].getVariable('video_src'))+'" /></td></tr>'+
'<tr><td><label>Thumb URL</label></td><td><input type="text" id="thumb_'+swfid+'" class="code_block" size="40" value="'+decodeURIComponent(unsafeWindow[swfid].getVariable('thumb_url'))+'" /></td></tr>'+
'<tr><td><label>Width</lable></td><td><input type="text" id="width_'+swfid+'" class="code_block" size="15" value="'+unsafeWindow[swfid].getVariable('stage_width')+'" /></td></tr>'+
'<tr><td><label>Height</lable></td><td><input type="text" id="height_'+swfid+'" class="code_block" size="15" value="'+unsafeWindow[swfid].getVariable('stage_height')+'" /></td></tr>'+
'<tr><td><label>Auto-Play</label></td><td><input type="checkbox" id="autoplay_'+swfid+'" /></td></tr></table>'+
'</p></div>\n'+
'<div class="dialog_buttons"><input class="inputsubmit" value="Update" id="update_'+swfid+'" type="button" /></div>';
dialog.show_dialog(html);
if ( (unsafeWindow[swfid].getVariable('video_autoplay')) && ((unsafeWindow[swfid].getVariable('video_autoplay')) == 1) ) {
document.getElementById("autoplay_"+swfid).checked = true;
} else {
document.getElementById("autoplay_"+swfid).checked = false;
}
document.getElementById("width_"+swfid).addEventListener('blur',function () {
now = document.getElementById("width_"+swfid).value;
before = unsafeWindow[swfid].getVariable('stage_width');
height = unsafeWindow[swfid].getVariable('stage_height');
document.getElementById("height_"+swfid).value = fix_height(before, now, height, swfid);
},false)
document.getElementById("height_"+swfid).addEventListener('blur',function () {
now = document.getElementById("height_"+swfid).value;
before = unsafeWindow[swfid].getVariable('stage_height');
width = unsafeWindow[swfid].getVariable('stage_width');
document.getElementById("width_"+swfid).value = fix_width(before, now, width, swfid);
},false)
document.getElementById("update_"+swfid).addEventListener('click',function () {
dialog.fade_out(500, 1100);
update_video(swfid);
},false)
}

function update_video(swfid) {
file = encodeURIComponent(document.getElementById("file_"+swfid).value);
width = document.getElementById("width_"+swfid).value;
height = document.getElementById("height_"+swfid).value;
thumb = encodeURIComponent(document.getElementById("thumb_"+swfid).value);
if (document.getElementById("autoplay_"+swfid).checked == true) {
autoplay = 1;
} else {
autoplay = 0;
}
hscale = height/unsafeWindow[swfid].getVariable('stage_height');
vidheight = Math.round(unsafeWindow[swfid].getVariable('video_height')*hscale);
if ((vidheight/height) <= .7) {
vidheight = Math.round(unsafeWindow[swfid].getVariable('stage_height')*.7);
wscale = vidheight/unsafeWindow[swfid].getVariable('video_height');
vidwidth = Math.round(unsafeWindow[swfid].getVariable('video_width')*wscale);
} else {
wscale = width/unsafeWindow[swfid].getVariable('stage_width');
vidwidth = Math.round(unsafeWindow[swfid].getVariable('video_width')*wscale);
}
length = unsafeWindow[swfid].getVariable('video_length');
seconds = unsafeWindow[swfid].getVariable('video_seconds');
type = unsafeWindow[swfid].getVariable('video_player_type');
unsafeWindow[swfid] = new unsafeWindow.SWFObject("http://static.ak.facebook.com/swf/mvp.swf?58:83575", swfid, width, height, "9.0.47", "#000000");
unsafeWindow[swfid].addParam("allowScriptAccess", "always");
unsafeWindow[swfid].addParam("scale", "showall");
unsafeWindow[swfid].addParam("allowFullScreen", "true");
unsafeWindow[swfid].addParam("wmode", "window");
unsafeWindow[swfid].addVariable("video_src", file);
unsafeWindow[swfid].addVariable("stage_width", width);
unsafeWindow[swfid].addVariable("stage_height", height);
unsafeWindow[swfid].addVariable("video_length", length);
unsafeWindow[swfid].addVariable("video_seconds", seconds);
unsafeWindow[swfid].addVariable("video_player_type", type);
unsafeWindow[swfid].addVariable("video_width", vidwidth);
unsafeWindow[swfid].addVariable("video_height", vidheight);
unsafeWindow[swfid].addVariable("thumb_url", thumb);
unsafeWindow[swfid].addVariable("slate_src", "http%3A%2F%2Fstatic.ak.facebook.com%2Fswf%2Fmvp_slate.swf%3F58%3A81294");
unsafeWindow[swfid].addVariable("tail_slate_src", "http%3A%2F%2Fstatic.ak.facebook.com%2Fswf%2Fmvp_tail_slate.swf%3F58%3A74597");
unsafeWindow[swfid].addVariable("video_autoplay", autoplay);
unsafeWindow[swfid].write(document.getElementById(swfid+"_holder"));
document.getElementById("code_"+swfid).value = unsafeWindow[swfid].getSWFHTML();
}

// Style I stole from Facebook
style = '\n.code_block {\n'+
'font-size:12px;\n' +
'background:#f7f7f7;\n' +
'margin-right:7px;\n' +
'border:1px solid #ccc;\n' +
'color:#555;\n' +
'}\n\n' +
'#public_link_photo {\n' +
'clear: both;\n' +
'color: #333;\n' +
'font-size: 9px;\n' +
'padding: 5px 5px;\n' +
'text-align: center;\n' +
'margin: 10px 0px 0px 0px;\n' +
'background: #f7f7f7;\n' +
'border-top: 1px solid #D8DFEA;\n' +
'}\n\n' +
'#public_link_photo span {\n' +
'color: black;\n' +
'display: block;\n' +
'font-size: 11px;\n' +
'}\n\n';

var dev = document.evaluate(
	'//div[@class="fb_video_holder"] | //div[@class="mvp_holder"]', 
	document, 
	null, 
	XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

if (dev.snapshotItem(0)) { addGlobalStyle(style);  }

for (var i = 0; i < dev.snapshotLength; i ++) {
// Is it a holder or an embed object?
if (dev.snapshotItem(i).className == "fb_video_holder") {
swfid = dev.snapshotItem(i).getElementsByTagName('embed')[0].getAttribute('id');
} else {
swfid = dev.snapshotItem(i).getElementsByTagName('embed')[0].getAttribute('id');
dev.snapshotItem(i).setAttribute('id', swfid+"_holder");
}
// If video is not on a video page put links under the video
if (document.location.href.match('/video/') == null) {
dev.snapshotItem(i).parentNode.parentNode.setAttribute('id', "actions_"+swfid);
posted_videos(swfid, get_values(swfid));
} else if (document.getElementById("video_actions")) {
document.getElementById("video_actions").setAttribute('id', "actions_"+swfid);
add_elements(swfid, get_values(swfid));
} else {
}
}

// Auto update script
// I know everyone and their mother has wrote one, but I thought I'd toss my hat in
if (self.location == top.location) {
if (GM_getValue('updated', 0) == 0) {
GM_setValue('updated', Math.round(new Date().getTime() / 1000));
} else if (GM_getValue('updated', 0) != 'off') {
GM_registerMenuCommand("Auto Update "+scr_name, function(){GM_setValue('updated', Math.round(new Date().getTime() / 1000));});
}
if ( (Math.round(new Date().getTime() / 1000) > (GM_getValue('updated', 0) + 6)) && (GM_getValue('updated', 0) != 'off') ) {
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://userscripts.org/scripts/review/'+scr_id+'?format=txt',
    headers: {
        'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(xpr) {
	version=/scr_version='(.*)';/i.exec(xpr.responseText);
	scr_name=/scr_name='(.*)';/i.exec(xpr.responseText);
	if ( (version[1] != scr_version) && (confirm('A new version of the '+scr_name[1]+' userscript is available. Do you want to update?')) ) {
	GM_setValue('updated', Math.round(new Date().getTime() / 1000));
	document.location.href = 'http://userscripts.org/scripts/source/'+scr_id+'.user.js';
	} else if (version[1] != scr_version) {
		if(confirm('Do you want to turn of auto updating for this script?')) {
		GM_setValue('updated', 'off');
		GM_registerMenuCommand("Auto Update "+scr_name[1], function(){GM_setValue('updated', Math.round(new Date().getTime() / 1000));});
		} else {
		GM_setValue('updated', Math.round(new Date().getTime() / 1000));
		}
	} else {
		GM_setValue('updated', Math.round(new Date().getTime() / 1000));
	}
	}
});
}
}