Skip to content

Instantly share code, notes, and snippets.

@bemasher
Created January 13, 2014 11:50
Show Gist options
  • Save bemasher/8399010 to your computer and use it in GitHub Desktop.
Save bemasher/8399010 to your computer and use it in GitHub Desktop.
Relatively painless magnet uri generation for torrentz.eu. There's some extra scaffolding for extension related stuff but this is the main guts.
// Relatively painless magnet uri generation for torrentz.eu. Digging reveals
// that the torrent hash and id (torrentz.eu specific) are stored as hidden
// inputs for the comment system. All that's required is some Ajax and string
// parsing for the announce list and some html injection to add the link to
// the page.
// Get the torrent name, hash and id (for announce list lookup)
var name = $(".download>h2>span").first().text();
var hash = $(':input[name="hash"]').first().val()
var torrent = $(':input[name="torrent"]').first().val()
$.ajax({
url: "https://torrentz.eu/announcelist_" + torrent,
datatype: "text"
}).done(function(announcelist) {
// Parse the tracker list
var trackers = $.trim(announcelist).split("\n\n");
// Create the magnet uri. 'urn:btih' gets url encoded when it shouldn't so
// just create this parameter manually.
var magnet = "magnet:?xt=urn:btih:" + hash;
magnet += "&" + $.param({
dn: name,
tr: trackers
}, true);
// Add new source entry to list
$("div.download>dl:first").after(
$('<dl/>').append(
$("<dt/>").append(
$("<a/>", {
href: magnet,
rel: "e",
target: "_blank"
}).append(
$("<span/>", {
'class': "u",
style: "background: transparent url('" + chrome.extension.getURL("icon16.png") + "') no-repeat 5px center;",
text: "Magnet URI"
}),
$("<span/>", {'class': "n", text: name})
)
)
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment