3 // @description Turn RaiTV silverlight objects into video tags
4 // @namespace http://oblomov.myopenid.com
6 // @include http://rai.tv/*
7 // @include http://rai.it/*
8 // @include http://www.rai.tv/*
9 // @include http://www.rai.it/*
10 // @include http://archivioluce.com/*
11 // @include http://www.archivioluce.com/*
12 // @author Giuseppe "Oblomov" Bilotta
13 // @version 20131004.1627
16 /* Since the Chrome sandbox is _extremely_ restrictive and we can't
17 * access things such as window.videoURL from the actual document,
18 * we have to resort to script injection to be completely cross-platform.
23 function addScript(source) {
24 var script = document.createElement('script');
25 script.setAttribute("type", "application/javascript");
26 script.textContent = 'window.addEventListener("load", ' + source + ', false);';
27 document.body.appendChild(script);
33 function getURL(name) {
34 var list = document.getElementsByName(name) ;
35 if (list && list[0]) {
36 return list[0].content;
38 var altname = name.toUpperCase().replace('VIDEO',
39 'video').replace('ANDROID', 'android');
40 console.log(name, altname);
41 console.log(window[name], window[altname]);
42 return window[altname];
47 std: getURL('videourl'),
48 mp4: getURL('videourl_mp4'),
49 wmv: getURL('videourl_wmv'),
50 h264: getURL('videourl_h264'),
51 m3u8: getURL('videourl_m3u8'),
52 m3u8_android: getURL('videourl_m3u8_android'),
55 function srctag(url, av, fmt) {
60 f = fmt.replace('_android','');
62 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
65 function H5video(fmt) {
66 return srctag(urls[fmt], 'video', fmt);
69 // create a link element for one of the page-wide media streams
72 var a = document.createElement('a');
74 a.innerHTML = fmt.toUpperCase();
75 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
81 // create a link element for a raw URL
82 function H5aRaw(name, url) {
84 var a = document.createElement('a');
86 a.innerHTML = name.toUpperCase();
87 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
93 var streamlist = document.createElement("li");
95 // reset stream list: this is used in the playTg and playAudio
96 // functions to reset the stream list when changing stream,
98 streamlist.innerHTML = '<span>Stream/Download:</span>';
100 // and of course once at the beginning of it all 8-)
105 var placeStreamList = function() {
106 // find place to append the stream list to
107 // if possible, look for an existing ul in what is likely to be the top
108 // otherwise, create an ul and put it either in a miniLink if available,
109 // or the top otherwise
110 // TODO use some smarter mechanism
111 var pp = sch.parentNode.parentNode.previousElementSibling;
112 var specs = pp.getElementsByTagName('ul');
113 if (!specs || !specs[0]) {
114 specs = document.createElement('ul');
115 specs.className = 'Specifiche';
116 var mid = document.getElementById('Notizie');
118 mid = mid.lastElementChild;
119 mid.insertBefore(specs, mid.firstElementChild);
121 pp.appendChild(specs);
126 specs.appendChild(streamlist);
127 console.log(streamlist, 'appended');
131 var ourReplayTV = function(det) {
133 sch = document.getElementById("SilverlightPlayer");
134 sch.style.width = '100%';
135 sch.style.height = '100%';
138 // stop loading any current audio/video
139 if (sch.firstChild && sch.firstChild.src) {
140 console.log('stopping current A/V');
141 sch.firstChild.src = '';
148 if (k.match(/^h264/)) {
150 if (url && url.length > 0) {
151 srcs += srctag(url, 'video', 'h264');
152 var r = k.split('_');
154 r = r[0] + " (" + r[1] + "KB/s)";
158 streamlist.appendChild(H5aRaw(r, url));
162 // re-create the video player
163 sch.innerHTML = "<video width='100%' height='100%' controls autoplay>" +
165 "<h3>Sorry, no supported video format found :-(</h3></video>";
169 if (window.replayTv) {
170 window.replayTv.playVideo_ujs = window.replayTv.playVideo;
171 window.replayTv.playVideo = function(det) {
174 window.replayTv.playVideo_ujs.call(window.replayTv, det);
181 if (window.replayTv.currVideo) {
182 window.replayTv.playVideo(window.replayTv.vid2data[window.replayTv.currVideo]);
186 sch = document.getElementById("silverlightControlHost");
192 // if we are on a page that defines the videourl* metas, go straight to creating
195 // prevent other JS with messing with this element further
196 sch.id = 'html5mediaHost';
197 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
198 H5video('mp4') + H5video('wmv') + H5video('h264') +
199 H5video('m3u8') + H5video('m3u8_android') +
201 "<h3>Sorry, no supported video format found :-(</h3></video>";
202 console.log(sch, 'hacked');
204 for (var fmt in urls) {
207 streamlist.appendChild(a);
212 // hack the play* JS functions
214 window.playTg = function (liveTg, video, h264, androidUrl) {
215 var dataP = new Date();
217 ggP = dataP.getDate() + "-";
218 mmP = dataP.getMonth() + 1 + "-";
219 aaaaP = 1900 + dataP.getYear();
221 // prevent page from refreshing, the site only checks for
222 // the existence of object/embed, not audio/video
223 window.refreshByJS = false;
225 // stop loading any current audio/video
226 if (sch.firstChild && sch.firstChild.src) {
227 console.log('stopping current A/V');
228 sch.firstChild.src = '';
231 // re-create the video player
232 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
233 srctag(h264, 'video') +
234 srctag(androidUrl, 'video') +
235 srctag(video, 'video') +
236 "<h3>Sorry, no supported video format found :-(</h3></video>";
238 // re-create stream list
240 // oh we would like to indicate the media type somehow,
241 // but it seems to be somewhat random. Not even a HEAD
242 // on the URL works reliably all the time
243 var a = H5aRaw('1', h264);
245 streamlist.appendChild(a);
246 if (androidUrl != h264) {
247 a = H5aRaw('2', androidUrl);
249 streamlist.appendChild(a);
251 if ((video != androidUrl) && (video != h264)) {
252 a = H5aRaw('3', video);
254 streamlist.appendChild(a);
257 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
258 console.log('playTg', liveTg, video, h264, androidUrl)
260 console.log('playTg hacked');
263 if (window.playAudio) {
264 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
265 var dataP = new Date();
267 ggP = dataP.getDate() + "-";
268 mmP = dataP.getMonth() + 1 + "-";
269 aaaaP = 1900 + dataP.getYear();
271 // prevent page from refreshing, the site only checks for
272 // the existence of object/embed, not audio/video
273 window.refreshByJS = false;
275 // stop loading any current audio/video
276 if (sch.firstChild && sch.firstChild.src) {
277 console.log('stopping current A/V');
278 sch.firstChild.src = '';
281 // re-create the audio player
282 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
283 srctag(mediaUrl, 'audio') +
284 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
286 // re-create stream list
288 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
290 streamlist.appendChild(a);
292 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
293 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
295 console.log('playAudio hacked');
300 if (window.ply && window.ply.configuration
301 && window.ply.configuration.file
303 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
304 var width = window.ply.configuration.width;
305 var height = window.ply.configuration.height;
307 window.cnt.innerHTML = "<video style='width:" + width +
308 "px; height:" + height + "' controls autostart>" +
309 srctag(src, 'video', null) +
311 console.log(window.cnt, 'hacked');
312 window.cnt.appendChild(H5aRaw("WMV", src));