GreaseMonkey tuning
[raitv.user.js] / raitv.user.js
1 // ==UserScript==
2 // @name        RaiTV video
3 // @description Turn RaiTV silverlight objects into video tags
4 // @namespace   http://oblomov.myopenid.com
5 // @grant       none
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
14 // ==/UserScript==
15
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.
19  */
20
21 (function() {
22
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);
28 }
29
30
31 addScript(
32 function() {
33         function getURL(name) {
34                 var list = document.getElementsByName(name) ;
35                 if (list && list[0]) {
36                         return list[0].content;
37                 } else {
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];
43                 }
44         }
45
46         var urls = {
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'),
53         }
54
55         function srctag(url, av, fmt) {
56                 if (!url)
57                         return '' ;
58                 var f = null;
59                 if (fmt != 'std') {
60                         f = fmt.replace('_android','');
61                 }
62                 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
63         }
64
65         function H5video(fmt) {
66                 return srctag(urls[fmt], 'video', fmt);
67         }
68
69         // create a link element for one of the page-wide media streams
70         function H5a(fmt) {
71                 if (urls[fmt]) {
72                         var a = document.createElement('a');
73                         a.href = urls[fmt];
74                         a.innerHTML = fmt.toUpperCase();
75                         a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
76                         return a;
77                 }
78                 return null
79         }
80
81         // create a link element for a raw URL
82         function H5aRaw(name, url) {
83                 if (url) {
84                         var a = document.createElement('a');
85                         a.href = url;
86                         a.innerHTML = name.toUpperCase();
87                         a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
88                         return a;
89                 }
90                 return null
91         }
92
93         var streamlist = document.createElement("li");
94
95         // reset stream list: this is used in the playTg and playAudio
96         // functions to reset the stream list when changing stream,
97         function slReset() {
98                 streamlist.innerHTML = '<span>Stream/Download:</span>';
99         }
100         // and of course once at the beginning of it all 8-)
101         slReset();
102
103         var sch;
104
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');
117                         if (mid) {
118                                 mid = mid.lastElementChild;
119                                 mid.insertBefore(specs, mid.firstElementChild);
120                         } else {
121                                 pp.appendChild(specs);
122                         }
123                 } else {
124                         specs = specs[0];
125                 }
126                 specs.appendChild(streamlist);
127                 console.log(streamlist, 'appended');
128
129         }
130
131         var ourReplayTV = function(det) {
132                 if (!sch) {
133                         sch = document.getElementById("SilverlightPlayer");
134                         sch.style.width = '100%';
135                         sch.style.height = '100%';
136                         placeStreamList();
137                 }
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 = '';
142                 }
143
144                 slReset();
145
146                 var srcs = "";
147                 for (var k in det) {
148                         if (k.match(/^h264/)) {
149                                 var url = det[k];
150                                 if (url && url.length > 0) {
151                                         srcs += srctag(url, 'video', 'h264');
152                                         var r = k.split('_');
153                                         if (r[1]) {
154                                                 r = r[0] + " (" + r[1] + "KB/s)";
155                                         } else {
156                                                 r = r[0];
157                                         }
158                                         streamlist.appendChild(H5aRaw(r, url));
159                                 }
160                         }
161                 }
162                 // re-create the video player
163                 sch.innerHTML = "<video width='100%' height='100%' controls autoplay>" +
164                         srcs +
165                         "<h3>Sorry, no supported video format found :-(</h3></video>";
166
167         }
168
169         if (window.replayTv) {
170                 window.replayTv.playVideo_ujs = window.replayTv.playVideo;
171                 window.replayTv.playVideo = function(det) {
172                         console.log("IN");
173                         try {
174                                 window.replayTv.playVideo_ujs.call(window.replayTv, det);
175                         } catch (e) {
176                                 console.log(e);
177                         }
178                         ourReplayTV(det);
179                         console.log("OUT");
180                 }
181                 if (window.replayTv.currVideo) {
182                         window.replayTv.playVideo(window.replayTv.vid2data[window.replayTv.currVideo]);
183                 }
184         }
185
186         sch = document.getElementById("silverlightControlHost");
187         if (sch) {
188                 // debug
189                 console.log(urls);
190
191                 placeStreamList();
192                 // if we are on a page that defines the videourl* metas, go straight to creating
193                 // the video element
194                 if (urls.std) {
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') +
200                                 H5video('std') +
201                                 "<h3>Sorry, no supported video format found :-(</h3></video>";
202                         console.log(sch, 'hacked');
203
204                         for (var fmt in urls) {
205                                 var a = H5a(fmt);
206                                 if (a)
207                                         streamlist.appendChild(a);
208                         }
209                         return; // done
210                 }
211
212                 // hack the play* JS functions
213                 if (window.playTg) {
214                         window.playTg = function (liveTg, video, h264, androidUrl) {
215                                 var dataP = new Date();
216                                 var ggP, mmP, aaaaP;
217                                 ggP = dataP.getDate() + "-";
218                                 mmP = dataP.getMonth() + 1 + "-";
219                                 aaaaP = 1900 + dataP.getYear();
220
221                                 // prevent page from refreshing, the site only checks for
222                                 // the existence of object/embed, not audio/video
223                                 window.refreshByJS = false;
224
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 = '';
229                                 }
230
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>";
237
238                                 // re-create stream list
239                                 slReset();
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);
244                                 if (a)
245                                         streamlist.appendChild(a);
246                                 if (androidUrl != h264) {
247                                         a = H5aRaw('2', androidUrl);
248                                         if (a)
249                                                 streamlist.appendChild(a);
250                                 }
251                                 if ((video != androidUrl) && (video != h264)) {
252                                         a = H5aRaw('3', video);
253                                         if (a)
254                                                 streamlist.appendChild(a);
255                                 }
256
257                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
258                                 console.log('playTg', liveTg, video, h264, androidUrl)
259                         }
260                         console.log('playTg hacked');
261                 }
262
263                 if (window.playAudio) {
264                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
265                                 var dataP = new Date();
266                                 var ggP, mmP, aaaaP;
267                                 ggP = dataP.getDate() + "-";
268                                 mmP = dataP.getMonth() + 1 + "-";
269                                 aaaaP = 1900 + dataP.getYear();
270
271                                 // prevent page from refreshing, the site only checks for
272                                 // the existence of object/embed, not audio/video
273                                 window.refreshByJS = false;
274
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 = '';
279                                 }
280
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>";
285
286                                 // re-create stream list
287                                 slReset();
288                                 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
289                                 if (a)
290                                         streamlist.appendChild(a);
291
292                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
293                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
294                         }
295                         console.log('playAudio hacked');
296                 }
297         }
298
299         // Archivio Luce
300         if (window.ply  && window.ply.configuration
301                         && window.ply.configuration.file
302                         && window.cnt) {
303                 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
304                 var width = window.ply.configuration.width;
305                 var height = window.ply.configuration.height;
306
307                 window.cnt.innerHTML = "<video style='width:" + width +
308                                         "px; height:" + height + "' controls autostart>" +
309                                         srctag(src, 'video', null) +
310                                         "</video>";
311                 console.log(window.cnt, 'hacked');
312                 window.cnt.appendChild(H5aRaw("WMV", src));
313         }
314 })
315
316 })();