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','video');
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'),
53 function srctag(url, av, fmt) {
56 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
59 function H5video(fmt) {
60 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
63 // create a link element for one of the page-wide media streams
66 var a = document.createElement('a');
68 a.innerHTML = fmt.toUpperCase();
69 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
75 // create a link element for a raw URL
76 function H5aRaw(name, url) {
78 var a = document.createElement('a');
80 a.innerHTML = name.toUpperCase();
81 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
87 var streamlist = document.createElement("li");
89 // reset stream list: this is used in the playTg and playAudio
90 // functions to reset the stream list when changing stream,
92 streamlist.innerHTML = '<span>Stream/Download:</span>';
94 // and of course once at the beginning of it all 8-)
97 var sch = document.getElementById("silverlightControlHost");
102 // find place to append the stream list to
103 // if possible, look for an existing ul in what is likely to be the top
104 // otherwise, create an ul and put it either in a miniLink if available,
105 // or the top otherwise
106 // TODO use some smarter mechanism
107 var pp = sch.parentNode.parentNode.previousElementSibling;
108 var specs = pp.getElementsByTagName('ul');
109 if (!specs || !specs[0]) {
110 specs = document.createElement('ul');
111 specs.className = 'Specifiche';
112 var mid = document.getElementById('Notizie');
114 mid = mid.lastElementChild;
115 mid.insertBefore(specs, mid.firstElementChild);
117 pp.appendChild(specs);
122 specs.appendChild(streamlist);
123 console.log(streamlist, 'appended');
125 // if we are on a page that defines the videourl* metas, go straight to creating
128 // prevent other JS with messing with this element further
129 sch.id = 'html5mediaHost';
130 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
131 H5video('mp4') + H5video('wmv') +
132 H5video('h264') + H5video('m3u8') +
134 "<h3>Sorry, no supported video format found :-(</h3></video>";
135 console.log(sch, 'hacked');
137 for (var fmt in urls) {
140 streamlist.appendChild(a);
145 // hack the play* JS functions
147 window.playTg = function (liveTg, video, h264, androidUrl) {
148 var dataP = new Date();
150 ggP = dataP.getDate() + "-";
151 mmP = dataP.getMonth() + 1 + "-";
152 aaaaP = 1900 + dataP.getYear();
154 // prevent page from refreshing, the site only checks for
155 // the existence of object/embed, not audio/video
156 window.refreshByJS = false;
158 // stop loading any current audio/video
159 if (sch.firstChild && sch.firstChild.src) {
160 console.log('stopping current A/V');
161 sch.firstChild.src = '';
164 // re-create the video player
165 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
166 srctag(h264, 'video') +
167 srctag(androidUrl, 'video') +
168 srctag(video, 'video') +
169 "<h3>Sorry, no supported video format found :-(</h3></video>";
171 // re-create stream list
173 // oh we would like to indicate the media type somehow,
174 // but it seems to be somewhat random. Not even a HEAD
175 // on the URL works reliably all the time
176 var a = H5aRaw('1', h264);
178 streamlist.appendChild(a);
179 if (androidUrl != h264) {
180 a = H5aRaw('2', androidUrl);
182 streamlist.appendChild(a);
184 if ((video != androidUrl) && (video != h264)) {
185 a = H5aRaw('3', video);
187 streamlist.appendChild(a);
190 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
191 console.log('playTg', liveTg, video, h264, androidUrl)
193 console.log('playTg hacked');
196 if (window.playAudio) {
197 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
198 var dataP = new Date();
200 ggP = dataP.getDate() + "-";
201 mmP = dataP.getMonth() + 1 + "-";
202 aaaaP = 1900 + dataP.getYear();
204 // prevent page from refreshing, the site only checks for
205 // the existence of object/embed, not audio/video
206 window.refreshByJS = false;
208 // stop loading any current audio/video
209 if (sch.firstChild && sch.firstChild.src) {
210 console.log('stopping current A/V');
211 sch.firstChild.src = '';
214 // re-create the audio player
215 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
216 srctag(mediaUrl, 'audio') +
217 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
219 // re-create stream list
221 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
223 streamlist.appendChild(a);
225 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
226 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
228 console.log('playAudio hacked');
233 if (window.ply && window.ply.configuration
234 && window.ply.configuration.file
236 var src = window.ply.configuration.file.replace(/^mms:\/\//,'http://');
237 var width = window.ply.configuration.width;
238 var height = window.ply.configuration.height;
240 window.cnt.innerHTML = "<video style='width:" + width +
241 "px; height:" + height + "' controls autostart>" +
242 srctag(src, 'video', null) +
244 console.log(window.cnt, 'hacked');
245 window.cnt.appendChild(H5aRaw("WMV", src));