Fix a couple of error messages
[GM_direct_download_links.git] / direct_download_links.user.js
1 // direct_download_links - Add direct download links for (video) files
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 for (video) files
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 var supported_sites = [
41   {
42     locationRegexp: /^http:\/\/video\.repubblica\.it\/.*$/,
43     fileElem: 'contA',
44     fileRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
45     linkDest: 'contA',
46   },
47   {
48     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
49     fileElem: 'boxPlayer',
50     fileRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
51     linkDest: 'box_embed',
52   },
53   {
54     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
55     fileElem: 'col-center',
56     fileRegexp: /'flvUrl', '((http|mms):\/\/[^']*)'/,
57     linkDest: 'col-center',
58   },
59   {
60     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
61     fileElem: 'tvzap_video',
62     fileRegexp: /'pcUrl', '((http|mms):\/\/[^']*)'/,
63     linkDest: 'playerCont',
64   },
65 ];
66
67 /* Apply different rules to different sites */
68 for (i = 0; i < supported_sites.length; i++) {
69   var site = supported_sites[i];
70
71   var result = window.location.href.match(site.locationRegexp);
72   if (result) {
73     var ret = direct_download_link_add(window.location.href, site.fileElem, site.fileRegexp, site.linkDest);
74     if (!ret) {
75       alert('Cannot add the link');
76     }
77   }
78 }
79
80 /**
81  * Add a Direct Download link on the page for the specified file
82  *
83  * @param pageURL: the URL of the page we are modifying
84  * @param fileElem: the element containing the file URL
85  * @param fileRegexp: the regular expression for finding the file, the first
86  *                    sub-pattern is taken as the file URL
87  * @param linkDest: the element where to place the Direct Download link
88  *
89  * @return: null on error, true on success
90  */
91 function direct_download_link_add(pageURL, fileElem, fileRegexp, linkDest) {
92   var element = document.getElementById(fileElem);
93   if (!element) {
94     alert('DirectDL (' + pageURL  + '): Cannot find the element ' + fileElem + ' containing the file URL.');
95     return null;
96   }
97
98   var content = element.textContent;
99   if (!content) {
100     alert('DirectDL (' + pageURL + '): textContent is null, cannot find file URL.');
101   }
102
103   var fileURL = content.match(fileRegexp)[1];
104   if (!fileURL) {
105       alert('DirectDL (' + pageURL + '): file URL not found, check the fileRegexp');
106       return null;
107   }
108
109   var links = document.getElementById(linkDest);
110   if (!links) {
111     alert('DirectDl (' + pageURL + '): Cannot add the direct download link.');
112     return null;
113   }
114
115   var download_link = document.createElement('a');
116   download_link.textContent = 'Direct Link';
117   download_link.setAttribute('href', fileURL);
118   var style = 'background-color: white; color: blue;';
119   style += ' border: 2px solid red;'
120   style += ' float: right; font-size: large;';
121   style += ' padding: .5em; margin: 1em;'
122   download_link.setAttribute('style', style);
123
124   links.insertBefore(download_link, links.firstChild);
125
126   return true;
127 }