Preliminary support for Rai Replay
[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 // @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     20130607.2301
13 // ==/UserScript==
14
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.
18  */
19
20 (function() {
21
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);
27 }
28
29
30 addScript(
31 function() {
32         function getURL(name) {
33                 var list = document.getElementsByName(name) ;
34                 if (list && list[0]) {
35                         return list[0].content;
36                 } else {
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];
42                 }
43         }
44
45         var urls = {
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'),
52         }
53
54         function srctag(url, av, fmt) {
55                 if (!url)
56                         return '' ;
57                 var f = null;
58                 if (fmt != 'std') {
59                         f = fmt.replace('_android','');
60                 }
61                 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
62         }
63
64         function H5video(fmt) {
65                 return srctag(urls[fmt], 'video', fmt);
66         }
67
68         // create a link element for one of the page-wide media streams
69         function H5a(fmt) {
70                 if (urls[fmt]) {
71                         var a = document.createElement('a');
72                         a.href = urls[fmt];
73                         a.innerHTML = fmt.toUpperCase();
74                         a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
75                         return a;
76                 }
77                 return null
78         }
79
80         // create a link element for a raw URL
81         function H5aRaw(name, url) {
82                 if (url) {
83                         var a = document.createElement('a');
84                         a.href = url;
85                         a.innerHTML = name.toUpperCase();
86                         a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
87                         return a;
88                 }
89                 return null
90         }
91
92         var streamlist = document.createElement("li");
93
94         // reset stream list: this is used in the playTg and playAudio
95         // functions to reset the stream list when changing stream,
96         function slReset() {
97                 streamlist.innerHTML = '<span>Stream/Download:</span>';
98         }
99         // and of course once at the beginning of it all 8-)
100         slReset();
101
102         if (window.replayTv && window.replayTv.vid2data[replayTv.currVideo]) {
103                 urls.std = window.replayTv.vid2data[replayTv.currVideo].h264;
104         }
105         var sch = document.getElementById("silverlightControlHost");
106         if (!sch) {
107                 sch = document.getElementById("SilverlightPlayer");
108                 sch.style.width = '100%';
109                 sch.style.height = '100%';
110         }
111         if (sch) {
112                 // debug
113                 console.log(urls);
114
115                 // find place to append the stream list to
116                 // if possible, look for an existing ul in what is likely to be the top
117                 // otherwise, create an ul and put it either in a miniLink if available,
118                 // or the top otherwise
119                 // TODO use some smarter mechanism
120                 var pp = sch.parentNode.parentNode.previousElementSibling;
121                 var specs = pp.getElementsByTagName('ul');
122                 if (!specs || !specs[0]) {
123                         specs = document.createElement('ul');
124                         specs.className = 'Specifiche';
125                         var mid = document.getElementById('Notizie');
126                         if (mid) {
127                                 mid = mid.lastElementChild;
128                                 mid.insertBefore(specs, mid.firstElementChild);
129                         } else {
130                                 pp.appendChild(specs);
131                         }
132                 } else {
133                         specs = specs[0];
134                 }
135                 specs.appendChild(streamlist);
136                 console.log(streamlist, 'appended');
137
138                 // if we are on a page that defines the videourl* metas, go straight to creating
139                 // the video element
140                 if (urls.std) {
141                         // prevent other JS with messing with this element further
142                         sch.id = 'html5mediaHost';
143                         sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
144                                 H5video('mp4') + H5video('wmv') + H5video('h264') +
145                                 H5video('m3u8') + H5video('m3u8_android') +
146                                 H5video('std') +
147                                 "<h3>Sorry, no supported video format found :-(</h3></video>";
148                         console.log(sch, 'hacked');
149
150                         for (var fmt in urls) {
151                                 var a = H5a(fmt);
152                                 if (a)
153                                         streamlist.appendChild(a);
154                         }
155                         return; // done
156                 }
157
158                 // hack the play* JS functions
159                 if (window.playTg) {
160                         window.playTg = function (liveTg, video, h264, androidUrl) {
161                                 var dataP = new Date();
162                                 var ggP, mmP, aaaaP;
163                                 ggP = dataP.getDate() + "-";
164                                 mmP = dataP.getMonth() + 1 + "-";
165                                 aaaaP = 1900 + dataP.getYear();
166
167                                 // prevent page from refreshing, the site only checks for
168                                 // the existence of object/embed, not audio/video
169                                 window.refreshByJS = false;
170
171                                 // stop loading any current audio/video
172                                 if (sch.firstChild && sch.firstChild.src) {
173                                         console.log('stopping current A/V');
174                                         sch.firstChild.src = '';
175                                 }
176
177                                 // re-create the video player
178                                 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
179                                         srctag(h264, 'video') +
180                                         srctag(androidUrl, 'video') +
181                                         srctag(video, 'video') +
182                                         "<h3>Sorry, no supported video format found :-(</h3></video>";
183
184                                 // re-create stream list
185                                 slReset();
186                                 // oh we would like to indicate the media type somehow,
187                                 // but it seems to be somewhat random. Not even a HEAD
188                                 // on the URL works reliably all the time
189                                 var a = H5aRaw('1', h264);
190                                 if (a)
191                                         streamlist.appendChild(a);
192                                 if (androidUrl != h264) {
193                                         a = H5aRaw('2', androidUrl);
194                                         if (a)
195                                                 streamlist.appendChild(a);
196                                 }
197                                 if ((video != androidUrl) && (video != h264)) {
198                                         a = H5aRaw('3', video);
199                                         if (a)
200                                                 streamlist.appendChild(a);
201                                 }
202
203                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
204                                 console.log('playTg', liveTg, video, h264, androidUrl)
205                         }
206                         console.log('playTg hacked');
207                 }
208
209                 if (window.playAudio) {
210                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
211                                 var dataP = new Date();
212                                 var ggP, mmP, aaaaP;
213                                 ggP = dataP.getDate() + "-";
214                                 mmP = dataP.getMonth() + 1 + "-";
215                                 aaaaP = 1900 + dataP.getYear();
216
217                                 // prevent page from refreshing, the site only checks for
218                                 // the existence of object/embed, not audio/video
219                                 window.refreshByJS = false;
220
221                                 // stop loading any current audio/video
222                                 if (sch.firstChild && sch.firstChild.src) {
223                                         console.log('stopping current A/V');
224                                         sch.firstChild.src = '';
225                                 }
226
227                                 // re-create the audio player
228                                 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
229                                         srctag(mediaUrl, 'audio') +
230                                         "<h3>Sorry, no supported audio format found :-(</h3></audio>";
231
232                                 // re-create stream list
233                                 slReset();
234                                 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
235                                 if (a)
236                                         streamlist.appendChild(a);
237
238                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
239                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
240                         }
241                         console.log('playAudio hacked');
242                 }
243         }
244
245         // Archivio Luce
246         if (window.ply  && window.ply.configuration
247                         && window.ply.configuration.file
248                         && window.cnt) {
249                 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
250                 var width = window.ply.configuration.width;
251                 var height = window.ply.configuration.height;
252
253                 window.cnt.innerHTML = "<video style='width:" + width +
254                                         "px; height:" + height + "' controls autostart>" +
255                                         srctag(src, 'video', null) +
256                                         "</video>";
257                 console.log(window.cnt, 'hacked');
258                 window.cnt.appendChild(H5aRaw("WMV", src));
259         }
260 })
261
262 })();