shitburg / Empornium Inline Preview

// ==UserScript==
// @name        Empornium Inline Preview
// @namespace   nowhere.man
// @include     http://*.empornium.me/torrents.php*
// @include     https://*.empornium.me/torrents.php*
// @version     1.1
// @grant       none
// ==/UserScript==

var new_img =
 "https://www.empornium.me/static/styles/empornium/images/sticky_unread.png"

function init() {

	if (window.location.href.indexOf("?id=") === -1) {

		if (!localStorage.lastId) {
			localStorage.lastId = 1;
		}

		if (!localStorage.maxId) {
			localStorage.maxId = 1;
		}

		identifyNewTorrents(localStorage.lastId);
		addClearNewButton();

		document.styleSheets[0].insertRule("div.tags{padding-left: 0px}", 0);
		document.styleSheets[0].insertRule("#torrent_table tr {vertical-align: middle;}", 0);
		document.styleSheets[0].insertRule(
			".icon_new{background: url(" + new_img + ") no-repeat scroll center}",
			0);
	} else {

		//always hide details
		document.getElementById("details_top").setAttribute("style", "display: none");
		document.getElementById("slide_button").innerHTML = "Show Info";

		//move tags to top
		var head = document.createElement("div");
		head.setAttribute("class", "head");

		var box = document.createElement("div");
		box.setAttribute("class", "box");

		var clear = document.createElement("div");
		clear.setAttribute("style", "clear: both");


		var linkbox = document.createElement("div");
		linkbox.setAttribute("style", "height: auto; clear: both; padding-bottom: 32px");

		head.innerHTML = "<strong>Tags</strong>";

		var tags = document.getElementById("tag_container").cloneNode(true);
		tags.setAttribute("style", "display: block");
		tags.setAttribute("class", "tags2");


		document.styleSheets[0].insertRule("div.tags2 li {width: auto; float: left;}", 0);

		var toRemove = tags.getElementsByTagName("div")[0];
		toRemove.parentNode.removeChild(toRemove);

		toRemove = tags.getElementsByTagName("div")[1];
		toRemove.parentNode.removeChild(toRemove);


		box.appendChild(tags);
		box.appendChild(clear);
		linkbox.appendChild(head);
		linkbox.appendChild(box);
		

		var container = document.querySelector("#details_top");

		container.parentNode.insertBefore(linkbox, container.nextSibling);

		document.querySelector("#details_top #torrent_tags").setAttribute("id", "");
		document.styleSheets[0].insertRule("div.s-useless-desc  {display: none;}", 0);

	}
}

function identifyNewTorrents(lastId) {

	var rows = document.evaluate(
		"//tr[@class='torrent rowa' or @class='torrent rowb' or @class='torrent redbar']",
		document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
	);
	var maxId = localStorage.maxId;
	var id_re = new RegExp('id=(\\d+)');

	for (var i = 0; i < rows.snapshotLength; i++) {
		var row = rows.snapshotItem(i);
		var torrentTitle = row.querySelector("td:nth-child(2) a");
		var link = torrentTitle.getAttribute("href");

		var regex = link.match(id_re);
		if (regex) {
			var id = parseInt(regex[1], 10);
			if (id > maxId) {
				maxId = id;
			}
			var overlay = window["overlay" + id];
			addNewTorrentSign(row, id, lastId);
			makeLargeMouseOver(row, overlay, id);
			replaceCategoryWithTorrentImage(row, overlay, id);

			moveTableCells(row);
		}
	}

	if (maxId > localStorage.maxId) {
		localStorage.maxId = maxId;
	}
}

function addClearNewButton() {
	var a = document.createElement('a');
	a.setAttribute("href", "javascript:void(0)");
	a.setAttribute("id", "clearNew");
	a.setAttribute("style", "display: block; text-align: center");
	var img = document.createElement("img");
	img.setAttribute("style", "vertical-align: middle;");
	img.src = new_img;
	var span = document.createElement("span");
	span.innerHTML = " Clear New";
	a.appendChild(img);
	a.appendChild(span);
	a.addEventListener('click', function() {
		localStorage.lastId = localStorage.maxId;
		markAsOld(localStorage.maxId);
		return false;
	}, true);

	var cont = document.querySelector("table.torrent_table td.cats_col");
	cont.appendChild(a);
}

