aluizbsilva / Empornium New Tag

// ==UserScript==
// @name        Empornium New Tag
// @namespace   http://localhost
// @include     http://*.empornium.me/torrents.php*
// @version     1
// @grant       none
// ==/UserScript==

var new_img =
	"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3wEWDSse4wl28AAAAW5JREFUOMvtkrsvQwEUxn/36q1qG4nBK5GIkDCYEIxiwOYPMLoibKZOJpFUymLpwEJDbEYiZhElDOIVaVW0lNaj9F09ltum5g4WJzk5X84r53znKCJCOaJSpvx9A9PC8cQ8MAW4gYyBPYYNAR/AnRFvM+osxRqnV3+NZ2OyeeUSp1d/iqVfZfPKJalcXCLJRwl++uQieiRLJ9Oy41+THf+aOL365UsiKMunM6ICFpOiMdo6CWA3V1TSaGsh851m9Xw2vHWzSHN1B111g7TXdBe0w6xWksh9PpiAvJDn+u2EvoYRW2E3i8mK3jlXn8x+cft+RlftAFnJoikavfXDRFKPAJdFEvcCG/HehqEiOalcgtXz2fDG9cLhbsBDlWbnPfVMMO6jztrE/v0WgPvXFY6e9tBUy68Jxtod/SKSFBE+MlFCXz5UpYK3dBhHz8q24vTqrpIr2ErwOBAAosCB4V8H8oUcR8/KtPL/yuU3+AE5xKIM0Abf2AAAAABJRU5ErkJggg==";

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(
			".icon_new{background: url(" + new_img + ") no-repeat scroll center}",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);
		}
	}
	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.setAttribute("title", "Click to mark all torrents as old");
	img.src = new_img;
	a.appendChild(img);
	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 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 from here");
		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");
	}
}

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