Fix spelling errors
[GM_direct_download_links.git] / direct_download_links.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://tv.repubblica.it/*
27 // @include        http://trovacinema.repubblica.it/*
28 // @include        http://www.kataweb.it/tvzap/*
29 // ==/UserScript==
30
31 /*
32  * TODO:
33  *  - find a way to use the same string as in the @include lines to match the
34  *    current window.location
35  */
36
37 var supported_sites = [
38   {
39     locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/,
40     fileElem: 'boxPlayer',
41     fileRegexp: /'pcUrl', '(http:\/\/[^']*)'/,
42     linkDest: 'box_embed',
43   },
44   {
45     locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/,
46     fileElem: 'col-center',
47     fileRegexp: /'flvUrl', '(http:\/\/[^']*)'/,
48     linkDest: 'col-center',
49   },
50   {
51     locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/,
52     fileElem: 'tvzap_video',
53     fileRegexp: /'pcUrl', '(http:\/\/[^']*)'/,
54     linkDest: 'playerCont',
55   },
56 ];
57
58 /* Apply different rules to different sites */
59 for (i = 0; i < supported_sites.length; i++) {
60   var site = supported_sites[i];
61
62   var result = window.location.href.match(site.locationRegexp);
63   if (result) {
64     var ret = direct_download_link_add(window.location.href, site.fileElem, site.fileRegexp, site.linkDest);
65     if (!ret) {
66       alert('Cannot add the link');
67     }
68   }
69 }
70
71 /**
72  * Add a Direct Download link on the page for the specified file
73  *
74  * @param pageURL: the URL of the page we are modifying
75  * @param fileElem: the element containing the file URL
76  * @param fileRegexp: the regular expression for finding the file, the first
77  *                    sub-pattern is taken as the file URL
78  * @param linkDest: the element where to place the Direct Download link
79  *
80  * @return: null on error, true on success
81  */
82 function direct_download_link_add(pageURL, fileElem, fileRegexp, linkDest) {
83   var element = document.getElementById(fileElem);
84   if (!element) {
85     alert('DirectDL (' + pageURL  + '): Cannot find the element containing the file URL.');
86     return null;
87   }
88
89   var content = element.textContent;
90   if (!content) {
91     alert('DirectDL (' + pageURL + '): textContent is null, cannot fild file URL.');
92   }
93
94   var fileURL = content.match(fileRegexp)[1];
95   if (!fileURL) {
96       alert('DirectDL (' + pageURL + '): file URL not found, check the fileRegexp');
97       return null;
98   }
99
100   var links = document.getElementById(linkDest);
101   if (!links) {
102     alert('DirectDl (' + pageURL + '): Cannot add the direct download link.');
103     return null;
104   }
105
106   var download_link = document.createElement('a');
107   download_link.textContent = 'Direct Download';
108   download_link.setAttribute('href', fileURL);
109   download_link.setAttribute('style', 'display: block; font-size: large; margin-left: 30px; margin-bottom: 1em;');
110
111   links.insertBefore(download_link, links.firstChild);
112
113   return true;
114 }