3 // @description Turn RaiTV silverlight objects into video tags
4 // @namespace http://oblomov.myopenid.com
5 // @include http://rai.tv/*
6 // @include http://rai.it/*
7 // @include http://www.rai.tv/*
8 // @include http://www.rai.it/*
9 // @include http://archivioluce.com/*
10 // @include http://www.archivioluce.com/*
11 // @author Giuseppe "Oblomov" Bilotta
12 // @version 20131004.1627
15 /* Since the Chrome sandbox is _extremely_ restrictive and we can't
16 * access things such as window.videoURL from the actual document,
17 * we have to resort to script injection to be completely cross-platform.
22 function addScript(source) {
23 var script = document.createElement('script');
24 script.setAttribute("type", "application/javascript");
25 script.textContent = '(' + source + ')();';
26 document.body.appendChild(script);
32 function getURL(name) {
33 var list = document.getElementsByName(name) ;
34 if (list && list[0]) {
35 return list[0].content;
37 var altname = name.toUpperCase().replace('VIDEO',
38 'video').replace('ANDROID', 'android');
39 console.log(name, altname);
40 console.log(window[name], window[altname]);
41 return window[altname];
46 std: getURL('videourl'),
47 mp4: getURL('videourl_mp4'),
48 wmv: getURL('videourl_wmv'),
49 h264: getURL('videourl_h264'),
50 m3u8: getURL('videourl_m3u8'),
51 m3u8_android: getURL('videourl_m3u8_android'),
54 function srctag(url, av, fmt) {
59 f = fmt.replace('_android','');
61 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
64 function H5video(fmt) {
65 return srctag(urls[fmt], 'video', fmt);
68 // create a link element for one of the page-wide media streams
71 var a = document.createElement('a');
73 a.innerHTML = fmt.toUpperCase();
74 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
80 // create a link element for a raw URL
81 function H5aRaw(name, url) {
83 var a = document.createElement('a');
85 a.innerHTML = name.toUpperCase();
86 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
92 var streamlist = document.createElement("li");
94 // reset stream list: this is used in the playTg and playAudio
95 // functions to reset the stream list when changing stream,
97 streamlist.innerHTML = '<span>Stream/Download:</span>';
99 // and of course once at the beginning of it all 8-)
104 var placeStreamList = function() {
105 // find place to append the stream list to
106 // if possible, look for an existing ul in what is likely to be the top
107 // otherwise, create an ul and put it either in a miniLink if available,
108 // or the top otherwise
109 // TODO use some smarter mechanism
110 var pp = sch.parentNode.parentNode.previousElementSibling;
111 var specs = pp.getElementsByTagName('ul');
112 if (!specs || !specs[0]) {
113 specs = document.createElement('ul');
114 specs.className = 'Specifiche';
115 var mid = document.getElementById('Notizie');
117 mid = mid.lastElementChild;
118 mid.insertBefore(specs, mid.firstElementChild);
120 pp.appendChild(specs);
125 specs.appendChild(streamlist);
126 console.log(streamlist, 'appended');
130 var ourReplayTV = function(det) {
132 sch = document.getElementById("SilverlightPlayer");
133 sch.style.width = '100%';
134 sch.style.height = '100%';
137 // stop loading any current audio/video
138 if (sch.firstChild && sch.firstChild.src) {
139 console.log('stopping current A/V');
140 sch.firstChild.src = '';
147 if (k.match(/^h264/)) {
149 if (url && url.length > 0) {
150 srcs += srctag(url, 'video', 'h264');
151 var r = k.split('_');
153 r = r[0] + " (" + r[1] + "KB/s)";
157 streamlist.appendChild(H5aRaw(r, url));
161 // re-create the video player
162 sch.innerHTML = "<video width='100%' height='100%' controls autoplay>" +
164 "<h3>Sorry, no supported video format found :-(</h3></video>";
168 if (window.replayTv) {
169 window.replayTv.playVideo_ujs = window.replayTv.playVideo;
170 window.replayTv.playVideo = function(det) {
173 window.replayTv.playVideo_ujs.call(window.replayTv, det);
180 if (window.replayTv.currVideo) {
181 window.replayTv.playVideo(window.replayTv.vid2data[window.replayTv.currVideo]);
185 sch = document.getElementById("silverlightControlHost");
191 // if we are on a page that defines the videourl* metas, go straight to creating
194 // prevent other JS with messing with this element further
195 sch.id = 'html5mediaHost';
196 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
197 H5video('mp4') + H5video('wmv') + H5video('h264') +
198 H5video('m3u8') + H5video('m3u8_android') +
200 "<h3>Sorry, no supported video format found :-(</h3></video>";
201 console.log(sch, 'hacked');
203 for (var fmt in urls) {
206 streamlist.appendChild(a);
211 // hack the play* JS functions
213 window.playTg = function (liveTg, video, h264, androidUrl) {
214 var dataP = new Date();
216 ggP = dataP.getDate() + "-";
217 mmP = dataP.getMonth() + 1 + "-";
218 aaaaP = 1900 + dataP.getYear();
220 // prevent page from refreshing, the site only checks for
221 // the existence of object/embed, not audio/video
222 window.refreshByJS = false;
224 // stop loading any current audio/video
225 if (sch.firstChild && sch.firstChild.src) {
226 console.log('stopping current A/V');
227 sch.firstChild.src = '';
230 // re-create the video player
231 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
232 srctag(h264, 'video') +
233 srctag(androidUrl, 'video') +
234 srctag(video, 'video') +
235 "<h3>Sorry, no supported video format found :-(</h3></video>";
237 // re-create stream list
239 // oh we would like to indicate the media type somehow,
240 // but it seems to be somewhat random. Not even a HEAD
241 // on the URL works reliably all the time
242 var a = H5aRaw('1', h264);
244 streamlist.appendChild(a);
245 if (androidUrl != h264) {
246 a = H5aRaw('2', androidUrl);
248 streamlist.appendChild(a);
250 if ((video != androidUrl) && (video != h264)) {
251 a = H5aRaw('3', video);
253 streamlist.appendChild(a);
256 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
257 console.log('playTg', liveTg, video, h264, androidUrl)
259 console.log('playTg hacked');
262 if (window.playAudio) {
263 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
264 var dataP = new Date();
266 ggP = dataP.getDate() + "-";
267 mmP = dataP.getMonth() + 1 + "-";
268 aaaaP = 1900 + dataP.getYear();
270 // prevent page from refreshing, the site only checks for
271 // the existence of object/embed, not audio/video
272 window.refreshByJS = false;
274 // stop loading any current audio/video
275 if (sch.firstChild && sch.firstChild.src) {
276 console.log('stopping current A/V');
277 sch.firstChild.src = '';
280 // re-create the audio player
281 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
282 srctag(mediaUrl, 'audio') +
283 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
285 // re-create stream list
287 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
289 streamlist.appendChild(a);
291 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
292 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
294 console.log('playAudio hacked');
299 if (window.ply && window.ply.configuration
300 && window.ply.configuration.file
302 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
303 var width = window.ply.configuration.width;
304 var height = window.ply.configuration.height;
306 window.cnt.innerHTML = "<video style='width:" + width +
307 "px; height:" + height + "' controls autostart>" +
308 srctag(src, 'video', null) +
310 console.log(window.cnt, 'hacked');
311 window.cnt.appendChild(H5aRaw("WMV", src));