Add an initCommand hook
[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. Look for something like GM_testUrl() which builds
40  *    the regexp starting from a glob line.
41  *  - use xpath instead of regexp like in http://a32.me/2009/11/greasemonkey/
42  *  - use jquery, like shown in http://a32.me/2009/11/greasemonkey/
43  */
44
45 /* Fields supported by the "site" object.
46  *
47  * Manadatory fields:
48  *   pageURL: the URL of the page we are modifying
49  *   urlContainer: the element containing the URL to link
50  *   urlRegexp: the regular expression for finding the URL, the first
51  *              sub-pattern is taken as the URL
52  *   linkDest: the element where to place the Direct Download link
53  *
54  *
55  * Optional fields:
56  *
57  *   initCommand: a function called before the regExp is matched, this can
58  *                be useful in cases when some action needs to be done in
59  *                order to make the element containing the regExp be actually
60  *                rendered. It must accept  a 'site' parameter.
61  *
62  *   onEvent: used to delay the urlRegexp matching to a certain event like
63  *            'DOMNodeInserted' useful when the URL is added by some javascript
64  *            library. It has two fields:
65  *
66  *              evt: the event we want to wait for (e.g. 'DOMNodeInserted')
67  *
68  *              targetElement: the element in the event handler we want the
69  *                urlRegexp is performed on.
70  *
71  *  processURL: a function to process the URL before adding the Direct
72  *              Downdload Link to the page, it must accept  a 'site' and a
73  *              'URL' parameters and dispatch the UrlFetched to pass the
74  *              modified URL to _add_link().
75  *
76  */
77 var supported_sites = [
78   {
79     locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
80     urlContainer: 'contA',
81     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
82     linkDest: 'contA',
83   },
84   {
85     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
86     urlContainer: 'boxPlayer',
87     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
88     linkDest: 'box_embed',
89   },
90   {
91     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
92     urlContainer: 'col-center',
93     urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
94     linkDest: 'col-center',
95   },
96   {
97     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
98     urlContainer: 'tvzap_video',
99     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
100     linkDest: 'playerCont',
101   },
102   {
103     locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
104     urlContainer: 'Player',
105     urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
106     onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
107     processURL: _rai_get_actual_url,
108     linkDest: 'Player',
109   },
110 ];
111
112 /* Apply different rules to different sites */
113 for (i = 0; i < supported_sites.length; i++) {
114   var site = supported_sites[i];
115
116   var result = window.location.href.match(site.locationRegexp);
117   if (result) {
118     if (site.initCommand) {
119       site.initCommand(site);
120     }
121     direct_download_link_add(window.location.href, site);
122   }
123 }
124
125 /**
126  * Add a Direct Download link on the page for the specified URL
127  *
128  * @param: a 'site' object described above.
129  *
130  * @return: null on error, true on success
131  */
132 function direct_download_link_add(pageURL, site) {
133   var element = document.getElementById(site.urlContainer);
134   if (!element) {
135     DDL_log('DirectDL (' + site.pageURL  + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
136     return null;
137   }
138
139   document.addEventListener('UrlFetched', _add_link, true);
140
141   // This is used for sites adding the URL to the DOM after DOMContentLoaded,
142   // for example by some javascript library (like Silverlight.js on rai.tv).
143   if (site.onEvent) {
144     element.addEventListener(site.onEvent.evt, function(e) {
145       if (site.onEvent.targetElement &&
146           e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
147         DDL_log('DirectDL (' + site.pageURL  + '): skipping element ' + e.target.tagName);
148         return;
149       }
150      _get_URL(site, element);
151     }, false);
152     return;
153   }
154
155   _get_URL(site, element);
156 }
157
158 function _get_URL(site, element) {
159   var content = element.innerHTML;
160   if (!content) {
161     DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
162     return;
163   }
164
165   var matches = content.match(site.urlRegexp);
166   if (!matches || matches.length < 2 || !matches[1]) {
167       DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
168       return;
169   }
170   var URL = matches[1];
171   if (!URL) {
172     DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
173     return;
174   }
175
176   if (site.processURL) {
177     site.processURL(site, URL);
178     return;
179   }
180
181   var evt = document.createEvent('Event');  
182   evt.initEvent('UrlFetched', true, true);  
183   evt.site = site;
184   evt.URL = URL;
185   document.dispatchEvent(evt);
186 }
187
188 function _add_link(e) {
189   var site = e.site;
190   var URL = e.URL;;
191
192   var destination = document.getElementById(site.linkDest);
193   if (!destination) {
194     DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
195     return;
196   }
197
198   // Check if we added the link already, if so just update the href attribute.
199   // This is useful when _get_URL() is called on async events.
200   var download_link = document.getElementById('GM_direct_downaload_link');
201   if (download_link) {
202     download_link.setAttribute('href', URL);
203   } else {
204     download_link = document.createElement('a');
205     download_link.textContent = 'Direct Link';
206     download_link.setAttribute('id', 'GM_direct_downaload_link');
207     download_link.setAttribute('href', URL);
208     var style = 'background-color: white; color: blue;';
209     style += ' border: 2px solid red;'
210     style += ' float: right; font-size: large;';
211     style += ' padding: .5em; margin: 1em;'
212     download_link.setAttribute('style', style);
213
214     destination.insertBefore(download_link, destination.firstChild);
215   }
216 }
217
218 function DDL_log(message) {
219   var debug = false;
220   if (debug) {
221     alert(message)
222   } else {
223     GM_log(message);
224   }
225 }
226
227 function _rai_get_actual_url(site, URL) {
228   // http://www.neaveru.com/wordpress/index.php/2008/05/09/greasemonkey-bug-domnodeinserted-event-doesnt-allow-gm_xmlhttprequest/
229   setTimeout( function() {
230     GM_xmlhttpRequest({
231       method: "GET",
232       // XXX A custom header. This is the "clever" trick Rai uses to ensure
233       // the content is accessed by www.rai.tv only...
234       headers: {'viaurl': 'www.rai.tv'},
235       url: URL,
236       onload: function(response) {
237         text = response.responseText;
238         text = text.replace(/&/g, '&amp;')
239         parser = new DOMParser();
240         xmlDoc = parser.parseFromString(text, "text/xml");
241
242         ref = xmlDoc.getElementsByTagName('REF');
243         if (ref.length > 0) {
244           href = ref[0].getAttribute('HREF');;
245
246           var evt = document.createEvent('Event');  
247           evt.initEvent('UrlFetched', true, true);  
248           evt.site = site;
249           evt.URL = href;
250           document.dispatchEvent(evt);
251         }
252       }
253     });
254   }, 0);
255 }