1 // direct_download_links - Add direct download links
4 // Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
5 // Released under the GPL license
6 // http://www.gnu.org/copyleft/gpl.html
8 // --------------------------------------------------------------------
10 // This is a Greasemonkey user script.
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.
17 // To uninstall, go to Tools/Manage User Scripts,
18 // select "Direct Download Links", and click Uninstall.
20 // --------------------------------------------------------------------
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/*
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
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/
45 /* Fields supported by the "site" object.
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
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.
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:
66 * evt: the event we want to wait for (e.g. 'DOMNodeInserted')
68 * targetElement: the element in the event handler we want the
69 * urlRegexp is performed on.
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().
77 var supported_sites = [
79 locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
80 urlContainer: 'contA',
81 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
85 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
86 urlContainer: 'boxPlayer',
87 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
88 linkDest: 'box_embed',
91 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
92 urlContainer: 'col-center',
93 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
94 linkDest: 'col-center',
97 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
98 urlContainer: 'tvzap_video',
99 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
100 linkDest: 'playerCont',
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,
112 /* Apply different rules to different sites */
113 for (i = 0; i < supported_sites.length; i++) {
114 var site = supported_sites[i];
116 var result = window.location.href.match(site.locationRegexp);
118 if (site.initCommand) {
119 site.initCommand(site);
121 direct_download_link_add(window.location.href, site);
126 * Add a Direct Download link on the page for the specified URL
128 * @param: a 'site' object described above.
130 * @return: null on error, true on success
132 function direct_download_link_add(pageURL, site) {
133 var element = document.getElementById(site.urlContainer);
135 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
139 document.addEventListener('UrlFetched', _add_link, true);
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).
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);
150 _get_URL(site, element);
155 _get_URL(site, element);
158 function _get_URL(site, element) {
159 var content = element.innerHTML;
161 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
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');
170 var URL = matches[1];
172 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
176 if (site.processURL) {
177 site.processURL(site, URL);
181 var evt = document.createEvent('Event');
182 evt.initEvent('UrlFetched', true, true);
185 document.dispatchEvent(evt);
188 function _add_link(e) {
192 var destination = document.getElementById(site.linkDest);
194 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
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');
202 download_link.setAttribute('href', URL);
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);
214 destination.insertBefore(download_link, destination.firstChild);
218 function DDL_log(message) {
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() {
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'},
236 onload: function(response) {
237 text = response.responseText;
238 text = text.replace(/&/g, '&')
239 parser = new DOMParser();
240 xmlDoc = parser.parseFromString(text, "text/xml");
242 ref = xmlDoc.getElementsByTagName('REF');
243 if (ref.length > 0) {
244 href = ref[0].getAttribute('HREF');;
246 var evt = document.createEvent('Event');
247 evt.initEvent('UrlFetched', true, true);
250 document.dispatchEvent(evt);