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/*
34 * - find a way to use the same string as in the @include lines to match the
35 * current window.location
36 * - use xpath instead of regexp like in http://a32.me/2009/11/greasemonkey/
37 * - use jquery, like shown in http://a32.me/2009/11/greasemonkey/
40 /* Fields supported by the "site" object.
43 * pageURL: the URL of the page we are modifying
44 * urlContainer: the element containing the URL to link
45 * urlRegexp: the regular expression for finding the URL, the first
46 * sub-pattern is taken as the URL
47 * linkDest: the element where to place the Direct Download link
54 var supported_sites = [
56 locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
57 urlContainer: 'contA',
58 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
62 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
63 urlContainer: 'boxPlayer',
64 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
65 linkDest: 'box_embed',
68 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
69 urlContainer: 'col-center',
70 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
71 linkDest: 'col-center',
74 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
75 urlContainer: 'tvzap_video',
76 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
77 linkDest: 'playerCont',
81 /* Apply different rules to different sites */
82 for (i = 0; i < supported_sites.length; i++) {
83 var site = supported_sites[i];
85 var result = window.location.href.match(site.locationRegexp);
87 var ret = direct_download_link_add(window.location.href, site);
89 DDL_log('Cannot add the link');
95 * Add a Direct Download link on the page for the specified URL
97 * @param: a 'site' object described above.
99 * @return: null on error, true on success
101 function direct_download_link_add(pageURL, site) {
102 var element = document.getElementById(site.urlContainer);
104 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
108 var URL = _get_URL(site, element);
110 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
114 return _add_link(site, URL);
117 function _get_URL(site, element) {
118 var content = element.innerHTML;
120 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
124 var matches = content.match(site.urlRegexp);
125 if (!matches || matches.length < 2 || !matches[1]) {
126 DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
129 var URL = matches[1];
134 function _add_link(site, URL) {
136 var links = document.getElementById(site.linkDest);
138 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
142 var download_link = document.createElement('a');
143 download_link.textContent = 'Direct Link';
144 download_link.setAttribute('href', URL);
145 var style = 'background-color: white; color: blue;';
146 style += ' border: 2px solid red;'
147 style += ' float: right; font-size: large;';
148 style += ' padding: .5em; margin: 1em;'
149 download_link.setAttribute('style', style);
151 links.insertBefore(download_link, links.firstChild);
156 function DDL_log(message) {