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.
66 var supported_sites = [
68 locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
69 urlContainer: 'contA',
70 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
74 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
75 urlContainer: 'boxPlayer',
76 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
77 linkDest: 'box_embed',
80 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
81 urlContainer: 'col-center',
82 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
83 linkDest: 'col-center',
86 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
87 urlContainer: 'tvzap_video',
88 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
89 linkDest: 'playerCont',
92 locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
93 urlContainer: 'Player',
94 urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
95 onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
100 /* Apply different rules to different sites */
101 for (i = 0; i < supported_sites.length; i++) {
102 var site = supported_sites[i];
104 var result = window.location.href.match(site.locationRegexp);
106 var ret = direct_download_link_add(window.location.href, site);
108 DDL_log('Cannot add the link');
114 * Add a Direct Download link on the page for the specified URL
116 * @param: a 'site' object described above.
118 * @return: null on error, true on success
120 function direct_download_link_add(pageURL, site) {
121 var element = document.getElementById(site.urlContainer);
123 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
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).
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);
136 var URL = _get_URL(site, element);
137 return _add_link(site, URL);
142 var URL = _get_URL(site, element);
144 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
148 return _add_link(site, URL);
151 function _get_URL(site, element) {
152 var content = element.innerHTML;
154 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
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');
163 var URL = matches[1];
168 function _add_link(site, URL) {
170 var links = document.getElementById(site.linkDest);
172 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
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);
185 links.insertBefore(download_link, links.firstChild);
190 function DDL_log(message) {