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