Show Nyaa files

Automatically shows files on Nyaa Torrents.

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name           Show Nyaa files
// @name:sv        Visa Nyaa-filer
// @namespace      AndreasSE93
// @description    Automatically shows files on Nyaa Torrents.
// @description:sv Visar automatiskt filer på Nyaa Torrents.
// @icon           https://files.nyaa.se/favicon.png
// @include        *://www.nyaa.se/*
// @include        *://sukebei.nyaa.se/*
// @version        1.3
// @license        https://opensource.org/licenses/MIT
// @compatible     Firefox 25-52
// @compatible     Chrome 55
// @grant          none
// ==/UserScript==

var nyaaFilesInit = function() {
	var replaceLink = function(link, replacement) {
		var parent = link.parentNode;
		if (link.previousSibling .textContent === "["    ) { parent.removeChild(link.previousSibling); }
		if (link.previousSibling instanceof HTMLBRElement) { parent.removeChild(link.previousSibling); }
		if (link.nextSibling     .textContent === "]"    ) { parent.removeChild(link.nextSibling);     }
		if (link.nextSibling     instanceof HTMLBRElement) { parent.removeChild(link.nextSibling);     }
		if (replacement == null) {
			parent.removeChild(link);
		} else {
			parent.replaceChild(replacement, link);
		}
	};

	var onload = function(xhr, link) {
		var fileList = xhr.response.getElementsByClassName("viewfile")[0];
		replaceLink(link, fileList);
	};

	var xhr    = new XMLHttpRequest();
	var links  = document.getElementsByClassName("viewilink");
	for (var link of links) {
		if (link.innerHTML === "Show files") {
			xhr.addEventListener("load", onload.bind(null, xhr, link));
			xhr.open("GET", link.href);
			xhr.responseType = "document";
			xhr.send();
		} else if (link.innerHTML === "Hide files") {
			replaceLink(link, null);
		}
	}
};

if (document.readyState === "loading") {
	document.addEventListener("DOMContentLoaded", nyaaFilesInit);
} else {
	nyaaFilesInit();
}