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
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.
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);
32 function getURL(name) {
33 var list = document.getElementsByName(name) ;
34 if (list && list[0]) {
35 return list[0].content;
37 var altname = name.toUpperCase().replace('VIDEO',
38 'video').replace('ANDROID', 'android');
39 console.log(name, altname);
40 console.log(window[name], window[altname]);
41 return window[altname];
46 std: getURL('videourl'),
47 mp4: getURL('videourl_mp4'),
48 wmv: getURL('videourl_wmv'),
49 h264: getURL('videourl_h264'),
50 m3u8: getURL('videourl_m3u8'),
51 m3u8_android: getURL('videourl_m3u8_android'),
54 function srctag(url, av, fmt) {
59 f = fmt.replace('_android','');
61 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
64 function H5video(fmt) {
65 return srctag(urls[fmt], 'video', fmt);
68 // create a link element for one of the page-wide media streams
71 var a = document.createElement('a');
73 a.innerHTML = fmt.toUpperCase();
74 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
80 // create a link element for a raw URL
81 function H5aRaw(name, url) {
83 var a = document.createElement('a');
85 a.innerHTML = name.toUpperCase();
86 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
92 var streamlist = document.createElement("li");
94 // reset stream list: this is used in the playTg and playAudio
95 // functions to reset the stream list when changing stream,
97 streamlist.innerHTML = '<span>Stream/Download:</span>';
99 // and of course once at the beginning of it all 8-)
102 if (window.replayTv && window.replayTv.vid2data[replayTv.currVideo]) {
103 urls.std = window.replayTv.vid2data[replayTv.currVideo].h264;
105 var sch = document.getElementById("silverlightControlHost");
107 sch = document.getElementById("SilverlightPlayer");
108 sch.style.width = '100%';
109 sch.style.height = '100%';
115 // find place to append the stream list to
116 // if possible, look for an existing ul in what is likely to be the top
117 // otherwise, create an ul and put it either in a miniLink if available,
118 // or the top otherwise
119 // TODO use some smarter mechanism
120 var pp = sch.parentNode.parentNode.previousElementSibling;
121 var specs = pp.getElementsByTagName('ul');
122 if (!specs || !specs[0]) {
123 specs = document.createElement('ul');
124 specs.className = 'Specifiche';
125 var mid = document.getElementById('Notizie');
127 mid = mid.lastElementChild;
128 mid.insertBefore(specs, mid.firstElementChild);
130 pp.appendChild(specs);
135 specs.appendChild(streamlist);
136 console.log(streamlist, 'appended');
138 // if we are on a page that defines the videourl* metas, go straight to creating
141 // prevent other JS with messing with this element further
142 sch.id = 'html5mediaHost';
143 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
144 H5video('mp4') + H5video('wmv') + H5video('h264') +
145 H5video('m3u8') + H5video('m3u8_android') +
147 "<h3>Sorry, no supported video format found :-(</h3></video>";
148 console.log(sch, 'hacked');
150 for (var fmt in urls) {
153 streamlist.appendChild(a);
158 // hack the play* JS functions
160 window.playTg = function (liveTg, video, h264, androidUrl) {
161 var dataP = new Date();
163 ggP = dataP.getDate() + "-";
164 mmP = dataP.getMonth() + 1 + "-";
165 aaaaP = 1900 + dataP.getYear();
167 // prevent page from refreshing, the site only checks for
168 // the existence of object/embed, not audio/video
169 window.refreshByJS = false;
171 // stop loading any current audio/video
172 if (sch.firstChild && sch.firstChild.src) {
173 console.log('stopping current A/V');
174 sch.firstChild.src = '';
177 // re-create the video player
178 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
179 srctag(h264, 'video') +
180 srctag(androidUrl, 'video') +
181 srctag(video, 'video') +
182 "<h3>Sorry, no supported video format found :-(</h3></video>";
184 // re-create stream list
186 // oh we would like to indicate the media type somehow,
187 // but it seems to be somewhat random. Not even a HEAD
188 // on the URL works reliably all the time
189 var a = H5aRaw('1', h264);
191 streamlist.appendChild(a);
192 if (androidUrl != h264) {
193 a = H5aRaw('2', androidUrl);
195 streamlist.appendChild(a);
197 if ((video != androidUrl) && (video != h264)) {
198 a = H5aRaw('3', video);
200 streamlist.appendChild(a);
203 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
204 console.log('playTg', liveTg, video, h264, androidUrl)
206 console.log('playTg hacked');
209 if (window.playAudio) {
210 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
211 var dataP = new Date();
213 ggP = dataP.getDate() + "-";
214 mmP = dataP.getMonth() + 1 + "-";
215 aaaaP = 1900 + dataP.getYear();
217 // prevent page from refreshing, the site only checks for
218 // the existence of object/embed, not audio/video
219 window.refreshByJS = false;
221 // stop loading any current audio/video
222 if (sch.firstChild && sch.firstChild.src) {
223 console.log('stopping current A/V');
224 sch.firstChild.src = '';
227 // re-create the audio player
228 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
229 srctag(mediaUrl, 'audio') +
230 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
232 // re-create stream list
234 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
236 streamlist.appendChild(a);
238 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
239 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
241 console.log('playAudio hacked');
246 if (window.ply && window.ply.configuration
247 && window.ply.configuration.file
249 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
250 var width = window.ply.configuration.width;
251 var height = window.ply.configuration.height;
253 window.cnt.innerHTML = "<video style='width:" + width +
254 "px; height:" + height + "' controls autostart>" +
255 srctag(src, 'video', null) +
257 console.log(window.cnt, 'hacked');
258 window.cnt.appendChild(H5aRaw("WMV", src));