From 3d5b6fbd5c37bf64cda349f88949a50e2665302c Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Tue, 2 Aug 2011 16:35:14 +0200 Subject: [PATCH] Initial import Signed-off-by: Antonio Ospite --- direct_download_links.js | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 direct_download_links.js diff --git a/direct_download_links.js b/direct_download_links.js new file mode 100644 index 0000000..f221e68 --- /dev/null +++ b/direct_download_links.js @@ -0,0 +1,114 @@ +// direct_download_links - Add direct download links for (video) files +// version 0.1 +// 2011-08-02 +// Copyright (C) 2011 Antonio Ospite +// Released under the GPL license +// http://www.gnu.org/copyleft/gpl.html +// +// -------------------------------------------------------------------- +// +// This is a Greasemonkey user script. +// +// To install, you need Greasemonkey: https://addons.mozilla.org/en-US/firefox/addon/748 +// Then restart Firefox and revisit this script. +// Under Tools, there will be a new menu item to "Install User Script". +// Accept the default configuration and install. +// +// To uninstall, go to Tools/Manage User Scripts, +// select "Direct Download Links", and click Uninstall. +// +// -------------------------------------------------------------------- +// +// ==UserScript== +// @name Direct Download Links +// @namespace http://git.ao2.it/GM_direct_download_links.git +// @description Add direct download links for (video) files +// @include http://tv.repubblica.it/* +// @include http://trovacinema.repubblica.it/* +// @include http://www.kataweb.it/tvzap/* +// ==/UserScript== + +/* + * TODO: + * - find a way to use the same string as in the @include lines to match the + * current window.location + */ + +var supported_sites = [ + { + locationRegexp: /^http:\/\/tv\.repubblica\.it\/.*$/, + fileElem: 'boxPlayer', + fileRegexp: /'pcUrl', '(http:\/\/[^']*)'/, + linkDest: 'box_embed', + }, + { + locationRegexp: /^http:\/\/trovacinema\.repubblica\.it\/.*$/, + fileElem: 'col-center', + fileRegexp: /'flvUrl', '(http:\/\/[^']*)'/, + linkDest: 'col-center', + }, + { + locationRegexp: /^http:\/\/www\.kataweb\.it\/tvzap\/.*$/, + fileElem: 'tvzap_video', + fileRegexp: /'pcUrl', '(http:\/\/[^']*)'/, + linkDest: 'playerCont', + }, +]; + +/* Apply different rules to different sites */ +for (i = 0; i < supported_sites.length; i++) { + var site = supported_sites[i]; + + var result = window.location.href.match(site.locationRegexp); + if (result) { + var ret = direct_download_link_add(window.location.href, site.fileElem, site.fileRegexp, site.linkDest); + if (!ret) { + alert('Cannot add the link'); + } + } +} + +/** + * Add a Direct Download link on the page for the specified file + * + * @param pageURL: the URL of the page we are modifying + * @param fileElem: the emelemt containig the file URL + * @param fileRegexp: the regular expression for finding the file, the first + * subpattern is taken as the file URL + * @param linkDest: the element where to place the DIrect Download link + * + * @return: null on error, true on success + */ +function direct_download_link_add(pageURL, fileElem, fileRegexp, linkDest) { + var element = document.getElementById(fileElem); + if (!element) { + alert('DirectDL (' + pageURL + '): Cannot find the element containing the file URL.'); + return null; + } + + var content = element.textContent; + if (!content) { + alert('DirectDL (' + pageURL + '): textContent is null, cannot fild file URL.'); + } + + var fileURL = content.match(fileRegexp)[1]; + if (!fileURL) { + alert('DirectDL (' + pageURL + '): file URL not found, check the fileRegexp'); + return null; + } + + var links = document.getElementById(linkDest); + if (!links) { + alert('DirectDl (' + pageURL + '): Cannot add the direct download link.'); + return null; + } + + var download_link = document.createElement('a'); + download_link.textContent = 'Direct Download'; + download_link.setAttribute('href', fileURL); + download_link.setAttribute('style', 'display: block; font-size: large; margin-left: 30px; margin-bottom: 1em;'); + + links.insertBefore(download_link, links.firstChild); + + return true; +} -- 2.1.4