-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTorrent_Delegate.user.js
93 lines (84 loc) · 2.05 KB
/
Torrent_Delegate.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ==UserScript==
// @name Torrent Delegate
// @namespace http://www.reality-debug.co.uk
// @description Delegates clicks to server torrent client.
// @include *
// @version 1
// ==/UserScript==
var debug = false;
var host = "http://192.168.1.3";
var subDir = "/transmission/rpc";
var port = "9091";
var webHost = host + ":" + port + subDir;
function postTorrentByURI(uri, newSessionID=null){
if(newSessionID == null){
var sessionID = 1;
}else{
var sessionID = newSessionID;
}
var req = "{\"method\": \"torrent-add\",\"arguments\":{\"filename\":\""+uri+"\"\}\}";
GM_xmlhttpRequest({
method:"POST",
url:webHost,
headers: {"User-Agent": "Mozilla/5.0","Accept": "text/xml", "X-Transmission-Session-Id": sessionID},
data: req,
onload: function(response){
if(response.responseText.match(/success/)){
alert("Torrent successfully added!");
}else if(response.responseText.match(/duplicate torrent/)){
alert("Torrent already exists");
}else{
var re = new RegExp(".+X-Transmission-Session-Id:(.+)(</code></p>)");
var newSessID = re.exec(response.responseText)[1];
postTorrentByURI(uri, newSessionID=newSessID);
}
},
ontimeout: function(response){
alert(response.responseText);
}
});
}
function isTorrentSite(){
var links = document.links;
for ( var i in links )
{
if(isTorrent(links[i].href)){
return true;
}
}
return false
}
function isTorrent(str){
if(str.match(/magnet:/g)){
return true;
}
return false;
}
function torrentHandler(eventIn){
var href = findHrefs(eventIn.target);
if(isTorrent(href.toString()) == true){
postTorrentByURI(href);
eventIn.stopPropagation();
eventIn.preventDefault();
}
}
function findHrefs(o){
if (o.nodeName == "A"){
return (o);
} else if (o.nodeName == "BODY") {
return false;
} else {
return o.parentNode;
}
}
if(isTorrentSite()){
var links = document.links;
for(var i in links){
if(debug == true){
if(!isTorrent(links[i].href)){
links[i].style.color="#FF0000";
}
}
}
document.addEventListener('click', torrentHandler, false);
}