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