// ==UserScript==
// @name Add Torrents To wTorrent (v1.2)
// @namespace http://www.brooksworks.com
// @description Let's you download torrents using wTorrent, an rTorrent interface, remotely from your browser.
// @include *
// @include http://isohunt.com/torrent_details/*
// @include http://thepiratebay.org/details.php?*
// @include http://torrentreactor.net/view.php?*
// @include http://www.mininova.org/*
// @include http://www.torrentspy.com/*
// @include http://ts.searching.com/*
// ==/UserScript==
/*
Add Torrents to wTorrent (v1.2)
Created 2008-10-30
Copyright Shannon Brooks (c) 2008
License GPL License (http://www.gnu.org/copyleft/gpl.html)
Let's you download torrents using wTorrent, an rTorrent interface, remotely
from your browser. Visit wTorrent Homepage (http://www.wtorrent-project.org/)
to learn more about rTorrent and wTorrent.The idea for this script and some
of its functions taken from "Add Torrents To uTorrent" by Julien Couvreur.
The original uTorrent script by Julien can be found on userscripts.org.
This script includes the ability to auto-login, though you don't need to use
that functionality. If you wish to log in manually (just open a tab with
wTorrent) you just need to set the wTorrent_OverRideCheck option in
about:config to true.
Preferences are saved in firefox. Open about:config and type wtorrent into
filter box. Variables are pretty self explanitory, but you can see what each
corresponds to below.
Change Log:
v1.0 Released 2008-10-30
Initial Release
v1.1 Released 2008-10-31
- Fix bug with matching any link with "undefined" somewhere in the body.
- Fix bug causing you to have to click twice on a link to add a torrent
if you are not logged in.
v1.2 Released 2008-12-14
- Fix bug with URLs containing ampersands not being interpreted properly
(thanks Constantine P.)
*/
(function() {
// wTorrent FQDN or IP
var host = GM_getValue("wTorrent_FQDN","wtorrent") ;
// wTorrent Port (if not 80)
var port = GM_getValue("wTorrent_Port","80");
// wTorrent Username
var username = GM_getValue("wTorrent_User","");
// wTorrent Password
var password = GM_getValue("wTorrent_Pass","");
// Download dir
var download_dir = GM_getValue("wTorrent_Download_Dir","");
// init variables in firefox about:config
if (GM_getValue("wTorrent_FQDN") == undefined) {
GM_setValue("wTorrent_FQDN","wtorrent");
GM_setValue("wTorrent_Port","80");
GM_setValue("wTorrent_User","");
GM_setValue("wTorrent_Pass","");
GM_setValue("wTorrent_OverRideCheck",false);
GM_setValue("wTorrent_OverRideCheck","");
GM_setValue("wTorrent_Download_Dir","");
}
// sanity checks
if (GM_getValue("wTorrent_OverRideCheck")===false) { if (host == "" || username == "" || password == "") { alert("You need to configure the \"Add Torrents To wTorrent\" user script with your wTorrent parameters before using it.\n\nType about:config in the address bar and filter for wtorrent. Variables are self explanitory."); return false; } }
if (port == "80" || port == "") { port = ""; } else { port = ":"+port; }
// register menu item to enable opening wTorrent page
function wTorrentMenu() { GM_openInTab("http://"+host+port); }
GM_registerMenuCommand("Open wTorrent", wTorrentMenu, "w", "shift alt");
// this function makes a wTorrent link using embeded images
var wTorrentLinkIdCounter = 0;
function makeWTorrentLink(link) {
var wTorrentLink = document.createElement('a');
wTorrentLink.setAttribute("href", link.href);
wTorrentLink.setAttribute("id","wTorrentLink"+wTorrentLinkIdCounter); wTorrentLinkIdCounter++;
wTorrentLink.setAttribute("class", "wtorrentlink");
wTorrentLink.addEventListener('click',wTorrentClick,false);
wTorrentLink.setAttribute("onclick", "return false;");
wTorrentLink.innerHTML = "<img src=\"" + wTorrentImage + "\" style='border: 0px; padding: 0 1px;' alt='Open with wTorrent' title='Open with wTorrent' />";
return wTorrentLink;
}
// wrapper function for wTorrentAdd to fix issue with not getting an error
// when clicking a torrent link the first time after not being logged in
function wTorrentClick() { wTorrentAdd(this.id); }
// this function posts the url for the torrent to wTorrent
function wTorrentAdd(wTorrentLinkId) {
// reference the link
var wTorrentLink = document.getElementById(wTorrentLinkId);
// here we actually send the form data with the torrenturl
post('http://'+host+port+'/index.php?cls=AddT', 'torrenturl='+encodeURIComponent(wTorrentLink.href)+'&start_now=on&download_dir='+download_dir, function(s) {
// our regexes to do checks after adding torrents
var match_added = /Torrent added correctly/gmi;
var already_exists = /Error: File already exists in torrent directory, can't create .torrent/gmi;
var login_needed = /login-username/gmi;
// check for string 'torrent added correctly' in return HTML
if (match_added.exec(s)) {
show_message('Torrent Successfully Added<br/>to wTorrent');
wTorrentLink.getElementsByTagName('img')[0].src=wTorrentSuccess;
wTorrentLink.getElementsByTagName('img')[0].title="Torrent Downloading";
}
// check for string 'error: file already exists in torrent directory'
else if (already_exists.exec(s)) {
show_message('Torrent Already Added<br />to wTorrent');
wTorrentLink.getElementsByTagName('img')[0].src=wTorrentFailed;
wTorrentLink.getElementsByTagName('img')[0].title="Torrent Already Downloaded";
}
// check for login page, and if we find the login we attempt to login
else if (login_needed.exec(s)) {
post('http://'+host+'/','userf='+username+'&passwdf='+password+'&user_login=Login', function(l) {
// if we are still getting a login page there is probably a typo in the config
var login_needed = /login-username/gmi;
if (login_needed.exec(l)) { show_message('Login Failed!<br />Check configuration...'); }
else { wTorrentAdd(wTorrentLinkId); }
});
}
// un-handled issue, try adding torrent manually as you may be having an issue with wtorrent
else {
show_message('Problem Adding Torrent<br />to wTorrent');
// debug
//alert("torrent url: "+wTorrentLink.href+"\n\nDebug Info:\n"+s);
wTorrentLink.getElementsByTagName('img')[0].src=wTorrentFailed;
wTorrentLink.getElementsByTagName('img')[0].title="Torrent Download Failed";
}
})
}
// generic post function taken from GreaseSpot
// http://wiki.greasespot.net/Code_snippets#POST_data_to_a_URL_with_callback_function
function post(url, data, cb) {
GM_xmlhttpRequest({
method: "POST",
url: url,
headers:{'Content-type':'application/x-www-form-urlencoded'},
data: data,
onload: function(xhr) { cb(xhr.responseText); }
});
}
// function from Julien's "Add Torrents to uTorrent" script
// this function inits the script and adds all the wTorrent download links
function scanLinks() {
var links = getLinks();
for (var i=0; i < links.length; i++){
var link = links[i];
if (match(link.href)) {
var wTorrentLink = makeWTorrentLink(link);
link.parentNode.insertBefore(wTorrentLink, link.nextSibling);
}
}
}
// function from Julien's "Add Torrents to uTorrent" script
// this function puts all the links on the page into an array
function getLinks() {
var doc_links = document.links;
var links = new Array();
for (var i=0; i < doc_links.length; i++){
links.push(doc_links[i]);
}
return links;
}
// function from Julien's "Add Torrents to uTorrent" script
// modified to be a little more efficient.
function match(url) {
var matchRegex = new Array(
// isohunt
/http:\/\/.*isohunt\.com\/download\//i,
/http:\/\/.*bt-chat\.com\/download\.php/,
// TorrentReactor
/http:\/\/dl\.torrentreactor\.net\/download.php\?/i,
// Mininova
/http:\/\/www\.mininova\.org\/get\//i,
// TorrentSpy
/http:\/\/ts\.searching\.com\/download\.asp\?/i,
/http:\/\/www\.torrentspy\.com\/download.asp\?/i,
// Seedler
/http:\/\/.*seedler\.org\/download\.x\?/i,
// all direct torrent links
/\.torrent$/
);
for (r=0; r<matchRegex.length; r++) {
if (url.match(matchRegex[r])) {
if (r<3) { alert(url.match(matchRegex[r])); }
return true;
}
}
return false;
}
// opacity script from brainerror.net
// http://brainerror.net/scripts/javascript/blendtrans/
function opacity(id, opacStart, opacEnd, millisec) {
//speed for each frame
var speed = Math.round(millisec / 100);
var timer = 0;
//determine the direction for the blending, if start and end are the same nothing happens
if(opacStart > opacEnd) {
for(i = opacStart; i >= opacEnd; i--) {
setTimeout(opacity_change,(timer * speed),i,id);
timer++;
}
}
else if(opacStart < opacEnd) {
for(i = opacStart; i <= opacEnd; i++) {
setTimeout(opacity_change,(timer * speed),i,id);
timer++;
}
}
}
function opacity_change(opacity, id) {
var object = document.getElementById(id).style;
object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
}
function opacity_shift(id) {
//if an element is invisible, make it visible, else make it ivisible
if(document.getElementById(id).style.opacity == 0) { opacity(id, 0, 100, 1000); }
else { opacity(id, 100, 0, 1000); }
}
// these functions show and hide status messages so that there is no need to click ok when
// adding torrents
GM_addStyle('#wtorrent_message { z-index: 9999; position: fixed; bottom: 5px; right: 5px; font-weight: bold; color: #333; background: #FFF url("data:image/jpeg;base64,%2F9j%2F4AAQSkZJRgABAQEASABIAAD%2F2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7%2FwAALCABQAAEBAREA%2F8QAFQABAQAAAAAAAAAAAAAAAAAABAX%2FxAAYEAEBAQEBAAAAAAAAAAAAAAAAE2EBUf%2FaAAgBAQAAPwB0MVIYqRxRhilE%2BWHyw6XPDJFz1%2F%2FZ") repeat; border: 2px solid #333; width: 200px; height: 60px; text-align: center; }');
function show_message(text,autohide) {
if (autohide == undefined) { var autohide = true; }
if (node=document.getElementById('wtorrent_message')) { node.parentNode.removeChild(node); }
var wTorrentMessage = document.createElement('div');
wTorrentMessage.setAttribute("id","wtorrent_message");
wTorrentMessage.innerHTML = "<p>"+text+"</p>";
wTorrentMessage.addEventListener('click',hide_message,false);
document.body.insertBefore(wTorrentMessage, document.body.firstChild);
opacity_shift('wtorrent_message');
if (autohide) { setTimeout(hide_message,4000); }
}
function hide_message() {
if(document.getElementById('wtorrent_message').style.opacity>0) { opacity_shift('wtorrent_message',true); }
}
// wTorrent Icons
var wTorrentImage = "data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP38%2FAPy7egu7ebdRPv6%2BQri1shw3c%2B%2Fhe3m3UT%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAP%2F%2F%2FwD%2F%2F%2F8A%2F%2F%2F%2FAPby7h%2FApYX%2Fu6mJ%2F7Kwkf%2FApYX%2Fq7aX%2F6DAof%2B5q4v%2F3M69if%2F%2F%2FwD%2F%2F%2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%DE%B0y%95%F8%17%A8%C8%05%2B%8B%C121%F8%B6%16%8C%17K%97aw%B7h%A3%60%D5%20%8D%15%E4%10%23%8A%03%FC%F4a%15%02%D7Z%F1%BD%9E%86%87T%E2%B3Z%01o%9D%19%FC%E5%16L%A8%F3%D1%93%95%CA%C7%60%22%E9(%3F%95%EF'%9E%1C%DC%CB%8EJv%E1K%B5%11%DE%86%F3%F1%7C%AA%3A%86G9%5B%97a%F6w8%E92%0DJw%0B%07%C4%E9f'%B1%93y%90%BF%9D%EB%17m%E6zs%D3%98%9E%ECTsw%E6%06%E1_%B7%F1_%F3%13%1D%D2%CE%B9Ir%1B%FE%00%00%00%00IEND%AEB%60%82";
var wTorrentSuccess = "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%01sRGB%00%AE%CE%1C%E9%00%00%00%06bKGD%00%FF%00%FF%00%FF%A0%BD%A7%93%00%00%00%09pHYs%00%00%0B%13%00%00%0B%13%01%00%9A%9C%18%00%00%00%07tIME%07%D8%0A%1D%16%177%7F%EF%5DZ%00%00%01yIDAT8%CB%A5%93%CD%2BDQ%18%C6%9Fs%EF%99%D3qgJ%B84%F9%18%9A%C9wI(%0B%A3d%C3%DAJ%B2%B4%B2%B1%B1%90dmia%C1%8E%FF%40%F64Q%12%93PL%94h%CCD%9Ad%CAm%A63%E7%9Ckc%A1i%AE%8F%EE%BB%7C%17%BF~o%CF%FB%10%D7u%E1g%0C%F8%1C%DF%00Zi9%B5%19%85E98%E5%E0%94%81%1A%0C%EB3%87%7F%07%14%85%C0%F4%E0%02%A8Ia%12%03%FB%D7%3B%FF3%E0%8C%E9%BD%CB-%F2%DD%00%00%F1c%A0%FFd%10%5D%25%B0%18A8dCj%85%EC%FB%03Z%EB%DA!%B4%C4%DC%F6%008e%D8%98%3D%A9%9C%82%BDD%A0%B4%89%A1%C88%8AR%40j%85%92%96PZAH%89%88%DD%0D!%85w%8C%B95%17%A6%A1%9Cd%FA%C0%89%D9%BDD%B9%1AR%97%A0%E1%A2%A9%B6%C3H%E7R%0E%A3%CC%F1%3C%C1%5E%22UAjZC%911%0C%B7%8D%E29%9FFI%95%F0%92%CF%A0%AF9%8E%00%0DX%99%5C%EAG%83BA%ABh%E2n%3F%9BL%1F%A36%14%86a0%D4%04%1Bp%FAx%80%9B%EC9%84%96V9%80%94w%A1q%99t2%8A%C4D%D7dx%A0e%04%17OG%B8%7F%BD%D4%16%E3%A1%DD%F9%C7%C2%AF%80%2FH%0F%A7H%8C%C4%E2%F5%B7%CFg%9AS%AB%FAp%F1%ED%A3R%8C%C4%AB%8D%CD%CB%A4%DFbHrJ%EA%AEVt%DE%EB%0F%88%DF%3A%7F%02_%1F%95%FF%E8%EA%EB%2C%00%00%00%00IEND%AEB%60%82";
// start the script
scanLinks();
})();