Created
September 4, 2021 15:24
-
-
Save pixelomer/0fbc907a2039c0a8d0c9f35d1705702e 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 Tranmsmission Web Magnets | |
// @version 1.0 | |
// @description Adds "Copy Magnet Link" option to Transmission Web | |
// @author pixelomer | |
// @match *://*:9091/* | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
(function(){ | |
var sendRequestReal = TransmissionRemote.prototype.sendRequest; | |
TransmissionRemote.prototype.sendRequest = function(data, callback, context, async) { | |
if ( | |
(data != null) && | |
(data.method === "torrent-get") && | |
(data.arguments != null) && | |
Array.isArray(data.arguments.fields) | |
) { | |
data.arguments.fields.push("magnetLink"); | |
} | |
sendRequestReal.apply(this, arguments); | |
} | |
var contextMenu = document.getElementById("torrent_context_menu"); | |
if (contextMenu != null) { | |
var verifyItem = null; | |
var children = contextMenu.children; | |
for (var i=0; i<children.length; i++) { | |
if (children[i].getAttribute("data-command") === "verify") { | |
verifyItem = children[i]; | |
break; | |
} | |
} | |
if (verifyItem != null) { | |
var newItem = verifyItem.cloneNode(false); | |
newItem.innerHTML = "Copy Magnet Link"; | |
newItem.setAttribute("data-command", "copy-magnet"); | |
newItem.setAttribute("id", "ui-id-copy-magnet"); | |
contextMenu.insertBefore(newItem, verifyItem.nextSibling); | |
var contextmenuReal = $.prototype.contextmenu; | |
$.prototype.contextmenu = function(object) { | |
if (this.selector === "ul#torrent_list") { | |
var selectReal = object.select; | |
object.select = function(event, ui) { | |
if (ui.cmd === "copy-magnet") { | |
var textArea = document.createElement("textarea"); | |
textArea.style.top = "0"; | |
textArea.style.left = "0"; | |
textArea.style.position = "fixed"; | |
textArea.value = ""; | |
var selectedTorrents = window.transmission.getSelectedTorrents(); | |
for (var i=0; i<selectedTorrents.length; i++) { | |
textArea.value += selectedTorrents[i].fields["magnetLink"] + "\n"; | |
} | |
document.body.appendChild(textArea); | |
textArea.focus(); | |
textArea.setSelectionRange(0, textArea.value.length - 1); | |
try { | |
document.execCommand("copy"); | |
} | |
catch (err) { | |
console.error("document.execCommand(\"copy\") didn't work.\n", err); | |
} | |
document.body.removeChild(textArea); | |
} | |
else { | |
selectReal.apply(this, arguments); | |
} | |
}; | |
} | |
contextmenuReal.apply(this, arguments); | |
}; | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment