Skip to content

Instantly share code, notes, and snippets.

@fopina
Created March 7, 2012 16:22
Show Gist options
  • Save fopina/1994169 to your computer and use it in GitHub Desktop.
Save fopina/1994169 to your computer and use it in GitHub Desktop.
Magnets2Transmission userscript
// ==UserScript==
// @name Magnet-Transmission
// @namespace http://fopina.co.cc
// @description PirateBay + Transmission
// @include https://thepiratebay.se/*
// @include http://thepiratebay.se/*
// ==/UserScript==
unsafeWindow.sendToTransmission = function(href) {
setTimeout(function(){
var bthost = GM_getValue('bthost')
if (!bthost) {
bthost = prompt('Your Transmission RPC URL - including /transmission/rpc','');
GM_setValue('bthost', bthost);
}
var btsession = GM_getValue('btsession')
GM_log('BTHost: ' + bthost);
GM_log('BTSession: ' + btsession);
var request = new Object();
request.method = 'torrent-add';
request.arguments = new Object();
request.arguments.filename = href;
var gmrequest = new Object();
gmrequest.method = 'GET';
gmrequest.url = bthost;
gmrequest.data = JSON.stringify(request);
gmrequest.headers = { 'X-Transmission-Session-Id': btsession };
gmrequest.onerror = function(resp) {
GM_log('got error ' + resp.toString());
}
gmrequest.onload = function (resp) {
GM_log('Status '+ resp.status + ': ' + resp.statusText);
if (resp.status == 409) {
GM_log('save CSFR');
var reply = resp.responseText.split('<code>')[1].split(':')[1].split('<')[0];
GM_setValue('btsession', reply);
btsession = reply;
GM_log('Acquired token ' + reply);
GM_xmlhttpRequest({
method: 'GET',
url: bthost,
data: JSON.stringify(request),
headers: {
'X-Transmission-Session-Id': btsession
},
onload: function (resp) {
GM_log('Status '+ resp.status + ': ' + resp.statusText);
if (resp.status == 409) {
GM_log('again?? fail...');
}
else {
GM_log('Text: ' + resp.responseText);
}
},
onerror: function (resp) {
GM_log('got error ' + resp.toString());
}
});
}
else {
GM_log('Text: ' + resp.responseText);
}
};
GM_xmlhttpRequest(gmrequest);
},0);
}
function removeAds() {
//sorry PirateBay, it's ok to have ads but flash ads??
var s = document.getElementsByClassName('ad')[0];
s.parentNode.removeChild(s);
s = document.getElementsByClassName('ads')[0];
s.parentNode.removeChild(s);
s = document.getElementsByTagName('iframe')[0];
s.parentNode.removeChild(s);
}
function replaceMagnetLinks() {
links = document.getElementsByTagName('a');
for (i = 0; i < links.length; i++) {
var link = links[i];
href = link.getAttribute('href');
if (href.indexOf('magnet:') == 0) {
var newlink = link.cloneNode(true);
newlink.setAttribute('href', 'javascript:sendToTransmission("' + href + '");');
newlink.childNodes[0].setAttribute('src','http://www.transmissionbt.com/favicon.ico');
link.appendChild(newlink);
}
}
}
removeAds();
replaceMagnetLinks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment