IMDB->Torrents Linker

By Arvid Last update Jun 19, 2005 — Installed 21,826 times.
// IMDB->Torrents Linker 2.2a

// ==UserScript==
// @name          IMDB->Torrents Linker
// @namespace     http://determinist.org/greasemonkey/
// @description   Places links to various torrentsites on IMDB. Version 2.2a

// @include       http://www.imdb.com/title/*/
// @include       http://www.imdb.com/title/*/#*
// @include       http://www.imdb.com/title/*/combined*
// @include       http://www.imdb.com/title/*/maindetails*

// @include       http://imdb.com/title/*/
// @include       http://imdb.com/title/*/#*
// @include       http://imdb.com/title/*/combined*
// @include       http://imdb.com/title/*/maindetails*
// ==/UserScript==

/*
Changelog:

2006-08-10	2.2a
* Sorry for not updating in a long time. Rewrite on the way.
* This is just a small fix for some minor html-changes on imdb
* Some small fixes.

2005-08-16	2.2	
* Added the tracker object
* More cleanup.
* Added a link to Torrent Typhoon (excellent searchpage, recommended)
Torrent Typhoon searches The Pirate Bay and Mininova, so I removed them.
But i kept the ability to search on IMDB-ID on Mininova
* Added the open all in tabs function. It only works in Greasemonkey 0.5 though

2005-08-14	2.1
* Removed non-ascii chars.
* Added the xpath function
* Added the license block
* General cleanup
* Divided code into functions

*/

/*
 BEGIN LICENSE BLOCK
Copyright (C) 2005 Arvid Jakobsson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You can download a copy of the GNU General Public License at
http://www.gnu.org/licenses/gpl.html
or get a free printed copy by writing to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
END LICENSE BLOCK
*/

var DISP_TORRENTS = true;
var DISP_COVERS = true;
var DISP_TRAILERS = true;
var DISP_SUBTITLES = true;
var DISP_ED2K = true;

// --------------- Engines --------------- 

var trackers = new Array();
var covers = new Array();
var trailers = new Array();
var subs = new Array();
var ed2k = new Array();

/*
Torrent-trackers
*/

//Since torrenttyphoon includes search results from mininova and thepiratebay, these are now commented.
//jabberwalker
trackers.push(new SearchEngine('JB', 'http://jabberwalker.com.ar/img/favicon.ico', 'http://jabberwalker.com.ar/search?q=%title&sub=submit&s=0&s=1&s=3&s=4&s=5&sb=seeds&so=desc&o=exc&o=only&sh=10', false));
//ThePirateBay
trackers.push(new SearchEngine("TPB", "http://thepiratebay.org/favicon.ico", "http://thepiratebay.org/search.php?video=on&q=%title", false));
//Mininova
trackers.push(new SearchEngine("MN",  "http://mininova.org/favicon.ico", "http://mininova.org/search/?cat=4&search=%title", false));
//Mininova
//trackers.push(new SearchEngine("MN-ID", "http://mininova.org/favicon.ico", "http://mininova.org/search/?imdb=%imdb-id", true));
//SweBits
//trackers.push(new SearchEngine("SB", "http://swebits.org/favicon.ico", "http://www.swebits.org/browse.php?c20=1&c5=1&c19=1&c7=1&incldead=0&search=%title", false));
//Torrentbytes
trackers.push(new SearchEngine("TB", "http://torrentbytes.net/favicon.ico", "http://torrentbytes.net/browse.php?incldead=0&search=%title", false));
//TorrentTyphoon
//trackers.push(new Tracker("TT", "", "http://www.torrenttyphoon.com/loading.aspx?cat=movies&q=", false));

//Google torrents
//trackers.push(new SearchEngine("-torrents>", "", "http://www.google.com/search?hl=en&lr=&q=torrents+%title",false));
//IsoHunt
trackers.push(new SearchEngine("IH", "http://isohunt.com/favicon.ico", "http://isohunt.com/torrents.php?ihq=%title",false));
//Yotoshi
trackers.push(new SearchEngine("yotoshi", "http://www.yotoshi.net/images/yotoshi.ico", "http://search.yotoshi.net/search.php?q=%title",false));
//Bittorent.com
trackers.push(new SearchEngine("BT", "http://www.bittorrent.com/favicon.ico", "http://search.bittorrent.com/search.jsp?query=%title",false));
//Torr-Bot
trackers.push(new SearchEngine("Tbot", "http://www.torr-bott.com/favicon.ico", "http://www.torr-bott.com/index.aspx?txtSearch=%title",false));

/*
ED2k
*/

//EDK
//ed2k.push(new SearchEngine("edk", "http://www.emule-project.net/favicon.ico", "http://www.filehash.com/search/%title",false));

/*
Trailers
*/
//Yahoo trailers
//trailers.push(new SearchEngine("-Trailer/", "", "http://video.search.yahoo.com/search/video?p=%title",false));

/*
Subtitles
*/

//Google Subs
//subs.push(new SearchEngine("-Subs>", "", "http://www.google.com/search?hl=en&lr=&q=subtitles+",false));
//unacs
//subs.push(new SearchEngine("unacs", "http://subs.unacs.bg/favicon.ico", "http://subs.unacs.bg/list.php?l=",false));
//SubDB
//subs.push(new SearchEngine("SDB", "", "http://www.subdb.net/subdb/html/modules.php?name=Download_Subtitles&combo_language=croatian&edsearch=",false));

/*
DVD-Covers
*/
//Picsearch.com
//covers.push(new SearchEngine("cover", "http://www.picsearch.com/favicon.ico", "http://www.picsearch.com/search.cgi?q=",false));


// --------------- END OF TRACKERS ---------------  

function xpath(query, context) {
	context = context ? context : document;
	
	return document.evaluate(query, context, null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

function SearchEngine(shortname, icon, searchurl, usesIMDBID) {
	this.shortname = shortname;
	this.icon = icon;
	this.searchurl = searchurl;
	this.usesIMDBID = usesIMDBID;
	
	this.getHTML = function (title, id) {
		var html = "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\"><b>" +
						"<a href=\"" + this.getSearchUrl(title, id);
		html += "\">";

		if (this.icon != "") {
			html += "<img width=\"16\" heigth=\"16\" border=\"0\" src=\"" + this.icon + "\" alt=\"" + this.shortname + "\">";
		}
		else {
			html += this.shortname;
		}
		html += "</b></font>";
		return html;
	}
	
	this.getSearchUrl = function (title, id) {
		var searchUrl = this.searchurl;
		if (this.usesIMDBID) {
			searchUrl = searchUrl.replace(/%imdb\-id/, id);
		}
		else {
			searchUrl = searchUrl.replace(/%title/, encodeURIComponent(title));
		}
		
		return searchUrl;
	}	
}

function openAllInTabs(title, id, inclIMDBID) {
	for (var i = 0; i < trackers.length; i++) {
		if (trackers[i].usesIMDBID && !inclIMDBID)
			continue;
		else
			GM_openInTab(trackers[i].getSearchUrl(title, id));
	}
}

function getTitle() {
	var title = document.title;
	title = title.match(/^(.*) \(/)[1];
	
	/*
	regexp = /'|,|:/g;
	title = title.replace(regexp, " ");
	*/
	
	return title;
}


function getId() {
	with (location.href) {
		var id = match(/title\/tt(.*?)\//i)[1];
	}
	return id;
}


function addIconBarIcons(title, id, trackers) {
	var iconbar = xpath("//strong[@class='title']/../../table[2]/tbody/tr", document);

	if (!iconbar || iconbar.snapshotLength != 1) {
		GM_log("Error! Couldn't find icon bar. Quiting!");
		return;
	}

	iconbar = iconbar.snapshotItem(0);
	iconbar.id = "iconbar";
	
	var tdimg;
	for (var i = 0; i < trackers.length; i++) {
		tdimg = document.createElement("td");
		tdimg.innerHTML = trackers[i].getHTML(title, id);
		iconbar.appendChild(tdimg);
	}
	iconbar.appendChild(document.createElement("td"));
	
	for (var i = 0; i < covers.length; i++) {
		tdimg = document.createElement("td");
		tdimg.innerHTML = covers[i].getHTML(title, id);
		iconbar.appendChild(tdimg);
	}
	iconbar.appendChild(document.createElement("td"));
	
	for (var i = 0; i < covers.length; i++) {
		tdimg = document.createElement("td");
		tdimg.innerHTML = covers[i].getHTML(title, id);
		iconbar.appendChild(tdimg);
	}
	iconbar.appendChild(document.createElement("td"));
	
	for (var i = 0; i < covers.length; i++) {
		tdimg = document.createElement("td");
		tdimg.innerHTML = covers[i].getHTML(title, id);
		iconbar.appendChild(tdimg);
	}
	iconbar.appendChild(document.createElement("td"));
	
	if (GM_openInTab) {
		var tdopenall = document.createElement("td");
		var aopenall = document.createElement("a");
		aopenall.innerHTML = "OPEN ALL";
		aopenall.href = "javascript:;";
		aopenall.setAttribute("class", "openall");
		aopenall.addEventListener("click", function () { openAllInTabs(title, id, true); }, false);
		tdopenall.appendChild(aopenall);
		
		iconbar.appendChild(tdopenall);
	}
}

function addAkaIcons(id, trackers) {
	var xpath_exp = "//i[@class='transl']";
	var akas = xpath(xpath_exp, document);
	
	if (!akas || akas.snapshotLength == 0) {
		GM_log("Error! Couldn't find akas. Quiting!");
		return;
	}
	
	var aka;
	for (var i = 0; aka = akas.snapshotItem(i); i++) {
		unsafeWindow.aka = aka.textContent;
		
		var title = aka.textContent.match(/(.*?)\s+\(.*?\)\s+\[.*?\]/)[1];
		GM_log(title);
		
		for (var ii = 0; ii < trackers.length; ii++) {
			if (!trackers[ii].usesIMDBID) {
				link_span = document.createElement("span");

				link_span.innerHTML = trackers[ii].getHTML(title, id);
				aka.appendChild(link_span);
				
				delim_text = document.createTextNode(" ");
				aka.appendChild(delim_text);
			}
		}
		
		if (GM_openInTab) { //If this API exists.
			var aopenall = document.createElement("a");
			aopenall.innerHTML = "OPEN ALL";
			aopenall.href = "javascript:;";
			aopenall.setAttribute("class", "openall");
			
			function creator (a, b) {
				return function () { openAllInTabs(a, b, false); }
			}
			
			aopenall.addEventListener("click", creator(title, id), false);

			aka.appendChild(aopenall);
		}
	}
}

function addStyles() {
	var open_all_class = "a.openall {\n" +
	"	font-weight: 401;\n" + 
	"	font-family: Verdana, Arial, Helvetica, sans-serif;\n" +
	"	font-size: 10px\n" +
	"}";
	
	GM_addStyle(open_all_class);
}

addStyles();
var title = getTitle();
var id = getId();
addIconBarIcons(title, id, trackers);
addAkaIcons(id, trackers);