Stream list on Rai.it homepage 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 // @author      Giuseppe "Oblomov" Bilotta
10 // @version     20130322.2255
11 // ==/UserScript==
12
13 (function() {
14
15         function getURL(name) {
16                 var list = document.getElementsByName(name) ;
17                 if (list && list[0]) {
18                         return list[0].content;
19                 } else {
20                         var altname = name.toUpperCase().replace('VIDEO','video');
21                         console.log(name, altname);
22                         console.log(window[name], window[altname]);
23                         return window[altname];
24                 }
25         }
26
27         var urls = {
28                 std: getURL('videourl'),
29                 mp4: getURL('videourl_mp4'),
30                 wmv: getURL('videourl_wmv'),
31                 h264: getURL('videourl_h264'),
32                 m3u8: getURL('videourl_m3u8'),
33         }
34
35         function srctag(url, av, fmt) {
36                 if (!url)
37                         return '' ;
38                 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
39         }
40
41         function H5video(fmt) {
42                 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
43         }
44
45         // create a link element for one of the page-wide media streams
46         function H5a(fmt) {
47                 if (urls[fmt]) {
48                         var a = document.createElement('a');
49                         a.href = urls[fmt];
50                         a.innerHTML = fmt.toUpperCase();
51                         a.style='font-weight:bold;margin-left:1em;color:white';
52                         return a;
53                 }
54                 return null
55         }
56
57         // create a link element for a raw URL
58         function H5aRaw(name, url) {
59                 if (url) {
60                         var a = document.createElement('a');
61                         a.href = url;
62                         a.innerHTML = name.toUpperCase();
63                         a.style='font-weight:bold;margin-left:1em;color:red';
64                         return a;
65                 }
66                 return null
67         }
68
69         var streamlist = document.createElement("li");
70
71         // reset stream list: this is used in the playTg and playAudio
72         // functions to reset the stream list when changing stream,
73         function slReset() {
74                 streamlist.innerHTML = '<span>Stream/Download:</span>';
75         }
76         // and of course once at the beginning of it all 8-)
77         slReset();
78
79         var sch = document.getElementById("silverlightControlHost");
80         if (sch) {
81                 // debug
82                 console.log(urls);
83
84                 // find place to append the stream list to
85                 // if possible, look for an existing ul in what is likely to be the top
86                 // otherwise, create an ul and put it either in a miniLink if available,
87                 // or the top otherwise
88                 // TODO use some smarter mechanism
89                 var pp = sch.parentNode.parentNode.previousElementSibling;
90                 var specs = pp.getElementsByTagName('ul');
91                 if (!specs || !specs[0]) {
92                         specs = document.createElement('ul');
93                         specs.className = 'Specifiche';
94                         var mid = document.getElementById('Notizie');
95                         if (mid) {
96                                 mid = mid.lastElementChild;
97                                 mid.insertBefore(specs, mid.firstElementChild);
98                         } else {
99                                 pp.appendChild(specs);
100                         }
101                 } else {
102                         specs = specs[0];
103                 }
104                 specs.appendChild(streamlist);
105                 console.log(streamlist, 'appended');
106
107                 // if we are on a page that defines the videourl* metas, go straight to creating
108                 // the video element
109                 if (urls.std) {
110                         // prevent other JS with messing with this element further
111                         sch.id = 'html5mediaHost';
112                         sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
113                                 H5video('mp4') + H5video('wmv') +
114                                 H5video('h264') + H5video('m3u8') +
115                                 H5video('std') +
116                                 "<h3>Sorry, no supported video format found :-(</h3></video>";
117                         console.log(sch, 'hacked');
118
119                         for (var fmt in urls) {
120                                 var a = H5a(fmt);
121                                 if (a)
122                                         streamlist.appendChild(a);
123                         }
124                         return; // done
125                 }
126
127                 // hack the play* JS functions
128                 if (window.playTg) {
129                         window.playTg = function (liveTg, video, h264, androidUrl) {
130                                 var dataP = new Date();
131                                 var ggP, mmP, aaaaP;
132                                 ggP = dataP.getDate() + "-";
133                                 mmP = dataP.getMonth() + 1 + "-";
134                                 aaaaP = 1900 + dataP.getYear();
135
136                                 // stop loading any current audio/video
137                                 if (sch.firstChild && sch.firstChild.src) {
138                                         console.log('stopping current A/V');
139                                         sch.firstChild.src = '';
140                                 }
141
142                                 // re-create the video player
143                                 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
144                                         srctag(h264, 'video') +
145                                         srctag(androidUrl, 'video') +
146                                         srctag(video, 'video') +
147                                         "<h3>Sorry, no supported video format found :-(</h3></video>";
148
149                                 // re-create stream list
150                                 slReset();
151                                 // oh we would like to indicate the media type somehow,
152                                 // but it seems to be somewhat random. Not even a HEAD
153                                 // on the URL works reliably all the time
154                                 var a = H5aRaw('1', h264);
155                                 if (a)
156                                         streamlist.appendChild(a);
157                                 if (androidUrl != h264) {
158                                         a = H5aRaw('2', androidUrl);
159                                         if (a)
160                                                 streamlist.appendChild(a);
161                                 }
162                                 if ((video != androidUrl) && (video != h264)) {
163                                         a = H5aRaw('3', video);
164                                         if (a)
165                                                 streamlist.appendChild(a);
166                                 }
167
168                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
169                                 console.log('playTg', liveTg, video, h264, androidUrl)
170                         }
171                         console.log('playTg hacked');
172                 }
173
174                 if (window.playAudio) {
175                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
176                                 var dataP = new Date();
177                                 var ggP, mmP, aaaaP;
178                                 ggP = dataP.getDate() + "-";
179                                 mmP = dataP.getMonth() + 1 + "-";
180                                 aaaaP = 1900 + dataP.getYear();
181
182                                 // stop loading any current audio/video
183                                 if (sch.firstChild && sch.firstChild.src) {
184                                         console.log('stopping current A/V');
185                                         sch.firstChild.src = '';
186                                 }
187
188                                 // re-create the audio player
189                                 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
190                                         srctag(mediaUrl, 'audio') +
191                                         "<h3>Sorry, no supported audio format found :-(</h3></audio>";
192
193                                 // re-create stream list
194                                 slReset();
195                                 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
196                                 if (a)
197                                         streamlist.appendChild(a);
198
199                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
200                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
201                         }
202                         console.log('playAudio hacked');
203                 }
204         }
205 })();