// ==UserScript==
// @name           Video UnEmbeder
// @namespace      http://www.arantius.com/misc/greasemonkey/
// @description    Go to the real video's page, when it's embedded in someone elses' page.
// @include        *
// @exclude        http://video.google.com/*
// @exclude        http://www.youtube.com/*
// @exclude        http://youtube.com/*
// @exclude        http://www.metacafe.com/*
// @exclude        http://video.yahoo.com/*
// @exclude        http://www.bloglines.com/*
// ==/UserScript==

function uniquePush(array, value) {
	for (var i=0, j=null; j=array[i]; i++) {
		if (value==j) return;
	}
	array.push(value);
}

//find the video(s)
var vids=document.evaluate(
	"//param[@name='movie'][contains(@value, 'video.google.com') or contains(@value, 'youtube.com')]/@value | "+
	"//embed[contains(@src, 'video.google.com') or contains(@src, 'youtube.com') or contains(@src, 'metacafe.com')]/@src | "+
	"//embed[contains(@src, 'yimg.com')]/@flashvars",
	document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
);
var docs=[];
for (var i=0, vid=null; vid=vids.snapshotItem(i); i++) {
	var doc='';
	if (vid.value.match(/video.google.com/)) {
		var m=decodeURIComponent(vid.value).match(/docid=(.*?)(&|$)/i);
		if (m[1]) doc='http://video.google.com/videoplay?docid='+m[1];
	} else if (vid.value.match(/youtube.com/)) {
		var m=vid.value.match(/[^\/]+$/);
		if (m) doc='http://www.youtube.com/watch?v='+m;
	} else if (vid.value.match(/video.yahoo.com/)) {
		var m=unescape(vid.value).match(/vid=([0-9a-f.]+)&/);
		if (m) doc='http://video.yahoo.com/video/play?vid='+m[1];
	} else if (vid.value.match(/metacafe.com/)) {
		//http://www.metacafe.com/fplayer/272865/girl_trampoline.swf
		//metacafe.com/watch/262661/balloon_in_a_microwave/
		var m=unescape(vid.value).match(/fplayer\/(.+)\.swf/);
		if (m) doc='http://www.metacafe.com/watch/'+m[1]+'/';
	}
	if (doc) uniquePush(docs, doc);
}

if (1==docs.length) {
	document.location.assign(docs[0]);
	return;
}
if (0!=docs.length) {
	if (confirm(
		'Video UnEmbeder found more than one embeded video.\n'+
		'Click OK to see a list of them, or Cancel to stay at the current page.'
	)) {
		document.write('<ul>');
		for (i in docs) {
			document.write('<li><a href="'+docs[i]+'">'+docs[i]+'</a></li>');
		}
		document.write('</ul>');
		document.close();
	}
	return;
}
