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);
33 function getURL(name) {
34 var list = document.getElementsByName(name) ;
35 if (list && list[0]) {
36 return list[0].content;
38 var altname = name.toUpperCase().replace('VIDEO',
39 'video').replace('ANDROID', 'android');
40 console.log(name, altname);
41 console.log(window[name], window[altname]);
42 return window[altname];
47 std: getURL('videourl'),
48 mp4: getURL('videourl_mp4'),
49 wmv: getURL('videourl_wmv'),
50 h264: getURL('videourl_h264'),
51 m3u8: getURL('videourl_m3u8'),
52 m3u8_android: getURL('videourl_m3u8_android'),
55 function srctag(url, av, fmt) {
60 f = fmt.replace('_android','');
62 return '<source src="' + url + '" ' + (f ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
65 function H5video(fmt) {
66 return srctag(urls[fmt], 'video', fmt);
69 // create a link element for one of the page-wide media streams
72 var a = document.createElement('a');
74 a.innerHTML = fmt.toUpperCase();
75 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
81 // create a link element for a raw URL
82 function H5aRaw(name, url) {
84 var a = document.createElement('a');
86 a.innerHTML = name.toUpperCase();
87 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
93 var streamlist = document.createElement("li");
95 // reset stream list: this is used in the playTg and playAudio
96 // functions to reset the stream list when changing stream,
98 streamlist.innerHTML = '<span>Stream/Download:</span>';
100 // and of course once at the beginning of it all 8-)
103 var sch = document.getElementById("silverlightControlHost");
108 // find place to append the stream list to
109 // if possible, look for an existing ul in what is likely to be the top
110 // otherwise, create an ul and put it either in a miniLink if available,
111 // or the top otherwise
112 // TODO use some smarter mechanism
113 var pp = sch.parentNode.parentNode.previousElementSibling;
114 var specs = pp.getElementsByTagName('ul');
115 if (!specs || !specs[0]) {
116 specs = document.createElement('ul');
117 specs.className = 'Specifiche';
118 var mid = document.getElementById('Notizie');
120 mid = mid.lastElementChild;
121 mid.insertBefore(specs, mid.firstElementChild);
123 pp.appendChild(specs);
128 specs.appendChild(streamlist);
129 console.log(streamlist, 'appended');
131 // if we are on a page that defines the videourl* metas, go straight to creating
134 // prevent other JS with messing with this element further
135 sch.id = 'html5mediaHost';
136 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
137 H5video('mp4') + H5video('wmv') + H5video('h264') +
138 H5video('m3u8') + H5video('m3u8_android') +
140 "<h3>Sorry, no supported video format found :-(</h3></video>";
141 console.log(sch, 'hacked');
143 for (var fmt in urls) {
146 streamlist.appendChild(a);
151 // hack the play* JS functions
153 window.playTg = function (liveTg, video, h264, androidUrl) {
154 var dataP = new Date();
156 ggP = dataP.getDate() + "-";
157 mmP = dataP.getMonth() + 1 + "-";
158 aaaaP = 1900 + dataP.getYear();
160 // prevent page from refreshing, the site only checks for
161 // the existence of object/embed, not audio/video
162 window.refreshByJS = false;
164 // stop loading any current audio/video
165 if (sch.firstChild && sch.firstChild.src) {
166 console.log('stopping current A/V');
167 sch.firstChild.src = '';
170 // re-create the video player
171 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
172 srctag(h264, 'video') +
173 srctag(androidUrl, 'video') +
174 srctag(video, 'video') +
175 "<h3>Sorry, no supported video format found :-(</h3></video>";
177 // re-create stream list
179 // oh we would like to indicate the media type somehow,
180 // but it seems to be somewhat random. Not even a HEAD
181 // on the URL works reliably all the time
182 var a = H5aRaw('1', h264);
184 streamlist.appendChild(a);
185 if (androidUrl != h264) {
186 a = H5aRaw('2', androidUrl);
188 streamlist.appendChild(a);
190 if ((video != androidUrl) && (video != h264)) {
191 a = H5aRaw('3', video);
193 streamlist.appendChild(a);
196 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
197 console.log('playTg', liveTg, video, h264, androidUrl)
199 console.log('playTg hacked');
202 if (window.playAudio) {
203 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
204 var dataP = new Date();
206 ggP = dataP.getDate() + "-";
207 mmP = dataP.getMonth() + 1 + "-";
208 aaaaP = 1900 + dataP.getYear();
210 // prevent page from refreshing, the site only checks for
211 // the existence of object/embed, not audio/video
212 window.refreshByJS = false;
214 // stop loading any current audio/video
215 if (sch.firstChild && sch.firstChild.src) {
216 console.log('stopping current A/V');
217 sch.firstChild.src = '';
220 // re-create the audio player
221 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
222 srctag(mediaUrl, 'audio') +
223 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
225 // re-create stream list
227 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
229 streamlist.appendChild(a);
231 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
232 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
234 console.log('playAudio hacked');
239 if (window.ply && window.ply.configuration
240 && window.ply.configuration.file
242 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
243 var width = window.ply.configuration.width;
244 var height = window.ply.configuration.height;
246 window.cnt.innerHTML = "<video style='width:" + width +
247 "px; height:" + height + "' controls autostart>" +
248 srctag(src, 'video', null) +
250 console.log(window.cnt, 'hacked');
251 window.cnt.appendChild(H5aRaw("WMV", src));