Stop page from refreshing if audio/video is active
[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                                 // prevent page from refreshing, the site only checks for
137                                 // the existence of object/embed, not audio/video
138                                 window.refreshByJS = false;
139
140                                 // stop loading any current audio/video
141                                 if (sch.firstChild && sch.firstChild.src) {
142                                         console.log('stopping current A/V');
143                                         sch.firstChild.src = '';
144                                 }
145
146                                 // re-create the video player
147                                 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
148                                         srctag(h264, 'video') +
149                                         srctag(androidUrl, 'video') +
150                                         srctag(video, 'video') +
151                                         "<h3>Sorry, no supported video format found :-(</h3></video>";
152
153                                 // re-create stream list
154                                 slReset();
155                                 // oh we would like to indicate the media type somehow,
156                                 // but it seems to be somewhat random. Not even a HEAD
157                                 // on the URL works reliably all the time
158                                 var a = H5aRaw('1', h264);
159                                 if (a)
160                                         streamlist.appendChild(a);
161                                 if (androidUrl != h264) {
162                                         a = H5aRaw('2', androidUrl);
163                                         if (a)
164                                                 streamlist.appendChild(a);
165                                 }
166                                 if ((video != androidUrl) && (video != h264)) {
167                                         a = H5aRaw('3', video);
168                                         if (a)
169                                                 streamlist.appendChild(a);
170                                 }
171
172                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
173                                 console.log('playTg', liveTg, video, h264, androidUrl)
174                         }
175                         console.log('playTg hacked');
176                 }
177
178                 if (window.playAudio) {
179                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
180                                 var dataP = new Date();
181                                 var ggP, mmP, aaaaP;
182                                 ggP = dataP.getDate() + "-";
183                                 mmP = dataP.getMonth() + 1 + "-";
184                                 aaaaP = 1900 + dataP.getYear();
185
186                                 // prevent page from refreshing, the site only checks for
187                                 // the existence of object/embed, not audio/video
188                                 window.refreshByJS = false;
189
190                                 // stop loading any current audio/video
191                                 if (sch.firstChild && sch.firstChild.src) {
192                                         console.log('stopping current A/V');
193                                         sch.firstChild.src = '';
194                                 }
195
196                                 // re-create the audio player
197                                 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
198                                         srctag(mediaUrl, 'audio') +
199                                         "<h3>Sorry, no supported audio format found :-(</h3></audio>";
200
201                                 // re-create stream list
202                                 slReset();
203                                 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
204                                 if (a)
205                                         streamlist.appendChild(a);
206
207                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
208                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
209                         }
210                         console.log('playAudio hacked');
211                 }
212         }
213 })();