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