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