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 /* Since the Chrome sandbox is _extremely_ restrictive and we can't access things such as window.videoURL from the actual document,
14 * we have to resort to script injection to be completely cross-platform.
19 function addScript(source) {
20 var script = document.createElement('script');
21 script.setAttribute("type", "application/javascript");
22 script.textContent = '(' + source + ')();';
23 document.body.appendChild(script);
30 function getURL(name) {
31 var list = document.getElementsByName(name) ;
32 if (list && list[0]) {
33 return list[0].content;
35 var altname = name.toUpperCase().replace('VIDEO','video');
36 console.log(name, altname);
37 console.log(window[name], window[altname]);
38 return window[altname];
43 std: getURL('videourl'),
44 mp4: getURL('videourl_mp4'),
45 wmv: getURL('videourl_wmv'),
46 h264: getURL('videourl_h264'),
47 m3u8: getURL('videourl_m3u8'),
50 function srctag(url, av, fmt) {
53 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
56 function H5video(fmt) {
57 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
60 // create a link element for one of the page-wide media streams
63 var a = document.createElement('a');
65 a.innerHTML = fmt.toUpperCase();
66 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:white');
72 // create a link element for a raw URL
73 function H5aRaw(name, url) {
75 var a = document.createElement('a');
77 a.innerHTML = name.toUpperCase();
78 a.setAttribute('style', 'font-weight:bold;margin-left:1em;color:red');
84 var streamlist = document.createElement("li");
86 // reset stream list: this is used in the playTg and playAudio
87 // functions to reset the stream list when changing stream,
89 streamlist.innerHTML = '<span>Stream/Download:</span>';
91 // and of course once at the beginning of it all 8-)
94 var sch = document.getElementById("silverlightControlHost");
99 // find place to append the stream list to
100 // if possible, look for an existing ul in what is likely to be the top
101 // otherwise, create an ul and put it either in a miniLink if available,
102 // or the top otherwise
103 // TODO use some smarter mechanism
104 var pp = sch.parentNode.parentNode.previousElementSibling;
105 var specs = pp.getElementsByTagName('ul');
106 if (!specs || !specs[0]) {
107 specs = document.createElement('ul');
108 specs.className = 'Specifiche';
109 var mid = document.getElementById('Notizie');
111 mid = mid.lastElementChild;
112 mid.insertBefore(specs, mid.firstElementChild);
114 pp.appendChild(specs);
119 specs.appendChild(streamlist);
120 console.log(streamlist, 'appended');
122 // if we are on a page that defines the videourl* metas, go straight to creating
125 // prevent other JS with messing with this element further
126 sch.id = 'html5mediaHost';
127 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
128 H5video('mp4') + H5video('wmv') +
129 H5video('h264') + H5video('m3u8') +
131 "<h3>Sorry, no supported video format found :-(</h3></video>";
132 console.log(sch, 'hacked');
134 for (var fmt in urls) {
137 streamlist.appendChild(a);
142 // hack the play* JS functions
144 window.playTg = function (liveTg, video, h264, androidUrl) {
145 var dataP = new Date();
147 ggP = dataP.getDate() + "-";
148 mmP = dataP.getMonth() + 1 + "-";
149 aaaaP = 1900 + dataP.getYear();
151 // prevent page from refreshing, the site only checks for
152 // the existence of object/embed, not audio/video
153 window.refreshByJS = false;
155 // stop loading any current audio/video
156 if (sch.firstChild && sch.firstChild.src) {
157 console.log('stopping current A/V');
158 sch.firstChild.src = '';
161 // re-create the video player
162 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
163 srctag(h264, 'video') +
164 srctag(androidUrl, 'video') +
165 srctag(video, 'video') +
166 "<h3>Sorry, no supported video format found :-(</h3></video>";
168 // re-create stream list
170 // oh we would like to indicate the media type somehow,
171 // but it seems to be somewhat random. Not even a HEAD
172 // on the URL works reliably all the time
173 var a = H5aRaw('1', h264);
175 streamlist.appendChild(a);
176 if (androidUrl != h264) {
177 a = H5aRaw('2', androidUrl);
179 streamlist.appendChild(a);
181 if ((video != androidUrl) && (video != h264)) {
182 a = H5aRaw('3', video);
184 streamlist.appendChild(a);
187 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
188 console.log('playTg', liveTg, video, h264, androidUrl)
190 console.log('playTg hacked');
193 if (window.playAudio) {
194 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
195 var dataP = new Date();
197 ggP = dataP.getDate() + "-";
198 mmP = dataP.getMonth() + 1 + "-";
199 aaaaP = 1900 + dataP.getYear();
201 // prevent page from refreshing, the site only checks for
202 // the existence of object/embed, not audio/video
203 window.refreshByJS = false;
205 // stop loading any current audio/video
206 if (sch.firstChild && sch.firstChild.src) {
207 console.log('stopping current A/V');
208 sch.firstChild.src = '';
211 // re-create the audio player
212 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
213 srctag(mediaUrl, 'audio') +
214 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
216 // re-create stream list
218 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
220 streamlist.appendChild(a);
222 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
223 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
225 console.log('playAudio hacked');