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