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
10 // @version 20130322.2255
15 function getURL(name) {
16 var list = document.getElementsByName(name) ;
17 if (list && list[0]) {
18 return list[0].content;
20 var altname = name.toUpperCase().replace('VIDEO','video');
21 console.log(name, altname);
22 console.log(window[name], window[altname]);
23 return window[altname];
28 std: getURL('videourl'),
29 mp4: getURL('videourl_mp4'),
30 wmv: getURL('videourl_wmv'),
31 h264: getURL('videourl_h264'),
32 m3u8: getURL('videourl_m3u8'),
35 function srctag(url, av, fmt) {
38 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
41 function H5video(fmt) {
42 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
45 // create a link element for one of the page-wide media streams
48 var a = document.createElement('a');
50 a.innerHTML = fmt.toUpperCase();
51 a.style='font-weight:bold;margin-left:1em;color:white';
57 // create a link element for a raw URL
58 function H5aRaw(name, url) {
60 var a = document.createElement('a');
62 a.innerHTML = name.toUpperCase();
63 a.style='font-weight:bold;margin-left:1em;color:red';
69 var streamlist = document.createElement("li");
71 // reset stream list: this is used in the playTg and playAudio
72 // functions to reset the stream list when changing stream,
74 streamlist.innerHTML = '<span>Stream/Download:</span>';
76 // and of course once at the beginning of it all 8-)
79 var sch = document.getElementById("silverlightControlHost");
84 // find place to append the stream list to
85 // if possible, look for an existing ul in what is likely to be the top
86 // otherwise, create an ul and put it either in a miniLink if available,
87 // or the top otherwise
88 // TODO use some smarter mechanism
89 var pp = sch.parentNode.parentNode.previousElementSibling;
90 var specs = pp.getElementsByTagName('ul');
91 if (!specs || !specs[0]) {
92 specs = document.createElement('ul');
93 specs.className = 'Specifiche';
94 var mid = document.getElementById('Notizie');
96 mid = mid.lastElementChild;
97 mid.insertBefore(specs, mid.firstElementChild);
99 pp.appendChild(specs);
104 specs.appendChild(streamlist);
105 console.log(streamlist, 'appended');
107 // if we are on a page that defines the videourl* metas, go straight to creating
110 // prevent other JS with messing with this element further
111 sch.id = 'html5mediaHost';
112 sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
113 H5video('mp4') + H5video('wmv') +
114 H5video('h264') + H5video('m3u8') +
116 "<h3>Sorry, no supported video format found :-(</h3></video>";
117 console.log(sch, 'hacked');
119 for (var fmt in urls) {
122 streamlist.appendChild(a);
127 // hack the play* JS functions
129 window.playTg = function (liveTg, video, h264, androidUrl) {
130 var dataP = new Date();
132 ggP = dataP.getDate() + "-";
133 mmP = dataP.getMonth() + 1 + "-";
134 aaaaP = 1900 + dataP.getYear();
136 // stop loading any current audio/video
137 if (sch.firstChild && sch.firstChild.src) {
138 console.log('stopping current A/V');
139 sch.firstChild.src = '';
142 // re-create the video player
143 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
144 srctag(h264, 'video') +
145 srctag(androidUrl, 'video') +
146 srctag(video, 'video') +
147 "<h3>Sorry, no supported video format found :-(</h3></video>";
149 // re-create stream list
151 // oh we would like to indicate the media type somehow,
152 // but it seems to be somewhat random. Not even a HEAD
153 // on the URL works reliably all the time
154 var a = H5aRaw('1', h264);
156 streamlist.appendChild(a);
157 if (androidUrl != h264) {
158 a = H5aRaw('2', androidUrl);
160 streamlist.appendChild(a);
162 if ((video != androidUrl) && (video != h264)) {
163 a = H5aRaw('3', video);
165 streamlist.appendChild(a);
168 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
169 console.log('playTg', liveTg, video, h264, androidUrl)
171 console.log('playTg hacked');
174 if (window.playAudio) {
175 window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
176 var dataP = new Date();
178 ggP = dataP.getDate() + "-";
179 mmP = dataP.getMonth() + 1 + "-";
180 aaaaP = 1900 + dataP.getYear();
182 // stop loading any current audio/video
183 if (sch.firstChild && sch.firstChild.src) {
184 console.log('stopping current A/V');
185 sch.firstChild.src = '';
188 // re-create the audio player
189 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
190 srctag(mediaUrl, 'audio') +
191 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
193 // re-create stream list
195 var a = H5aRaw(mediatype ? mediatype : '(???)', mediaUrl);
197 streamlist.appendChild(a);
199 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
200 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
202 console.log('playAudio hacked');