Last active
August 6, 2020 14:50
-
-
Save putty182/dca189173d24b72485c6e916fed3725c to your computer and use it in GitHub Desktop.
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 Deluge post-process button | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Adds a 'post-process' button that queues the currently selected torrent for post-processing. | |
// @author You | |
// @match https://example.com/ | |
// @grant none | |
// ==/UserScript== | |
window.addEventListener('load', function() { | |
const root = document.querySelector('#connectionman').parentElement.parentElement | |
function getActiveTorrents() { | |
var selectedTorrents = window.deluge.torrents.selModel.selections.items | |
return selectedTorrents.map(selectedTorrent => ({ | |
hash: selectedTorrent.id, | |
name: selectedTorrent.data.name, | |
path: selectedTorrent.data.download_location, | |
label: selectedTorrent.data.label | |
})) | |
} | |
function addPostProcessButton() { | |
var button = document.querySelector('#post-process-button') | |
if (!button) { | |
var td = document.createElement('td') | |
td.id = "ext-gen67" | |
td.className = "x-toolbar-cell" | |
td.innerHTML = ` | |
<table id="connectionman" cellspacing="0" class="x-btn x-btn-text-icon" style="width: auto;"> | |
<tbody class="x-btn-small x-btn-icon-small-left"> | |
<tr> | |
<td class="x-btn-tl"><i> </i></td><td class="x-btn-tc"></td> | |
<td class="x-btn-tr"><i> </i></td> | |
</tr> | |
<tr> | |
<td class="x-btn-ml"><i> </i></td> | |
<td class="x-btn-mc"> | |
<em class=" x-unselectable" unselectable="on"> | |
<button type="button" id="post-process-button" class=" x-btn-text x-deluge-connection-manager">Post-process</button>\ | |
</em> | |
</td> | |
<td class="x-btn-mr"><i> </i></td> | |
</tr> | |
<tr> | |
<td class="x-btn-bl"><i> </i></td> | |
<td class="x-btn-bc"></td> | |
<td class="x-btn-br"><i> </i></td> | |
</tr> | |
</tbody> | |
</table> | |
` | |
root.appendChild(td) | |
button = document.querySelector('#post-process-button') | |
} | |
button.removeEventListener('click', onPostProcessButtonClick) | |
button.addEventListener('click', onPostProcessButtonClick, false) | |
} | |
async function onPostProcessButtonClick(evt) { | |
var responses = getActiveTorrents().map(async torrent => { | |
const response = await fetch("/post-process", { | |
method: "POST", | |
body: JSON.stringify(torrent), | |
headers: { "content-type": "application/json" }, | |
credentials: "include" | |
}); | |
console.log(response) | |
return response | |
}) | |
console.log(`processed ${responses.length} torrents`) | |
} | |
addPostProcessButton(); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment