Filelist filter

Custom Filelist filter for TV series and number of downloads

Terá de instalar uma extensão como Tampermonkey, Greasemonkey ou Violentmonkey para instalar este script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

Terá de instalar uma extensão como Tampermonkey ou Violentmonkey para instalar este script.

Terá de instalar uma extensão como Tampermonkey ou Userscripts para instalar este script.

Terá de instalar uma extensão como Tampermonkey para instalar este script.

Terá de instalar uma extensão de gestão de scripts de utilizador para instalar este script.

(Já tenho um gestor de scripts de utilizador, deixe-me instalá-lo!)

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão como Stylus para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

Terá de instalar uma extensão de gestão de estilos de utilizador para instalar este estilo.

(Já tenho um gestor de estilos de utilizador, deixe-me instalá-lo!)

// ==UserScript==
// @name Filelist filter
// @description Custom Filelist filter for TV series and number of downloads
// @include https://filelist.io/*
// @version 0.0.1.20260525152136
// @namespace https://greasyfork.org/users/162242
// ==/UserScript==

// ==UserScript==
// @name Filelist filter
// @description Custom Filelist filter for TV series and number of downloads, with multi-page loading
// @include https://filelist.io/*
// @version 0.0.3.20260525
// @namespace https://greasyfork.org/users/162242
// @downloadURL https://update.greasyfork.org/scripts/36115/Filelist%20filter.user.js
// @updateURL https://update.greasyfork.org/scripts/36115/Filelist%20filter.meta.js
// ==/UserScript==
var wordListing = [
"A Knight of the Seven Kingdoms.S",
"Alien: Earth.S",
"Batman: Caped Crusader.S",
"Black Mirror.S",
"Fallout.S",
"For All Mankind.S",
"Foundation.S",
"House of the Dragon.S",
"I Am Groot.S",
"Invasion.S",
"INVINCIBLE.S",
"Paradise.S",
"Reacher.S",
"Severance.S",
"Silo.S",
"South Park.S",
"The Last of Us.S",
"The Lord of the Rings: The Rings of Power.S",
"The Terminal List.S",
"The Walking Dead: Daryl Dixon.S",
"The Walking Dead: Dead City.S",
"The Witcher.S",
"Wednesday.S"
];
var pagesToLoad = 10;        // pages fetched per batch
var maxPages = 50;           // safety cap (max pages ever fetched)
var targetMatches = 120;      // stop once we have this many visible matches
var selection1 = 500;
var selection2 = 999;
var selection3 = 1999;
var selectionColor1 = "#003366";
var selectionColor2 = "#002900";
var selectionColor3 = "#6B0000";
var extraPagesLoaded = false;
var pagesAlreadyFetched = 0;

var ul = document.getElementById("nav").children[0];
var li = document.createElement('li');
li.className = "fleft";
ul.appendChild(li);
var a = document.createElement('a');
var linkText = document.createTextNode("Activate");
a.appendChild(linkText);
a.title = "Load more pages, highlight, and hide non-matches";
a.href = "javascript:void(0)";
a.setAttribute("id", "CustomButton");
a.addEventListener("click", myScript);
li.appendChild(a);

function buildPattern(entry) {
	return entry.replace(/'/g, '').replace(/[ \-_:]/g, '.');
}

async function loadPagesBatch(startOffset, batchSize) {
	var firstRow = document.querySelector('.torrentrow');
	if (!firstRow) return 0;
	var container = firstRow.parentNode;

	var currentUrl = new URL(window.location.href);
	var currentPage = parseInt(currentUrl.searchParams.get('page') || '0', 10);

	var requests = [];
	for (var p = 0; p < batchSize; p++) {
		var nextUrl = new URL(currentUrl);
		nextUrl.searchParams.set('page', currentPage + startOffset + p + 1);
		requests.push(
			fetch(nextUrl.toString(), { credentials: 'include' })
				.then(function(r) { return r.ok ? r.text() : null; })
				.catch(function() { return null; })
		);
	}

	var pages = await Promise.all(requests);
	var parser = new DOMParser();
	var totalAdded = 0;
	for (var idx = 0; idx < pages.length; idx++) {
		if (!pages[idx]) continue;
		var doc = parser.parseFromString(pages[idx], 'text/html');
		var rows = doc.querySelectorAll('.torrentrow');
		for (var r = 0; r < rows.length; r++) {
			container.appendChild(rows[r]);
			totalAdded++;
		}
	}
	return totalAdded;
}

function applyFiltering() {
	var torrentrow = document.getElementsByClassName("torrentrow");
	var visibleCount = 0;
	for (i = 0; i < torrentrow.length; i++) {
		if (typeof torrentrow[i].children !== 'undefined') {
			var title = torrentrow[i].children[1].children[0].children[0].innerText;
			var snatched = torrentrow[i].children[7].children[0].children[0].textContent;
			snatched = snatched.replace(",", "");
			snatched = snatched.replace("times", "");
			snatched = parseInt(snatched);

			var titleMatch = false;
			for (k = 0; k < wordListing.length; k++) {
				if (title.match(buildPattern(wordListing[k]))) {
					torrentrow[i].children[1].children[0].children[0].style.textDecoration = "underline overline";
					torrentrow[i].children[1].children[0].children[0].style.color = "#33CCFF";
					titleMatch = true;
				}
			}

			var snatchColor = false;
			if (snatched > selection3) {
				torrentrow[i].style.backgroundColor = selectionColor3;
				snatchColor = true;
			} else if (snatched > selection2) {
				torrentrow[i].style.backgroundColor = selectionColor2;
				snatchColor = true;
			} else if (snatched > selection1) {
				torrentrow[i].style.backgroundColor = selectionColor1;
				snatchColor = true;
			}

			if (titleMatch || snatchColor) {
				visibleCount++;
			} else {
				torrentrow[i].style.display = "none";
			}
		}
	}
	return visibleCount;
}

async function myScript() {
	var btn = document.getElementById("CustomButton");

	if (btn.innerHTML.indexOf("Activate") === 0 || btn.innerHTML.indexOf("Loading") === 0) {
		btn.innerHTML = "Loading...";

		if (!extraPagesLoaded) {
			var matchCount = 0;
			while (pagesAlreadyFetched < maxPages && matchCount < targetMatches) {
				var batchSize = Math.min(pagesToLoad, maxPages - pagesAlreadyFetched);
				var added = await loadPagesBatch(pagesAlreadyFetched, batchSize);
				pagesAlreadyFetched += batchSize;
				if (added === 0) break;  // reached end of pagination
				matchCount = applyFiltering();
				btn.innerHTML = "Loading... (" + matchCount + ")";
			}
			extraPagesLoaded = true;
		} else {
			applyFiltering();
		}

		btn.innerHTML = "Deactivate";
	} else {
		btn.innerHTML = "Activate";

		var torrentrow = document.getElementsByClassName("torrentrow");
		for (j = 0; j < torrentrow.length; j++) {
			torrentrow[j].children[1].children[0].children[0].style.textDecoration = "none";
			torrentrow[j].removeAttribute("style");
			torrentrow[j].children[1].children[0].children[0].removeAttribute("style");
		}
	}
}