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
40 * - use xpath instead of regexp like in http://a32.me/2009/11/greasemonkey/
41 * - use jquery, like shown in http://a32.me/2009/11/greasemonkey/
44 /* Fields supported by the "site" object.
47 * pageURL: the URL of the page we are modifying
48 * urlContainer: the element containing the URL to link
49 * urlRegexp: the regular expression for finding the URL, the first
50 * sub-pattern is taken as the URL
51 * linkDest: the element where to place the Direct Download link
55 * onEvent: used to delay the urlRegexp matching to a certain event like
56 * 'DOMNodeInserted' useful when the URL is added by some javascript
57 * library. It has two fields:
59 * evt: the event we want to wait for (e.g. 'DOMNodeInserted')
61 * targetElement: the element in the event handler we want the
62 * urlRegexp is performed on.
65 var supported_sites = [
67 locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
68 urlContainer: 'contA',
69 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
73 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
74 urlContainer: 'boxPlayer',
75 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
76 linkDest: 'box_embed',
79 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
80 urlContainer: 'col-center',
81 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
82 linkDest: 'col-center',
85 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
86 urlContainer: 'tvzap_video',
87 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
88 linkDest: 'playerCont',
91 locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
92 urlContainer: 'Player',
93 urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
94 onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
99 /* Apply different rules to different sites */
100 for (i = 0; i < supported_sites.length; i++) {
101 var site = supported_sites[i];
103 var result = window.location.href.match(site.locationRegexp);
105 var ret = direct_download_link_add(window.location.href, site);
107 DDL_log('Cannot add the link');
113 * Add a Direct Download link on the page for the specified URL
115 * @param: a 'site' object described above.
117 * @return: null on error, true on success
119 function direct_download_link_add(pageURL, site) {
120 var element = document.getElementById(site.urlContainer);
122 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
128 element.addEventListener(site.onEvent.evt, function(e) {
129 if (site.onEvent.targetElement &&
130 e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
131 DDL_log('DirectDL (' + site.pageURL + '): skipping element ' + e.target.tagName);
134 var URL = _get_URL(site, element);
135 return _add_link(site, URL);
140 var URL = _get_URL(site, element);
142 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
146 return _add_link(site, URL);
149 function _get_URL(site, element) {
150 var content = element.innerHTML;
152 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
156 var matches = content.match(site.urlRegexp);
157 if (!matches || matches.length < 2 || !matches[1]) {
158 DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
161 var URL = matches[1];
166 function _add_link(site, URL) {
168 var links = document.getElementById(site.linkDest);
170 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
174 var download_link = document.createElement('a');
175 download_link.textContent = 'Direct Link';
176 download_link.setAttribute('href', URL);
177 var style = 'background-color: white; color: blue;';
178 style += ' border: 2px solid red;'
179 style += ' float: right; font-size: large;';
180 style += ' padding: .5em; margin: 1em;'
181 download_link.setAttribute('style', style);
183 links.insertBefore(download_link, links.firstChild);
188 function DDL_log(message) {