Skip to content

Instantly share code, notes, and snippets.

@keiya
Created April 8, 2023 08:53
Show Gist options
  • Save keiya/5ca054847140ee88bffb515d79c789d4 to your computer and use it in GitHub Desktop.
Save keiya/5ca054847140ee88bffb515d79c789d4 to your computer and use it in GitHub Desktop.
Violentmonkey script that scan magnet links and sends an RPC request to Transmission on click
// ==UserScript==
// @name Magnet link to Transmission
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Automatically send magnet link to Transmission for download
// @match *://*/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
const transmissionUrl = "http://xxxx:9091/transmission/rpc";
const transmissionAuth = "";
(function () {
'use strict';
// Find all magnet links on the page
const magnetLinks = document.querySelectorAll('a[href^="magnet:"]');
// Loop through the magnet links
magnetLinks.forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault();
// Get the magnet link
const magnet = link.href;
// Set up the RPC request to Transmission
const request = {
"method": "torrent-add",
"arguments": {
"paused": false,
"filename": magnet
}
};
GM_xmlhttpRequest({
method: "POST",
url: transmissionUrl,
headers: {
"Authorization": "Basic " + btoa(transmissionAuth),
"Content-Type": "application/json"
},
onload: (response) => {
if (response.status === 409) {
// セッションIDの取得に失敗した場合、エラーメッセージからセッションIDを抜き出して、リクエストに再度付与して送信する
const sessionId = response.responseText.match(/X-Transmission-Session-Id: ([a-zA-Z0-9]+)/)[1];
GM_xmlhttpRequest({
method: "POST",
url: transmissionUrl,
headers: {
"Authorization": "Basic " + btoa(transmissionAuth),
"X-Transmission-Session-Id": sessionId,
"Content-Type": "application/json"
},
data: JSON.stringify(request),
onload: (response) => {
if (response.status === 200) {
console.log("Transmission RPC request succeeded:", response.responseText);
} else {
console.error("Transmission RPC request failed:", response);
}
},
onerror: (error) => {
console.error("Transmission RPC request failed:", error);
}
});
} else if (response.status === 200) {
// セッション// IDを取得して、トレントを追加するリクエストを送信する
const sessionId = response.responseHeaders.match(/X-Transmission-Session-Id: ([a-zA-Z0-9]+)/)[1];
addTorrentRequest["arguments"]["filename"] = "magnet:?xt=urn:btih:トレントのハッシュ値";
GM_xmlhttpRequest({
method: "POST",
url: transmissionUrl,
headers: {
"Authorization": "Basic " + btoa(transmissionAuth),
"X-Transmission-Session-Id": sessionId,
"Content-Type": "application/json"
},
data: JSON.stringify(addTorrentRequest),
onload: (response) => {
if (response.status === 200) {
console.log("Transmission RPC request succeeded:", response.responseText);
} else {
console.error("Transmission RPC request failed:", response);
}
},
onerror: (error) => {
console.error("Transmission RPC request failed:", error);
}
});
} else {
console.error("Transmission RPC request failed:", response);
}
}
});
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment