Last active
September 24, 2017 07:04
-
-
Save waclaw66/1d873f9d2b47a223db5cfe2b5a944755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Quickly add torrent files to Deluge 1.3+ WebUI | |
// @description This script adds a Deluge icon next to torrent links. Clicking on it adds the torrent to the WebUI without leaving the page. | |
// @namespace http://deluge-torrent.info/ | |
// @include * | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
// Version : 1.6.5 | |
// Author : | |
// First version by Martijn Voncken ; [email protected]. | |
// Re-write and Deluge 1.2+ support : aidos | |
// Deluge 1.3+ fix: waclaw66 | |
// Contributors : | |
// Url-based submit and parsing based on : "Add Torrents To utorrent" by Julien Couvreur | |
// Cookie support by Aqtrans | |
// Updated | |
// ****************************************************************** | |
// /!\ These parameters need to be edited before using the script /!\ | |
// ****************************************************************** | |
// URL format : http(s)://HOST:PORT/ | |
var deluge_password = "password"; | |
var deluge_url = ""; // for example : "https://example.net:8081" | |
//******************************************************************* | |
if (deluge_url == "") { alert('You need to edit this user script with your Deluge parameters before using it.'); } | |
/* Image number: | |
0 = deluge (blue) | |
1 = downloading (green) | |
2 = active (blue+green) | |
3 = seeding (blue) | |
4 = loading | |
5 = error | |
*/ | |
var images = { | |
0:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH1wkPFjUNUreV2QAAAnRJREFUOI19k0toE1EUhv/Mncykd5pMMtNkTDQKGrEWC5ZqXVQ36sJSqAvJztdKUItu3HTXhYggWLBFdyIiihQ3XYgLRUVKfUCpVFrow7TW2k7StGmSmUxmJhk3FmyT9Czv+c7HOefeC2wT3T397dd7Bo5tx7hqJS7cuie0Hdw/zbndZGxivvHR3Wtr1TimlqDBJz3RbSaslUkosjs6VIurKujqfhjn/cq5JY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YyEoIBkACNxLMn6OkZ1F6jKiLVs1mpIknDEWoLIm5lPEkQEQHcdhEC6s4M9sMUA3ZUVMmlKZo+4f0K664B9B7q1uUl8+jRZY94Am97TjyKMcwy8Z/SAi573xCIGUFsV2dhqtxfv372bbl29ijcGL0JUKDBdLjNdLjM4c4YgQqrKf/k8z3u9NFWtVkU6GYBfvS8VnbMhSa5NXrqUHX78GNdus/n6NdY50kaDVJVGPs8/29tpS6RSUf3+rVcuOvfLB+Pj33xy9myWep2NKEK8Z9gYtkPgeb+ftkQWflP94Z1huubcQm5oaDZrjKoIiKAiJhYhUV2sqJbem8a9OB/bi7M90LUqsnEY+z8wyrjddZe8zwAAAABJRU5ErkJggg==' | |
}; | |
var counter = 0; | |
function makeTorrentLink(link, url, i) | |
{ | |
var torrentLink = document.createElement('a'); | |
var id = 'deluge_link' + i; | |
torrentLink.setAttribute("href", url); | |
torrentLink.setAttribute("id", id); | |
torrentLink.style.paddingLeft = "2px"; | |
torrentLink.innerHTML = "<img src=\"" + images[0] + "\" style='border: 0px' id='image_" + id + "' />"; | |
link.parentNode.insertBefore(torrentLink, link.nextSibling); | |
var domLink = document.getElementById(id); | |
domLink.addEventListener("click", evt_callback_factory(url, id),false); | |
} | |
function changeImageSrc(linkId, status, msg) | |
{ | |
msg = typeof(msg) != 'undefined' ? msg : "Blah"; | |
var imgIdx = 2; | |
if( status == "ready" ) {imgIdx = 0;} | |
if( status == "loading" ) {imgIdx = 4;} | |
if( status == "done" ) {imgIdx = 1;} | |
if( status == "error" ) {imgIdx = 5;} | |
img = document.getElementById("image_"+linkId); | |
img.src = images[imgIdx]; | |
img.title = msg; | |
} | |
function evt_callback_factory(url, linkId) | |
{ | |
function send_request(method, params, onsuccess) // extra args get passed to onsuccess | |
{ | |
var extra_args = new Array(); | |
for(var i=3; i < arguments.length; i++) extra_args.push(arguments[i]); | |
GM_xmlhttpRequest( | |
{ | |
method: "POST", | |
url: deluge_url + "/json", | |
headers: {'Content-type':'application/json'}, | |
data: '{"method":"' + method + '","params":' + JSON.stringify(params) + ',"id":' + counter++ + '}', | |
onload: function(xhr) | |
{ | |
res = JSON.parse(xhr.responseText); | |
if (res.error == null) | |
{ | |
extra_args.unshift( res ); | |
onsuccess.apply(null, extra_args); | |
} | |
else | |
{ | |
changeImageSrc(linkId, "error", "Error in response to " + method + ":\n" + xhr.responseText); | |
} | |
}, | |
onerror:function(xhr) | |
{ | |
changeImageSrc(linkId, "error", "Error while calling " + method); | |
} | |
}); | |
} | |
function evt_torrent_submit(evt) | |
{ | |
changeImageSrc(linkId, "loading", "Authenticating..."); | |
var params = [ deluge_password ]; | |
send_request("auth.login", params, get_download_path); | |
if (evt.preventDefault) | |
{ | |
evt.preventDefault(); | |
} | |
} | |
function get_download_path(res) | |
{ | |
changeImageSrc(linkId, "loading", "Get download dir..."); | |
var params = []; | |
if(url.match(/^magnet:/i) ) | |
send_request("core.get_config", params, add_magnet); | |
else | |
send_request("core.get_config", params, get_torrent); | |
} | |
function get_torrent(res) | |
{ | |
var remote_download_dir = res.result.download_location; | |
changeImageSrc(linkId, "loading", "Downloading torrent..."); | |
var params = [url, document.cookie]; | |
send_request("web.download_torrent_from_url", params, add_torrent, remote_download_dir); | |
} | |
function add_magnet(res) | |
{ | |
var remote_download_dir = res.result.download_location; | |
changeImageSrc(linkId, "loading", "Adding magnet..."); | |
var remote_download_dir = res.result.download_location; | |
var params = [url, {download_location: remote_download_dir}]; | |
send_request("core.add_torrent_magnet", params, after_add); | |
} | |
function add_torrent(res, remote_download_dir) | |
{ | |
var path = res.result; | |
changeImageSrc(linkId, "loading", "Adding torrent..."); | |
var params = [[{path : path, options : {download_location: remote_download_dir}}]]; | |
send_request("web.add_torrents", params, after_add); | |
} | |
function after_add(res) | |
{ | |
changeImageSrc(linkId, "done", "Adding successful : \n" + url + "\n"); | |
} | |
function debug_request(res) | |
{ | |
alert(res.result); | |
} | |
return evt_torrent_submit; | |
} | |
function match(url) | |
{ | |
var ret = { isValid : false, torrentUrl : url}; | |
// magnet link | |
if (url.match(/^magnet:\?.*xt=urn:btih:[a-f0-9]{40}.*/i)) { | |
ret.isValid = true; | |
} | |
if (url.match(/\.torrent$/)) { | |
ret.isValid = true; | |
} | |
// isohunt format | |
if (url.match(/^http(s?):\/\/.*isohunt\.com\/download\//i)) { | |
ret.isValid = true; | |
} | |
// bt-chat format | |
if (url.match(/^http(s?):\/\/www\.bt-chat\.com\/download[0-9]?\.php\?id=([0-9]+)/i)) { | |
ret.isValid = true; | |
ret.torrentUrl = url+"&type=torrent"; | |
} | |
// TorrentReactor | |
if (url.match(/^http(s?):\/\/dl\.torrentreactor\.net\/download.php\?/i)) { | |
ret.isValid = true; | |
} | |
// Mininova | |
if (url.match(/^http(s?):\/\/www\.mininova\.org\/get\//i)) { | |
ret.isValid = true; | |
} | |
// TorrentSpy | |
if (url.match(/^http(s?):\/\/ts\.searching\.com\/download\.asp\?/i)) { | |
ret.isValid = true; | |
} | |
if (url.match(/^http(s?):\/\/www\.torrentspy\.com\/download.asp\?/i)) { | |
ret.isValid = true; | |
} | |
// Seedler | |
if (url.match(/^http(s?):\/\/.*seedler\.org\/download\.x\?/i)) { | |
ret.isValid = true; | |
} | |
//Gazelle : (what.cd etc..) | |
if (url.match(/^http(s?):\/\/.*\/torrents\.php\?action\=download/i)) { | |
ret.isValid = true; | |
} | |
//Legal torrents. | |
if (url.match(/^http(s?):\/\/.*legaltorrents\.com\/get\//i)) { | |
ret.isValid = true; | |
} | |
// Demonoid format (from userscripts comments) | |
if (url.match(/^http(s?):\/\/www\.demonoid\.com\/files\/download\/HTTP\//i)) { | |
ret.isValid = true; | |
} | |
// Demonoid format II (from userscripts comments) | |
if (url.match(/^http(s?):\/\/www\.demonoid\.com\/files\/download\//i)) { | |
ret.isValid = true; | |
} | |
// BitSnoop | |
if (url.match(/^http(s?):\/\/bitsnoop\.com\/get\//i)) { | |
ret.isValid = true; | |
} | |
// TorCache | |
if (url.match(/^http(s?):\/\/torcache\.net\/torrent\//i)) { | |
ret.isValid = true; | |
} | |
// Twitter eztv.it | |
if (url.match(/^http(s?):\/\/re\.zoink\.it\/.+/i)) { | |
ret.isValid = true; | |
} | |
return ret; | |
} | |
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 scanLinks() | |
{ | |
// duplicate link list, so we don't forever loop when we will be adding our owns | |
var links = getLinks(); | |
for (var i=0; i < links.length; i++) | |
{ | |
var link = links[i]; | |
var matchObj = match(link.href); | |
if (matchObj.isValid) | |
{ | |
makeTorrentLink(link, matchObj.torrentUrl, i); | |
} | |
} | |
} | |
scanLinks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment