Some support for streaming Rai pages
[raitv.user.js] / raitv.user.js
1 // ==UserScript==
2 // @name        RaiTV video
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
11 // ==/UserScript==
12
13 (function() {
14
15         function getURL(name) {
16                 var list = document.getElementsByName(name) ;
17                 if (list && list[0]) {
18                         return list[0].content;
19                 } else {
20                         var altname = name.toUpperCase().replace('VIDEO','video');
21                         console.log(name, altname);
22                         console.log(window[name], window[altname]);
23                         return window[altname];
24                 }
25         }
26
27         var urls = {
28                 std: getURL('videourl'),
29                 mp4: getURL('videourl_mp4'),
30                 wmv: getURL('videourl_wmv'),
31                 h264: getURL('videourl_h264'),
32                 m3u8: getURL('videourl_m3u8'),
33         }
34
35         function srctag(url, av, fmt) {
36                 if (!url)
37                         return '' ;
38                 return '<source src="' + url + '" ' + (fmt ? 'type="' + av + '/' + fmt + '" ' : '') + '/>'
39         }
40
41         function H5video(fmt) {
42                 return srctag(urls[fmt], 'video', (fmt == 'std' ? null : fmt));
43         }
44
45         function H5a(fmt) {
46                 if (urls[fmt]) {
47                         var a = document.createElement('a');
48                         a.href = urls[fmt];
49                         a.innerHTML = fmt.toUpperCase();
50                         a.style='font-weight:bold;margin-left:1em;color:white';
51                         return a;
52                 }
53                 return null
54         }
55
56         var sch = document.getElementById("silverlightControlHost");
57         if (sch) {
58                 console.log(urls);
59                 // if we are on a page that defines the videourl* metas, go straight to creating
60                 // the video element
61                 if (urls.std) {
62                         // prevent other JS with messing with this element further
63                         sch.id = 'html5mediaHost';
64                         sch.innerHTML = "<video style='width:100%; height:100%' controls>" +
65                                 H5video('mp4') + H5video('wmv') +
66                                 H5video('h264') + H5video('m3u8') +
67                                 H5video('std') +
68                                 "<h3>Sorry, no supported video format found :-(</h3></video>";
69                         console.log(sch, 'hacked');
70
71                         var list = document.createElement("li");
72                         list.id = 'mediaurl';
73                         list.innerHTML = '<span>Stream/Download:</span>';
74                         for (var fmt in urls) {
75                                 var a = H5a(fmt);
76                                 if (a)
77                                         list.appendChild(a);
78                         }
79                         // find place to append the list to:
80                         var pp = sch.parentNode.parentNode.previousElementSibling;
81                         var specs = pp.getElementsByTagName('ul');
82                         if (!specs || !specs[0]) {
83                                 specs = document.createElement('ul');
84                                 specs.className = 'Specifiche';
85                                 pp.appendChild(specs);
86                         } else {
87                                 specs = specs[0];
88                         }
89                         specs.appendChild(list);
90                         console.log(list, 'enumerated and appended');
91                         return; // done
92                 }
93
94                 // hack the play* JS functions
95                 if (window.playTg) {
96                         window.playTg = function (liveTg, video, h264, androidUrl) {
97                                 var dataP = new Date();
98                                 var ggP, mmP, aaaaP;
99                                 ggP = dataP.getDate() + "-";
100                                 mmP = dataP.getMonth() + 1 + "-";
101                                 aaaaP = 1900 + dataP.getYear();
102                                 // stop loading any current audio/video
103                                 if (sch.firstChild && sch.firstChild.src) {
104                                         console.log('stopping current A/V');
105                                         sch.firstChild.src = '';
106                                 }
107                                 sch.innerHTML = "<video width='258' height='195' controls autoplay>" +
108                                         srctag(h264, 'video', 'h264') +
109                                         srctag(androidUrl, 'video') +
110                                         srctag(video, 'video') +
111                                         "<h3>Sorry, no supported video format found :-(</h3></video>";
112                                 setNielsen(location.href + '&video=' + liveTg + '&data=' + ggP + mmP + aaaaP + '', true);
113                                 console.log('playTg', liveTg, video, h264, androidUrl)
114                         }
115                         console.log('playTg hacked');
116                 }
117                 if (window.playAudio) {
118                         window.playAudio = function (grrEdizione, mediaUrl, mediatype) {
119                                 var dataP = new Date();
120                                 var ggP, mmP, aaaaP;
121                                 ggP = dataP.getDate() + "-";
122                                 mmP = dataP.getMonth() + 1 + "-";
123                                 aaaaP = 1900 + dataP.getYear();
124                                 // stop loading any current audio/video
125                                 if (sch.firstChild && sch.firstChild.src) {
126                                         console.log('stopping current A/V');
127                                         sch.firstChild.src = '';
128                                 }
129                                 sch.innerHTML = "<audio width='258' height='35' controls autoplay>" +
130                                         srctag(mediaUrl, 'audio') +
131                                 "<h3>Sorry, no supported audio format found :-(</h3></audio>";
132                                 setNielsen(location.href + '&audio=' + grrEdizione + '&data=' + ggP + mmP + aaaaP + '', true);
133                                 console.log('playAudio', grrEdizione, mediaUrl, mediatype);
134                         }
135                         console.log('playAudio hacked');
136                 }
137         }
138 })();