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
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:
60 * evt: the event we want to wait for (e.g. 'DOMNodeInserted')
62 * targetElement: the element in the event handler we want the
63 * urlRegexp is performed on.
65 * processURL: a function to process the URL before adding the Direct
66 * Downdload Link to the page, it must accept a 'site' and a
67 * 'URL' parameters and dispatch the UrlFetched to pass the
68 * modified URL to _add_link().
71 var supported_sites = [
73 locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
74 urlContainer: 'contA',
75 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
79 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
80 urlContainer: 'boxPlayer',
81 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
82 linkDest: 'box_embed',
85 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
86 urlContainer: 'col-center',
87 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
88 linkDest: 'col-center',
91 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
92 urlContainer: 'tvzap_video',
93 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
94 linkDest: 'playerCont',
97 locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
98 urlContainer: 'Player',
99 urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
100 onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
101 processURL: _rai_get_actual_url,
106 /* Apply different rules to different sites */
107 for (i = 0; i < supported_sites.length; i++) {
108 var site = supported_sites[i];
110 var result = window.location.href.match(site.locationRegexp);
112 direct_download_link_add(window.location.href, site);
117 * Add a Direct Download link on the page for the specified URL
119 * @param: a 'site' object described above.
121 * @return: null on error, true on success
123 function direct_download_link_add(pageURL, site) {
124 var element = document.getElementById(site.urlContainer);
126 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
130 document.addEventListener('UrlFetched', _add_link, true);
132 // This is used for sites adding the URL to the DOM after DOMContentLoaded,
133 // for example by some javascript library (like Silverlight.js on rai.tv).
135 element.addEventListener(site.onEvent.evt, function(e) {
136 if (site.onEvent.targetElement &&
137 e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
138 DDL_log('DirectDL (' + site.pageURL + '): skipping element ' + e.target.tagName);
141 _get_URL(site, element);
145 _get_URL(site, element);
148 function _get_URL(site, element) {
149 var content = element.innerHTML;
151 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
155 var matches = content.match(site.urlRegexp);
156 if (!matches || matches.length < 2 || !matches[1]) {
157 DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
160 var URL = matches[1];
162 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
166 if (site.processURL) {
167 site.processURL(site, URL);
171 var evt = document.createEvent('Event');
172 evt.initEvent('UrlFetched', true, true);
175 document.dispatchEvent(evt);
178 function _add_link(e) {
182 var destination = document.getElementById(site.linkDest);
184 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
188 var download_link = document.createElement('a');
189 download_link.textContent = 'Direct Link';
190 download_link.setAttribute('href', URL);
191 var style = 'background-color: white; color: blue;';
192 style += ' border: 2px solid red;'
193 style += ' float: right; font-size: large;';
194 style += ' padding: .5em; margin: 1em;'
195 download_link.setAttribute('style', style);
197 destination.insertBefore(download_link, destination.firstChild);
200 function DDL_log(message) {
209 function _rai_get_actual_url(site, URL) {
210 // http://www.neaveru.com/wordpress/index.php/2008/05/09/greasemonkey-bug-domnodeinserted-event-doesnt-allow-gm_xmlhttprequest/
211 setTimeout( function() {
214 // XXX A custom header. This is the "clever" trick Rai uses to ensure
215 // the content is accessed by www.rai.tv only...
216 headers: {'viaurl': 'www.rai.tv'},
218 onload: function(response) {
219 text = response.responseText;
220 text = text.replace(/&/g, '&')
221 parser = new DOMParser();
222 xmlDoc = parser.parseFromString(text, "text/xml");
224 ref = xmlDoc.getElementsByTagName('REF');
225 if (ref.length > 0) {
226 href = ref[0].getAttribute('HREF');;
228 var evt = document.createEvent('Event');
229 evt.initEvent('UrlFetched', true, true);
232 document.dispatchEvent(evt);