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
13 if (typeof(unsafeWindow) == 'undefined') {
14 unsafeWindow = window;
19 function getURL(name) {
20 var list = document.getElementsByName(name) ;
21 if (list && list[0]) {
22 return list[0].content;
24 var altname = name.toUpperCase().replace('VIDEO','video');
25 console.log(name, altname);
26 console.log(unsafeWindow[name], unsafeWindow[altname]);
27 return unsafeWindow[altname];
32 std: getURL('videourl'),
33 mp4: getURL('videourl_mp4'),
34 wmv: getURL('videourl_wmv'),
35 h264: getURL('videourl_h264'),
36 m3u8: getURL('videourl_m3u8'),
39 function srctag(url, av, fmt) {
42 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
45 function H5video(fmt) {
46 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
49 // create a link element for one of the page-wide media streams
52 var a = document.createElement('a');
54 a.innerHTML = fmt.toUpperCase();
55 a.style='font-weight:bold;margin-left:1em;color:white';
61 // create a link element for a raw URL
62 function H5aRaw(name, url) {
64 var a = document.createElement('a');
66 a.innerHTML = name.toUpperCase();
67 a.style='font-weight:bold;margin-left:1em;color:red';
73 var streamlist = document.createElement("li");
75 // reset stream list: this is used in the playTg and playAudio
76 // functions to reset the stream list when changing stream,
78 streamlist.innerHTML = '<span>Stream/Download:</span>';
80 // and of course once at the beginning of it all 8-)
83 var sch = document.getElementById("silverlightControlHost");
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');
100 mid = mid.lastElementChild;
101 mid.insertBefore(specs, mid.firstElementChild);
103 pp.appendChild(specs);
108 specs.appendChild(streamlist);
109 console.log(streamlist, 'appended');
111 // if we are on a page that defines the videourl* metas, go straight to creating
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') +
120 "<h3>Sorry, no supported video format found :-(</h3></video>";
121 console.log(sch, 'hacked');
123 for (var fmt in urls) {
126 streamlist.appendChild(a);
131 // hack the play* JS functions
132 if (unsafeWindow.playTg) {
133 unsafeWindow.playTg = function (liveTg, video, h264, androidUrl) {
134 var dataP = new Date();
136 ggP = dataP.getDate() + "-";
137 mmP = dataP.getMonth() + 1 + "-";
138 aaaaP = 1900 + dataP.getYear();
140 // prevent page from refreshing, the site only checks for
141 // the existence of object/embed, not audio/video
142 unsafeWindow.refreshByJS = false;
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 = '';
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>";
157 // re-create stream list
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);
164 streamlist.appendChild(a);
165 if (androidUrl != h264) {
166 a = H5aRaw('2', androidUrl);
168 streamlist.appendChild(a);
170 if ((video != androidUrl) && (video != h264)) {
171 a = H5aRaw('3', video);
173 streamlist.appendChild(a);
176 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
177 console.log('playTg', liveTg, video, h264, androidUrl)
179 console.log('playTg hacked');
182 if (unsafeWindow.playAudio) {
183 unsafeWindow.playAudio = function (grrEdizione, mediaUrl, mediatype) {
184 var dataP = new Date();
186 ggP = dataP.getDate() + "-";
187 mmP = dataP.getMonth() + 1 + "-";
188 aaaaP = 1900 + dataP.getYear();
190 // prevent page from refreshing, the site only checks for
191 // the existence of object/embed, not audio/video
192 unsafeWindow.refreshByJS = false;
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 = '';
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>";
205 // re-create stream list
207 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
209 streamlist.appendChild(a);
211 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
212 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
214 console.log('playAudio hacked');