-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendtoqBittorrent.user.js
More file actions
119 lines (109 loc) · 4.75 KB
/
SendtoqBittorrent.user.js
File metadata and controls
119 lines (109 loc) · 4.75 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// ==UserScript==
// @name Send to qBittorrent
// @namespace acf.me.uk
// @description Send torrents to qBittorrent via the right click context menu. TamperMonkey only!
// @author ACF, MSerj
// @version 0.3
// @downloadURL https://github.com/acffordyce973/UserScripts/raw/refs/heads/main/SendtoqBittorrent.user.js
// @updateURL https://github.com/acffordyce973/UserScripts/raw/refs/heads/main/SendtoqBittorrent.user.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=qbittorrent.org
// @include *
// @grant GM_registerMenuCommand
// @grant GM_notification
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @run-at document-start
// ==/UserScript==
// Set up the qBittorrent configuration prompts
async function setUpQBittorrentSettings() {
const qBittorrentUrl = prompt("Enter qBittorrent WEB UI address:", GM_getValue("qBittorrentUrl", ""));
const username = prompt("Enter qBittorrent WEB UI username:", GM_getValue("username", ""));
const password = prompt("Enter qBittorrent WEB UI password:", GM_getValue("password", ""));
const categories = prompt("Enter comma separated categories:", GM_getValue("categories", []));
GM_setValue("qBittorrentUrl", qBittorrentUrl);
GM_setValue("username", username);
GM_setValue("password", password);
GM_setValue("categories", categories.replace(/\s+/g, '').split(","));
alert("qBittorrent settings saved.");
}
// Function to send download link to qBittorrent WEB UI
function sendToQBittorrent(downloadUrl, category) {
const qBittorrentUrl = GM_getValue("qBittorrentUrl");
const username = GM_getValue("username");
const password = GM_getValue("password");
if (!qBittorrentUrl || !username || !password) {
alert("Please configure your qBittorrent settings first.");
setUpQBittorrentSettings();
return;
}
// Authenticate with QNAP to get a session token
GM_xmlhttpRequest({
method: "POST",
url: `${qBittorrentUrl}/api/v2/auth/login`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
data: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`,
onload: function(response) {
if (response.status === 200 && response.responseText === "Ok.") {
GM_xmlhttpRequest({
method: "POST",
url: `${qBittorrentUrl}/api/v2/torrents/add`,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: `urls=${encodeURIComponent(downloadUrl)}&category=${category}`,
onload: function(response) {
if (response.status === 200) {
if (category == "") { category="Default"; }
GM_notification({text: downloadUrl, title: `Torrent Added to ${category}`, url: qBittorrentUrl, onclick: (event) =>
{
console.log("Notification was clicked.");
},
});
} else {
alert(`Failed to add torrent: ${response.responseText}`);
}
},
onerror: function(error) {
console.error(error)
alert(`Add torrent request failed: ${error}`);
}
});
} else {
console.warn(response)
alert("Failed to authenticate in qBittorrent. Please check your credentials.");
}
},
onerror: function(error) {
console.error(error)
alert("Error connecting to qBittorrent for authentication.");
}
});
}
let clickedEl = null;
document.addEventListener("contextmenu", function(event) {
clickedEl = event.target;
});
GM_registerMenuCommand("Default Download", () => {
if (clickedEl) {
console.log(`the clicked element is :`)
console.log(clickedEl)
const target = clickedEl.closest("a");
sendToQBittorrent(target.href, "");
}
});
let categories = GM_getValue("categories", []);
for (const category of categories) {
GM_registerMenuCommand(category + " Download", () => {
if (clickedEl) {
console.log(`the clicked element is :`)
console.log(clickedEl)
const target = clickedEl.closest("a");
sendToQBittorrent(target.href, category);
}
});
}
// Menu command to configure qBittorrent settings
GM_registerMenuCommand("Configure qBittorrent Settings", setUpQBittorrentSettings);