Add m3u8_android url too
[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
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 = document.getElementById("silverlightControlHost");
104         if (sch) {
105                 // debug
106                 console.log(urls);
107
108                 // find place to append the stream list to
109                 // if possible, look for an existing ul in what is likely to be the top
110                 // otherwise, create an ul and put it either in a miniLink if available,
111                 // or the top otherwise
112                 // TODO use some smarter mechanism
113                 var pp = sch.parentNode.parentNode.previousElementSibling;
114                 var specs = pp.getElementsByTagName('ul');
115                 if (!specs || !specs[0]) {
116                         specs = document.createElement('ul');
117                         specs.className = 'Specifiche';
118                         var mid = document.getElementById('Notizie');
119                         if (mid) {
120                                 mid = mid.lastElementChild;
121                                 mid.insertBefore(specs, mid.firstElementChild);
122                         } else {
123                                 pp.appendChild(specs);
124                         }
125                 } else {
126                         specs = specs[0];
127                 }
128                 specs.appendChild(streamlist);
129                 console.log(streamlist, 'appended');
130
131                 // if we are on a page that defines the videourl* metas, go straight to creating
132                 // the video element
133                 if (urls.std) {
134                         // prevent other JS with messing with this element further
135                         sch.id = 'html5mediaHost';
136                         sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
137                                 H5video('mp4') + H5video('wmv') + H5video('h264') +
138                                 H5video('m3u8') + H5video('m3u8_android') +
139                                 H5video('std') +
140                                 "<h3>Sorry, no supported video format found :-(</h3></video>";
141                         console.log(sch, 'hacked');
142
143                         for (var fmt in urls) {
144                                 var a = H5a(fmt);
145                                 if (a)
146                                         streamlist.appendChild(a);
147                         }
148                         return; // done
149                 }
150
151                 // hack the play* JS functions
152                 if (window.playTg) {
153                         window.playTg = function (liveTg, video, h264, androidUrl) {
154                                 var dataP = new Date();
155                                 var ggP, mmP, aaaaP;
156                                 ggP = dataP.getDate() + "-";
157                                 mmP = dataP.getMonth() + 1 + "-";
158                                 aaaaP = 1900 + dataP.getYear();
159
160                                 // prevent page from refreshing, the site only checks for
161                                 // the existence of object/embed, not audio/video
162                                 window.refreshByJS = false;
163
164                                 // stop loading any current audio/video
165                                 if (sch.firstChild && sch.firstChild.src) {
166                                         console.log('stopping current A/V');
167                                         sch.firstChild.src = '';
168                                 }
169
170                                 // re-create the video player
171                                 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
172                                         srctag(h264, 'video') +
173                                         srctag(androidUrl, 'video') +
174                                         srctag(video, 'video') +
175                                         "<h3>Sorry, no supported video format found :-(</h3></video>";
176
177                                 // re-create stream list
178                                 slReset();
179                                 // oh we would like to indicate the media type somehow,
180                                 // but it seems to be somewhat random. Not even a HEAD
181                                 // on the URL works reliably all the time
182                                 var a = H5aRaw('1', h264);
183                                 if (a)
184                                         streamlist.appendChild(a);
185                                 if (androidUrl != h264) {
186                                         a = H5aRaw('2', androidUrl);
187                                         if (a)
188                                                 streamlist.appendChild(a);
189                                 }
190                                 if ((video != androidUrl) && (video != h264)) {
191                                         a = H5aRaw('3', video);
192                                         if (a)
193                                                 streamlist.appendChild(a);
194                                 }
195
196                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
197                                 console.log('playTg', liveTg, video, h264, androidUrl)
198                         }
199                         console.log('playTg hacked');
200                 }
201
202                 if (window.playAudio) {
203                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
204                                 var dataP = new Date();
205                                 var ggP, mmP, aaaaP;
206                                 ggP = dataP.getDate() + "-";
207                                 mmP = dataP.getMonth() + 1 + "-";
208                                 aaaaP = 1900 + dataP.getYear();
209
210                                 // prevent page from refreshing, the site only checks for
211                                 // the existence of object/embed, not audio/video
212                                 window.refreshByJS = false;
213
214                                 // stop loading any current audio/video
215                                 if (sch.firstChild && sch.firstChild.src) {
216                                         console.log('stopping current A/V');
217                                         sch.firstChild.src = '';
218                                 }
219
220                                 // re-create the audio player
221                                 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
222                                         srctag(mediaUrl, 'audio') +
223                                         "<h3>Sorry, no supported audio format found :-(</h3></audio>";
224
225                                 // re-create stream list
226                                 slReset();
227                                 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
228                                 if (a)
229                                         streamlist.appendChild(a);
230
231                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
232                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
233                         }
234                         console.log('playAudio hacked');
235                 }
236         }
237
238         // Archivio Luce
239         if (window.ply  && window.ply.configuration
240                         && window.ply.configuration.file
241                         && window.cnt) {
242                 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
243                 var width = window.ply.configuration.width;
244                 var height = window.ply.configuration.height;
245
246                 window.cnt.innerHTML = "<video style='width:" + width +
247                                         "px; height:" + height + "' controls autostart>" +
248                                         srctag(src, 'video', null) +
249                                         "</video>";
250                 console.log(window.cnt, 'hacked');
251                 window.cnt.appendChild(H5aRaw("WMV", src));
252         }
253 })
254
255 })();