Emulateur Freebox avec GreaseMonkey

Un article de Wiki FreePlayer.org.

Voici un petit script GreaseMonkey qui vous permettra de simuler le fonctionnement des xdiv sur votre navigateur. Pour l'instant ce script ne gére que les events :

  • onfocus
  • onblur
  • focused

et les actions :

  • hide
  • show
  • defer_location

Si vous avez des remarques ou des évolutions à me soumettre c'est avec grand plaisir.

// ==UserScript==
// @name          Free
// @namespace     http://www.mytf1vod.tf1.fr
// @description	  emulate freebox xdiv
// @author        Fabien Lalanne
// @include       http://free-stb.tf1vision.com/*
// ==/UserScript==

var $;

// Add jQuery
(function(){
	if (typeof unsafeWindow.jQuery == 'undefined') {
		var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,
			GM_JQ = document.createElement('script');

		GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
		GM_JQ.type = 'text/javascript';
		GM_JQ.async = true;

		GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
	}
	GM_wait();
})();

// Check if jQuery's loaded
function GM_wait() {
	if (typeof unsafeWindow.jQuery == 'undefined') {
		window.setTimeout(GM_wait, 100);
	} else {
		$ = unsafeWindow.jQuery.noConflict(true);
		letsJQuery();
	}
}

// All your GM code must be inside this function
function letsJQuery() {
	
	$('xdiv').each(function (i) {
		if ($(this).attr('id') != 'cadre') {
			if ($(this).attr('visible') == "") {
				$(this).css('display' , '');
			} else {
				$(this).css('display' , 'none');
			}
			//--> gestion de la profondeur pas encore au point
			// if ($(this).attr('depth') == "" || $(this).attr('depth') == undefined) {
				// $(this).css('z-index' , 0);
			// } else {
				// $(this).css('z-index' , Math.abs($(this).attr('depth')));
			// }
			$(this).css('position' , 'absolute')
					.css('left', $(this).attr('x'))
					.css('top', $(this).attr('y'))
					.removeAttr('x')
					.removeAttr('y')
					.removeAttr('depth');
		}
	});	
	var body = document.getElementsByTagName('body')[0];
	var html = body.innerHTML;
			
	var reg2 = /xdiv/g;
	html = html.replace(reg2,'div');
	
	body.innerHTML = html;
	body.style.margin = 0;
	document.getElementById('pageMain').setAttribute('align', '');
	$('a').each(function (i) {
		if ($(this).attr('onfocus') != "" && $(this).attr('onfocus') != undefined) {
			addAOnfocus($(this));
		}
		if ($(this).attr('onblur') != "" && $(this).attr('onblur') != undefined) {
			addAOnblur($(this));
		}
		if ($(this).attr('focused') == "") {
			$(this).trigger('mouseover');
		}		
	});
	
	$('table').each(function(){
		if ($(this).attr('background') != "" && $(this).attr('background') != undefined) {
			var bgi = $(this).attr('background');
			bgi = bgi.replace('/devcpfweb026', 'http://devs.tf1.fr');
			$(this).css('background-image', 'url('+bgi+')');
			$(this).removeAttr('background');
		}
	});
}

function addAOnfocus(elt) {
	elt.mouseover(function() {
		var onfocus = $(this).attr('onfocus').split(';');
		$.each(onfocus, function(i, v) {
			if (v != "") {
				var actions = v.split('="');
				var id = actions[0];
				var action = actions[1];
				if (action.indexOf('hide') >= 0) {
					$('#'+id).hide();
				}
				if (action.indexOf('show') >= 0) {
					$('#'+id).show();
				}
				if (id.indexOf('defer_location') >= 0) {
					var url = action.replace('"', '');
					url = url.replace(';', '');
					if (url.indexOf('http') < 0) {
						url = 'http://'+document.domain+url;
					}
					document.location.href = url;
				}
			}
		});
	});	
}

function addAOnblur(elt) {
	elt.mouseout(function() {
		var onblur = $(this).attr('onblur').split(';');
		$.each(onblur, function(i, v) {
			if (v != "") {
				var actions = v.split('=');
				var id = actions[0];
				var action = actions[1];
				if (action.indexOf('hide') >= 0) {
					$('#'+id).hide();
				}
				if (action.indexOf('show') >= 0) {
					$('#'+id).show();
				}
			}
		});
	});	
}