Add initial support for rai.tv
[GM_direct_download_links.git] / direct_download_links.user.js
1 // direct_download_links - Add direct download links
2 // version 0.2
3 // 2011-11-14
4 // Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
5 // Released under the GPL license
6 // http://www.gnu.org/copyleft/gpl.html
7 //
8 // --------------------------------------------------------------------
9 //
10 // This is a Greasemonkey user script.
11 //
12 // To install, you need Greasemonkey: https://addons.mozilla.org/en-US/firefox/addon/748
13 // Then restart Firefox and revisit this script.
14 // Under Tools, there will be a new menu item to "Install User Script".
15 // Accept the default configuration and install.
16 //
17 // To uninstall, go to Tools/Manage User Scripts,
18 // select "Direct Download Links", and click Uninstall.
19 //
20 // --------------------------------------------------------------------
21 //
22 // ==UserScript==
23 // @name           Direct Download Links
24 // @namespace      http://git.ao2.it/GM_direct_download_links.git
25 // @description    Add direct download links
26 // @include        http://video.repubblica.it/*
27 // @include        http://tv.repubblica.it/*
28 // @include        http://trovacinema.repubblica.it/*
29 // @include        http://www.kataweb.it/tvzap/*
30 // @include        http://www.rai.tv/*
31 // ==/UserScript==
32 //
33 // NOTE, for rai.tv to work you need to install a script like:
34 // http://git.ao2.it/smooth-dl.git/blob_plain/HEAD:/scripts/SilverSpoof.user.js
35
36 /*
37  * TODO:
38  *  - find a way to use the same string as in the @include lines to match the
39  *    current window.location
40  *  - use xpath instead of regexp like in http://a32.me/2009/11/greasemonkey/
41  *  - use jquery, like shown in http://a32.me/2009/11/greasemonkey/
42  */
43
44 /* Fields supported by the "site" object.
45  *
46  * Manadatory fields:
47  *   pageURL: the URL of the page we are modifying
48  *   urlContainer: the element containing the URL to link
49  *   urlRegexp: the regular expression for finding the URL, the first
50  *              sub-pattern is taken as the URL
51  *   linkDest: the element where to place the Direct Download link
52  *
53  *
54  * Optional fields:
55  *   onEvent: used to delay the urlRegexp matching to a certain event like
56  *            'DOMNodeInserted' useful when the URL is added by some javascript
57  *            library. It has two fields:
58  *
59  *              evt: the event we want to wait for (e.g. 'DOMNodeInserted')
60  *
61  *              targetElement: the element in the event handler we want the
62  *                urlRegexp is performed on.
63  *
64  */
65 var supported_sites = [
66   {
67     locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
68     urlContainer: 'contA',
69     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
70     linkDest: 'contA',
71   },
72   {
73     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
74     urlContainer: 'boxPlayer',
75     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
76     linkDest: 'box_embed',
77   },
78   {
79     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
80     urlContainer: 'col-center',
81     urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
82     linkDest: 'col-center',
83   },
84   {
85     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
86     urlContainer: 'tvzap_video',
87     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
88     linkDest: 'playerCont',
89   },
90   {
91     locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
92     urlContainer: 'Player',
93     urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
94     onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
95     linkDest: 'Player',
96   },
97 ];
98
99 /* Apply different rules to different sites */
100 for (i = 0; i < supported_sites.length; i++) {
101   var site = supported_sites[i];
102
103   var result = window.location.href.match(site.locationRegexp);
104   if (result) {
105     var ret = direct_download_link_add(window.location.href, site);
106     if (!ret) {
107       DDL_log('Cannot add the link');
108     }
109   }
110 }
111
112 /**
113  * Add a Direct Download link on the page for the specified URL
114  *
115  * @param: a 'site' object described above.
116  *
117  * @return: null on error, true on success
118  */
119 function direct_download_link_add(pageURL, site) {
120   var element = document.getElementById(site.urlContainer);
121   if (!element) {
122     DDL_log('DirectDL (' + site.pageURL  + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
123     return null;
124   }
125
126
127   if (site.onEvent) {
128     element.addEventListener(site.onEvent.evt, function(e) {
129       if (site.onEvent.targetElement &&
130           e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
131         DDL_log('DirectDL (' + site.pageURL  + '): skipping element ' + e.target.tagName);
132         return;
133       }
134     var URL = _get_URL(site, element);
135     return _add_link(site, URL);
136     }, false);
137     return true;
138   }
139
140   var URL = _get_URL(site, element);
141   if (!URL) {
142     DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
143     return null;
144   }
145
146   return _add_link(site, URL);
147 }
148
149 function _get_URL(site, element) {
150   var content = element.innerHTML;
151   if (!content) {
152     DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
153     return null;
154   }
155
156   var matches = content.match(site.urlRegexp);
157   if (!matches || matches.length < 2 || !matches[1]) {
158       DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
159       return null;
160   }
161   var URL = matches[1];
162
163   return URL;
164 }
165
166 function _add_link(site, URL) {
167
168   var links = document.getElementById(site.linkDest);
169   if (!links) {
170     DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
171     return null;
172   }
173
174   var download_link = document.createElement('a');
175   download_link.textContent = 'Direct Link';
176   download_link.setAttribute('href', URL);
177   var style = 'background-color: white; color: blue;';
178   style += ' border: 2px solid red;'
179   style += ' float: right; font-size: large;';
180   style += ' padding: .5em; margin: 1em;'
181   download_link.setAttribute('style', style);
182
183   links.insertBefore(download_link, links.firstChild);
184
185   return true;
186 }
187
188 function DDL_log(message) {
189   var debug = false;
190   if (debug) {
191     alert(message)
192   } else {
193     GM_log(message);
194   }
195 }