Add a DDL_log() functions
[GM_direct_download_links.git] / direct_download_links.user.js
1 // direct_download_links - Add direct download links
2 // version 0.2
3 // 2011-11-14
4 // Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
5 // Released under the GPL license
6 // http://www.gnu.org/copyleft/gpl.html
7 //
8 // --------------------------------------------------------------------
9 //
10 // This is a Greasemonkey user script.
11 //
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.
16 //
17 // To uninstall, go to Tools/Manage User Scripts,
18 // select "Direct Download Links", and click Uninstall.
19 //
20 // --------------------------------------------------------------------
21 //
22 // ==UserScript==
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 // ==/UserScript==
31
32 /*
33  * TODO:
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/
38  */
39
40 /* Fields supported by the "site" object.
41  *
42  * Manadatory fields:
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
48  *
49  *
50  * Optional fields:
51  *   TODO
52  *
53  */
54 var supported_sites = [
55   {
56     locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
57     urlContainer: 'contA',
58     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
59     linkDest: 'contA',
60   },
61   {
62     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
63     urlContainer: 'boxPlayer',
64     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
65     linkDest: 'box_embed',
66   },
67   {
68     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
69     urlContainer: 'col-center',
70     urlRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
71     linkDest: 'col-center',
72   },
73   {
74     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
75     urlContainer: 'tvzap_video',
76     urlRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
77     linkDest: 'playerCont',
78   },
79 ];
80
81 /* Apply different rules to different sites */
82 for (i = 0; i < supported_sites.length; i++) {
83   var site = supported_sites[i];
84
85   var result = window.location.href.match(site.locationRegexp);
86   if (result) {
87     var ret = direct_download_link_add(window.location.href, site);
88     if (!ret) {
89       DDL_log('Cannot add the link');
90     }
91   }
92 }
93
94 /**
95  * Add a Direct Download link on the page for the specified URL
96  *
97  * @param: a 'site' object described above.
98  *
99  * @return: null on error, true on success
100  */
101 function direct_download_link_add(pageURL, site) {
102   var element = document.getElementById(site.urlContainer);
103   if (!element) {
104     DDL_log('DirectDL (' + site.pageURL  + '): Cannot find the element ' + site.urlContainer + ' containing the URL.');
105     return null;
106   }
107
108   var content = element.innerHTML;
109   if (!content) {
110     DDL_log('DirectDL (' + site.pageURL + '): content is null, cannot find URL.');
111     return null;
112   }
113
114   var matches = content.match(site.urlRegexp);
115   if (!matches || matches.length < 2 || !matches[1]) {
116       DDL_log('DirectDL (' + site.pageURL + '): URL not found, check the urlRegexp');
117       return null;
118   }
119   var URL = matches[1];
120
121   var links = document.getElementById(site.linkDest);
122   if (!links) {
123     DDL_log('DirectDl (' + site.pageURL + '): Cannot add the direct download link.');
124     return null;
125   }
126
127   var download_link = document.createElement('a');
128   download_link.textContent = 'Direct Link';
129   download_link.setAttribute('href', URL);
130   var style = 'background-color: white; color: blue;';
131   style += ' border: 2px solid red;'
132   style += ' float: right; font-size: large;';
133   style += ' padding: .5em; margin: 1em;'
134   download_link.setAttribute('style', style);
135
136   links.insertBefore(download_link, links.firstChild);
137
138   return true;
139 }
140
141 function DDL_log(message) {
142   var debug = false;
143   if (debug) {
144     alert(message)
145   } else {
146     GM_log(message);
147   }
148 }