Add some comments
[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  *   onEvent: used to delay the urlRegexp matching to a certain event like
57  *            'DOMNodeInserted' useful when the URL is added by some javascript
58  *            library. It has two fields:
59  *
60  *              evt: the event we want to wait for (e.g. 'DOMNodeInserted')
61  *
62  *              targetElement: the element in the event handler we want the
63  *                urlRegexp is performed on.
64  *
65  */
66 var supported_sites = [
67   {
68     locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
69     urlContainer: 'contA',
70     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
71     linkDest: 'contA',
72   },
73   {
74     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
75     urlContainer: 'boxPlayer',
76     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
77     linkDest: 'box_embed',
78   },
79   {
80     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
81     urlContainer: 'col-center',
82     urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
83     linkDest: 'col-center',
84   },
85   {
86     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
87     urlContainer: 'tvzap_video',
88     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
89     linkDest: 'playerCont',
90   },
91   {
92     locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
93     urlContainer: 'Player',
94     urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
95     onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
96     linkDest: 'Player',
97   },
98 ];
99
100 /* Apply different rules to different sites */
101 for (i = 0; i < supported_sites.length; i++) {
102   var site = supported_sites[i];
103
104   var result = window.location.href.match(site.locationRegexp);
105   if (result) {
106     var ret = direct_download_link_add(window.location.href, site);
107     if (!ret) {
108       DDL_log('Cannot add the link');
109     }
110   }
111 }
112
113 /**
114  * Add a Direct Download link on the page for the specified URL
115  *
116  * @param: a 'site' object described above.
117  *
118  * @return: null on error, true on success
119  */
120 function direct_download_link_add(pageURL, site) {
121   var element = document.getElementById(site.urlContainer);
122   if (!element) {
123     DDL_log('DirectDL (' + site.pageURL  + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
124     return null;
125   }
126
127   // This is used for sites adding the URL to the DOM after DOMContentLoaded,
128   // for example by some javascript library (like Silverlight.js on rai.tv).
129   if (site.onEvent) {
130     element.addEventListener(site.onEvent.evt, function(e) {
131       if (site.onEvent.targetElement &&
132           e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
133         DDL_log('DirectDL (' + site.pageURL  + '): skipping element ' + e.target.tagName);
134         return;
135       }
136     var URL = _get_URL(site, element);
137     return _add_link(site, URL);
138     }, false);
139     return true;
140   }
141
142   var URL = _get_URL(site, element);
143   if (!URL) {
144     DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
145     return null;
146   }
147
148   return _add_link(site, URL);
149 }
150
151 function _get_URL(site, element) {
152   var content = element.innerHTML;
153   if (!content) {
154     DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
155     return null;
156   }
157
158   var matches = content.match(site.urlRegexp);
159   if (!matches || matches.length < 2 || !matches[1]) {
160       DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
161       return null;
162   }
163   var URL = matches[1];
164
165   return URL;
166 }
167
168 function _add_link(site, URL) {
169
170   var links = document.getElementById(site.linkDest);
171   if (!links) {
172     DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
173     return null;
174   }
175
176   var download_link = document.createElement('a');
177   download_link.textContent = 'Direct Link';
178   download_link.setAttribute('href', URL);
179   var style = 'background-color: white; color: blue;';
180   style += ' border: 2px solid red;'
181   style += ' float: right; font-size: large;';
182   style += ' padding: .5em; margin: 1em;'
183   download_link.setAttribute('style', style);
184
185   links.insertBefore(download_link, links.firstChild);
186
187   return true;
188 }
189
190 function DDL_log(message) {
191   var debug = false;
192   if (debug) {
193     alert(message)
194   } else {
195     GM_log(message);
196   }
197 }