Last active
October 27, 2021 02:40
-
-
Save bluecube/5446ff963e03b3a3de9b4dae73912024 to your computer and use it in GitHub Desktop.
Userscript: Piratebay Transmission button
This file contains hidden or 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 Piratebay Transmission button | |
| // @version 1 | |
| // @grant none | |
| // @match https://*.thepiratebay.org/* | |
| // ==/UserScript== | |
| function transmission_request(data, sessionId) { | |
| return fetch( | |
| "http://192.168.1.2:8080/transmission/rpc", | |
| { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| "X-Transmission-Session-Id": sessionId | |
| }, | |
| body: JSON.stringify(data) | |
| } | |
| ) | |
| } | |
| async function transmission_call(method, arguments) { | |
| const data = {method: method, arguments: arguments}; | |
| const response1 = await transmission_request(data, ""); | |
| if (response1.ok) | |
| return await response1.json(); | |
| else if (response1.status != 409) | |
| throw new Error('Network response was not 2xx or 409'); | |
| const sessionId = response1.headers.get("X-Transmission-Session-Id"); | |
| const response2 = await transmission_request(data, sessionId); | |
| return await response2.json(); | |
| } | |
| document.querySelectorAll('a[href^="magnet:"]').forEach(function(magnetLink) { | |
| //const magnetIcon = magnetLink.getElementsByTagName('img')[0]; | |
| const magnetUri = magnetLink.href; | |
| var transmissionIcon = document.createElement('img'); | |
| transmissionIcon.src = "https://raw.githubusercontent.com/transmission/transmission/8566df069899ce8923463cadeb0ff66d4544991a/qt/icons/hicolor/16x16/transmission-qt.png"; | |
| transmissionIcon.alt = "Open in Transmission" | |
| transmissionIcon.width = 12; | |
| transmissionIcon.height = 12; | |
| var transmissionLink = document.createElement('a'); | |
| transmissionLink.href = "javascript:void(0);"; | |
| transmissionLink.onclick = function() { | |
| transmission_call("torrent-add", {filename: magnetUri}); | |
| }; | |
| transmissionLink.appendChild(transmissionIcon); | |
| magnetLink.parentNode.insertBefore(transmissionLink, magnetLink.nextSibling); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment