There are 2 previous versions of this script.
// ==UserScript==
// @name P2P Links
// @namespace http://userscripts.org/scripts/show/28699
// @description Activates all P2P (eMule / BitTorrent) links on the page at the user command
// @include *
// ==/UserScript==
// Removes duplicates entries from an array
Array.prototype.removeDuplicates = function() {
// Iterates through all the elements of the array but the last one
for (var i = 0; i < this.length - 1; i++) {
// Iterates through all the following elements of the array
for (var f = i + 1; f < this.length; f++) {
if (this[i] == this[f]) this.splice(f, 1); // If a duplicate entry is found, delete it
}
}
}
// Runs a particular XPath expression p against the context node context (or the document, if not provided)
// Returns the results as an array
function $x(p, context) {
if (!context) context = document;
var arr = [], xpr = document.evaluate(p, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0, l = xpr.snapshotLength; i < l; i++) arr.push(xpr.snapshotItem(i));
return arr;
}
// Returns an array with the contents of the HTMLCollection, HTMLOptionsCollection or NodeList object passed
// This is useful because we can use Array methods like forEach, map, filter, etc... then with the returned array
// Returns null if htmlCol is undefined, but its type isn't checked
function colToArray(htmlCol) {
if (!htmlCol) return null;
var arrayCol = [];
for (var i = 0; i < htmlCol.length; i++) arrayCol.push(htmlCol.item(i));
return arrayCol;
}
// Returns an array of all the eMule links
function getAlleMuleLinks() {
return $x("//a[starts-with(translate(@href, 'edk', 'EDK'), 'ED2K://')]");
}
// Returns an array of all the BitTorrent links
function getAllBTLinks() {
// Generic BT support
var arrayBTLinks = $x("//a[translate(substring(@href, string-length(@href) - 7), 'torent', 'TORENT') = '.TORRENT']");
// Support for notable websites with indirect links to torrent files
var arraySpecialBTLinks = [], siteHost = window.location.hostname;
if (/^www\.mininova\.org$/i.test(siteHost)) {
// Mininova support: href = "http://www.mininova.org/get/[torrentId]"
// Gets an array with all the links that match the pattern
arraySpecialBTLinks = colToArray(document.links);
arraySpecialBTLinks = arraySpecialBTLinks.filter(function(s) {return /^http:\/\/www\.mininova\.org\/get\/\d+$/i.test(s.href)});
}
else if (/^www\.demonoid\.com$/i.test(siteHost)) {
// Demonoid support: href = "http://www.demonoid.com/files/download/(HTTP/)[torrentId]/[Number]/"
// Gets an array with all the links that match the pattern
arraySpecialBTLinks = colToArray(document.links);
arraySpecialBTLinks = arraySpecialBTLinks.filter(function(s) {return /^http:\/\/www\.demonoid\.com\/files\/download\/(?:HTTP\/)?\d+\/\d+\/$/i.test(s.href)});
}
arrayBTLinks = arrayBTLinks.concat(arraySpecialBTLinks);
return arrayBTLinks;
}
// Replaces the current url with newUrl, activating the ED2K links
function urlReplace(newUrl) {
window.location.replace(newUrl);
}
// Filters the passed nodes. It will return true if the node is in the selection
function selectedLinkFilter(node) {
return window.getSelection().containsNode(node, true);
}
// Search for all the ED2K links (linkType 1) or links to torrent files (linkType 2) and activates them (a new tab will be opened with each BitTorrent link)
// If onlySelected is true, only the links in the selection will be evaluated
// The urlReplace function won't work with BitTorrent links because the download of the torrent file isn't instantaneous, so it will only load the last torrent
function clickLinks(linkType, onlySelected) {
var arrayLinks = (linkType == 1) ? getAlleMuleLinks() : getAllBTLinks();
if (onlySelected) arrayLinks = arrayLinks.filter(selectedLinkFilter);
arrayLinks = arrayLinks.map(function(s) {return s.href}); // Gets the href of each link
arrayLinks.removeDuplicates();
arrayLinks.forEach((linkType == 1) ? urlReplace : GM_openInTab);
}
// Registers the click all / click selected P2P links commands
GM_registerMenuCommand("Activate all eMule links", function() {clickLinks(1, false)}, null, null, "E");
GM_registerMenuCommand("Activate all BitTorrent links", function() {clickLinks(2, false)}, null, null, "B");
GM_registerMenuCommand("Activate all eMule links in the selection", function() {clickLinks(1, true)}, null, null, "M");
GM_registerMenuCommand("Activate all BitTorrent links in the selection", function() {clickLinks(2, true)}, null, null, "T");
