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/*
31 // @include http://soundcloud.com/*
37 * - find a way to use the same string as in the @include lines to match the
38 * current window.location. Look for something like GM_testUrl() which builds
39 * the regexp starting from a glob line.
40 * - use jquery, like shown in http://a32.me/2009/11/greasemonkey/
41 * - Support the "download" attribute for anchors:
42 * http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#downloading-resources
45 /* Fields supported by the "site" object.
48 * locationRegExp: the regexp describing the URL of the page we are modifying
49 * urlContainerXPath: the XPath of 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 * linkDestXPath: the XPath of 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 urlContainerXPath: '//div[@id="contA"]',
81 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
82 linkDestXPath: '//div[@id="contA"]',
85 locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
86 urlContainerXPath: '//div[@id="boxPlayer"]',
87 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
88 linkDest: 'box_embed',
89 linkDestXPath: '//div[@id="box_embed"]',
92 locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
93 urlContainerXPath: '//div[@id="col-center"]',
94 urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
95 linkDestXPath: '//div[@id="col-center"]',
98 locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
99 urlContainerXPath: '//div[@id="tvzap_video"]',
100 urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
101 linkDestXPath: '//div[@id="playerCont"]',
104 locationRegexp: /^http:\/\/www\.rai\.tv\/.*$/,
105 initCommand: function(site) {
106 unsafeWindow.Silverlight.isInstalled = function(version) {
110 urlContainerXPath: '//div[@id="Player"]',
111 urlRegexp: /mediaUri=(http:\/\/[^,]*)/,
112 onEvent: { evt: 'DOMNodeInserted', targetElement: 'object' },
113 processURL: _rai_get_actual_url,
114 linkDestXPath: '//div[@id="Player"]',
117 locationRegexp: /^http:\/\/soundcloud.com\/.*$/,
118 urlContainerXPath: '//div[@id="main-content-inner"]',
119 urlRegexp: /"streamUrl":"([^"]*)"/,
120 linkDestXPath: '//div[@id="main-content-inner"]',
124 /* Apply different rules to different sites */
125 for (i = 0; i < supported_sites.length; i++) {
126 var site = supported_sites[i];
128 var result = window.location.href.match(site.locationRegexp);
130 if (site.initCommand) {
131 site.initCommand(site);
133 direct_download_link_add(window.location.href, site);
137 function getElementByXPath(query, root) {
138 return document.evaluate(query, root || document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
142 * Add a Direct Download link on the page for the specified URL
144 * @param: a 'site' object described above.
146 * @return: null on error, true on success
148 function direct_download_link_add(pageURL, site) {
149 var element = getElementByXPath(site.urlContainerXPath);
151 DDL_log('DirectDL (' + site.pageURL + '): Cannot find the element ' + site.urlContainerXPath + ' containing the URL.');
155 document.addEventListener('UrlFetched', _add_link, true);
157 // This is used for sites adding the URL to the DOM after DOMContentLoaded,
158 // for example by some javascript library (like Silverlight.js on rai.tv).
160 element.addEventListener(site.onEvent.evt, function(e) {
161 if (site.onEvent.targetElement &&
162 e.target.tagName.toLowerCase() != site.onEvent.targetElement) {
163 DDL_log('DirectDL (' + site.pageURL + '): skipping element ' + e.target.tagName);
166 _get_URL(site, element);
171 _get_URL(site, element);
174 function _get_URL(site, element) {
175 var content = element.innerHTML;
177 DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
181 var matches = content.match(site.urlRegexp);
182 if (!matches || matches.length < 2 || !matches[1]) {
183 DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
186 var URL = matches[1];
188 DDL_log('DirectDL (' + site.pageURL + '): cannot get the URL.');
192 if (site.processURL) {
193 site.processURL(site, URL);
197 var evt = document.createEvent('Event');
198 evt.initEvent('UrlFetched', true, true);
201 document.dispatchEvent(evt);
204 function _add_link(e) {
208 var destination = getElementByXPath(site.linkDestXPath);
210 DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
214 // Check if we added the link already, if so just update the href attribute.
215 // This is useful when _get_URL() is called on async events.
216 var download_link = document.getElementById('GM_direct_downaload_link');
218 download_link.setAttribute('href', URL);
220 download_link = document.createElement('a');
221 download_link.textContent = 'Direct Link';
222 download_link.setAttribute('id', 'GM_direct_downaload_link');
223 download_link.setAttribute('href', URL);
224 var style = 'background-color: white; color: blue;';
225 style += ' border: 2px solid red;'
226 style += ' float: right; font-size: large;';
227 style += ' padding: .5em; margin: 1em;'
228 style += ' position: relative; z-index: 1000;'
229 download_link.setAttribute('style', style);
231 destination.insertBefore(download_link, destination.firstChild);
235 function DDL_log(message) {
244 function _rai_get_actual_url(site, URL) {
246 // SmoothStreaming manifest files get added without processing, for now:
247 if (URL.match(/.*\.csm$/)) {
248 var evt = document.createEvent('Event');
249 evt.initEvent('UrlFetched', true, true);
252 document.dispatchEvent(evt);
256 // http://www.neaveru.com/wordpress/index.php/2008/05/09/greasemonkey-bug-domnodeinserted-event-doesnt-allow-gm_xmlhttprequest/
257 setTimeout( function() {
260 // XXX A custom header. This is the "clever" trick Rai uses to ensure
261 // the content is accessed by www.rai.tv only...
262 headers: {'viaurl': 'www.rai.tv'},
264 onload: function(response) {
265 text = response.responseText;
266 text = text.replace(/&/g, '&')
267 parser = new DOMParser();
268 xmlDoc = parser.parseFromString(text, "text/xml");
270 ref = xmlDoc.getElementsByTagName('REF');
271 if (ref.length > 0) {
272 href = ref[0].getAttribute('HREF');;
274 var evt = document.createEvent('Event');
275 evt.initEvent('UrlFetched', true, true);
278 document.dispatchEvent(evt);