function moveTableCells(row) {
	var goodTag = false;
	var badTag = false;

	if (row.querySelector("span.s-good")) {
		goodTag = true;
	}

	if (row.querySelector("span.s-terrible")) {
		badTag = true;
	}

	if (badTag) {
		row.parentNode.removeChild(row);
		return;
	}

	row.getElementsByTagName('td')[0].setAttribute("rowspan", "3");

	//move tags row
	var newtr = document.createElement("tr");
	newtr.setAttribute("class", row.getAttribute("class"));
	var newtd = document.createElement("td");
	newtd.setAttribute("colspan", "9");
	var tags = row.getElementsByTagName("div")[1];
	newtd.appendChild(tags);
	newtr.appendChild(newtd);
	row.parentNode.insertBefore(newtr, row.nextSibling);

	//move title
	newtr = document.createElement("tr");
	newtr.setAttribute("class", row.getAttribute("class"));
	newtd = document.createElement("td");
	newtd.setAttribute("colspan", "9");

	if (goodTag) {
		newtd.setAttribute("style", "background: #a9df9c");
	}

	var anchor = row.querySelector("a[onmouseout]");
	newtd.appendChild(anchor);
	newtr.appendChild(newtd);
	row.parentNode.insertBefore(newtr, row.nextSibling);


	//formatting
	row.getElementsByTagName("span")[0].setAttribute("style", "float:left");


}

function addNewTorrentSign(row, id, lastId) {
	if (id > lastId) {
		var img_element = document.createElement('span');
		img_element.setAttribute("class", "icon icon_new");
		img_element.setAttribute("tid", id);
		img_element.setAttribute("title", "This torrent is new, click to mark as old");
		img_element.setAttribute("style", "cursor: pointer;");
		img_element.addEventListener("click", function handler(evt) {
			markAsOld(this.getAttribute('tid'));
		}, true);
		row.getElementsByTagName("span")[0].appendChild(img_element);
	}
}

function markAsOld(id) {
	localStorage.lastId = parseInt(id, 10);
	var imgs = document.evaluate(
		"//span[@class='icon icon_new' and @tid <= " + id + "]",
		document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
	);
	for (var i = 0; i < imgs.snapshotLength; i++) {
		imgs.snapshotItem(i).setAttribute("style", "display:none");
	}
}


function makeLargeMouseOver(row, html, id) {
	var holder = document.createElement("div");
	holder.innerHTML = html;
	var hoverImages = holder.getElementsByTagName('img');
	if (hoverImages.length > 0) {
		html = "<img src='" + hoverImages[0].src + "' style='box-shadow: 3px 3px 3px #7C7C7C; max-width: " +
			(window.innerWidth / 2) + "px; max-height: " + (window.innerHeight / 2) + "px;'>;";
		window["overlay" + id] = html;
	}
	row.getElementsByTagName("img")[0].setAttribute("onmouseout", "return nd();");
	row.getElementsByTagName("img")[0].setAttribute("onmouseover", "return overlib(overlay" + id + ", FULLHTML);");
}

function replaceCategoryWithTorrentImage(row, html, id) {
	var holder = document.createElement("div");
	holder.innerHTML = html;
	var hoverImages = holder.getElementsByTagName('img');
	var rowImages = row.getElementsByTagName('img');
	if (hoverImages.length > 0 && rowImages.length > 0) {
		rowImages[0].src = hoverImages[0].src;
		rowImages[0].setAttribute("style", "max-height: 150px; max-width: 150px; box-shadow: 3px 3px 3px #7C7C7C;");

		var a = document.createElement('a');
		a.innerHTML = "[" + rowImages[0].parentNode.parentNode.getAttribute("title") + "] ";
		a.setAttribute("href", rowImages[0].parentNode.getAttribute("href"));
		a.setAttribute("style", "padding-left: 10px;");
		row.getElementsByTagName("span")[0].appendChild(a);

		rowImages[0].parentNode.setAttribute("href", "/torrents.php?id=" + id);
	}

}



setTimeout(function() {
	init();
}, 250);