// ==UserScript==
// @name RaiTV video
// @description Turn RaiTV silverlight objects into video tags
// @namespace http://oblomov.myopenid.com
// @include http://rai.tv/*
// @include http://rai.it/*
// @include http://www.rai.tv/*
// @include http://www.rai.it/*
// @include http://archivioluce.com/*
// @include http://www.archivioluce.com/*
// @author Giuseppe "Oblomov" Bilotta
// @version 20130607.2301
// ==/UserScript==
/* Since the Chrome sandbox is _extremely_ restrictive and we can't
* access things such as window.videoURL from the actual document,
* we have to resort to script injection to be completely cross-platform.
*/
(function() {
function addScript(source) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + source + ')();';
document.body.appendChild(script);
}
addScript(
function() {
function getURL(name) {
var list = document.getElementsByName(name) ;
if (list && list[0]) {
return list[0].content;
} else {
var altname = name.toUpperCase().replace('VIDEO','video');
console.log(name, altname);
console.log(window[name], window[altname]);
return window[altname];
}
}
var urls = {
std: getURL('videourl'),
mp4: getURL('videourl_mp4'),
wmv: getURL('videourl_wmv'),
h264: getURL('videourl_h264'),
m3u8: getURL('videourl_m3u8'),
}
function srctag(url, av, fmt) {
if (!url)
return '' ;
return ''
}
function H5video(fmt) {
return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
}
// create a link element for one of the page-wide media streams
function H5a(fmt) {
if (urls[fmt]) {
var a = document.createElement('a');
a.href = urls[fmt];
a.innerHTML = fmt.toUpperCase();
a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
return a;
}
return null
}
// create a link element for a raw URL
function H5aRaw(name, url) {
if (url) {
var a = document.createElement('a');
a.href = url;
a.innerHTML = name.toUpperCase();
a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
return a;
}
return null
}
var streamlist = document.createElement("li");
// reset stream list: this is used in the playTg and playAudio
// functions to reset the stream list when changing stream,
function slReset() {
streamlist.innerHTML = 'Stream/Download:';
}
// and of course once at the beginning of it all 8-)
slReset();
var sch = document.getElementById("silverlightControlHost");
if (sch) {
// debug
console.log(urls);
// find place to append the stream list to
// if possible, look for an existing ul in what is likely to be the top
// otherwise, create an ul and put it either in a miniLink if available,
// or the top otherwise
// TODO use some smarter mechanism
var pp = sch.parentNode.parentNode.previousElementSibling;
var specs = pp.getElementsByTagName('ul');
if (!specs || !specs[0]) {
specs = document.createElement('ul');
specs.className = 'Specifiche';
var mid = document.getElementById('Notizie');
if (mid) {
mid = mid.lastElementChild;
mid.insertBefore(specs, mid.firstElementChild);
} else {
pp.appendChild(specs);
}
} else {
specs = specs[0];
}
specs.appendChild(streamlist);
console.log(streamlist, 'appended');
// if we are on a page that defines the videourl* metas, go straight to creating
// the video element
if (urls.std) {
// prevent other JS with messing with this element further
sch.id = 'html5mediaHost';
sch.innerHTML = "";
console.log(sch, 'hacked');
for (var fmt in urls) {
var a = H5a(fmt);
if (a)
streamlist.appendChild(a);
}
return; // done
}
// hack the play* JS functions
if (window.playTg) {
window.playTg = function (liveTg, video, h264, androidUrl) {
var dataP = new Date();
var ggP, mmP, aaaaP;
ggP = dataP.getDate() + "-";
mmP = dataP.getMonth() + 1 + "-";
aaaaP = 1900 + dataP.getYear();
// prevent page from refreshing, the site only checks for
// the existence of object/embed, not audio/video
window.refreshByJS = false;
// stop loading any current audio/video
if (sch.firstChild && sch.firstChild.src) {
console.log('stopping current A/V');
sch.firstChild.src = '';
}
// re-create the video player
sch.innerHTML = "";
// re-create stream list
slReset();
// oh we would like to indicate the media type somehow,
// but it seems to be somewhat random. Not even a HEAD
// on the URL works reliably all the time
var a = H5aRaw('1', h264);
if (a)
streamlist.appendChild(a);
if (androidUrl != h264) {
a = H5aRaw('2', androidUrl);
if (a)
streamlist.appendChild(a);
}
if ((video != androidUrl) && (video != h264)) {
a = H5aRaw('3', video);
if (a)
streamlist.appendChild(a);
}
setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
console.log('playTg', liveTg, video, h264, androidUrl)
}
console.log('playTg hacked');
}
if (window.playAudio) {
window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
var dataP = new Date();
var ggP, mmP, aaaaP;
ggP = dataP.getDate() + "-";
mmP = dataP.getMonth() + 1 + "-";
aaaaP = 1900 + dataP.getYear();
// prevent page from refreshing, the site only checks for
// the existence of object/embed, not audio/video
window.refreshByJS = false;
// stop loading any current audio/video
if (sch.firstChild && sch.firstChild.src) {
console.log('stopping current A/V');
sch.firstChild.src = '';
}
// re-create the audio player
sch.innerHTML = "";
// re-create stream list
slReset();
var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
if (a)
streamlist.appendChild(a);
setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
console.log('playAudio', grrEdizione, mediaUrl, mediatype);
}
console.log('playAudio hacked');
}
}
// Archivio Luce
if (window.ply && window.ply.configuration
&& window.ply.configuration.file
&& window.cnt) {
var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
var width = window.ply.configuration.width;
var height = window.ply.configuration.height;
window.cnt.innerHTML = "";
console.log(window.cnt, 'hacked');
window.cnt.appendChild(H5aRaw("WMV", src));
}
})
})();