Created
March 10, 2017 09:00
-
-
Save sorz/6dac5737236c9c01fdfb76e4532ce497 to your computer and use it in GitHub Desktop.
Upload a torrent on Transmission Web Interface by pasting URL of thread on share.dmhy.org directly.
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 Transmission-DMHY | |
// @namespace org.sorz.lab.transmission-dmhy | |
// @include https://example.com/transmission/web/* | |
// @version 1 | |
// @grant GM_xmlhttpRequest | |
// @require http://code.jquery.com/jquery-3.1.1.slim.min.js | |
// ==/UserScript== | |
const ICON = 'https://share.dmhy.org/favicon.ico'; | |
let jquery = jQuery; | |
jQuery.noConflict(true); | |
let $ = jquery; | |
$().ready(() => { | |
// add button | |
$('<div id="toolbar-separator">').appendTo('#toolbar'); | |
let $btn = $('<div id="toolbar-dmhy" title="Add from dmhy.org">'); | |
$btn.css('background', `url('${ICON}') center no-repeat`); | |
$btn.css('background-size', '25px 25px'); | |
$btn.appendTo('#toolbar'); | |
$btn.click(dmhyButtonClicked); | |
}); | |
function dmhyButtonClicked() { | |
let threadUrl = prompt("Please type thread URL", "https://share.dmhy.org/topics/view/"); | |
if (threadUrl == null) | |
return; | |
torrentFromDmhyThread(threadUrl, (torrentUrl) => { | |
fetchAsBase64(torrentUrl, uploadTorrentFile); | |
}); | |
} | |
function torrentFromDmhyThread(threadUrl, callback) { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: threadUrl, | |
onerror: () => {alert(`Failed to load page ${threadUrl}.`);}, | |
onload: (resp) => { | |
let html = $.parseHTML(resp.responseText); | |
let page = $(html); | |
let url = page.find('#tabs-1').find('a').first().attr('href'); | |
console.log('Torrent URL: ' + url); | |
if (!url) | |
alert(`Cannot fount torrent on page ${threadUrl}.`); | |
else | |
callback('https:' + url); | |
}}); | |
} | |
function fetchAsBase64(url, callback) { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url, | |
binary: true, | |
responseType: 'blob', | |
onerror: () => { | |
alert(`Failed to downlaod file ${threadUrl}.`); | |
}, | |
onload: (resp) => { | |
console.log('Downloaded file size:' + resp.response.size); | |
let reader = new FileReader(); | |
reader.onload = (e) => { | |
let contents = e.target.result; | |
let key = "base64,"; | |
let index = contents.indexOf(key); | |
if (index > -1) | |
callback(contents.substring(index + key.length)); | |
else | |
alert('Failed to encode torrent file.'); | |
}; | |
reader.readAsDataURL(resp.response); | |
} | |
}); | |
} | |
function uploadTorrentFile(torrent) { | |
let options = { | |
'method': 'torrent-add', | |
arguments: { | |
'paused': false, | |
'download-dir': $('input#add-dialog-folder-input').val(), | |
'metainfo': torrent | |
} | |
}; | |
let callback = (resp) => { | |
if (resp.result != 'success') | |
alert('Failed to upload torrent: ' + resp.result); | |
}; | |
let clonedOptions = cloneInto(options, unsafeWindow); | |
let exportedCallback = exportFunction(callback, unsafeWindow); | |
unsafeWindow.transmission.remote.sendRequest(clonedOptions, exportedCallback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment