TC4Shell.com - Add Download Buttons for 7-Zip Plugins page

Add download buttons to TC4Shell.com's 7-Zip plugins listing page, so one may download from the listing page directly, without opening plugin's page.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           TC4Shell.com - Add Download Buttons for 7-Zip Plugins page
// @name:ru        TC4Shell.com - Добавьте кнопки загрузки для cтраница плагинов архиватора 7-Zip
// @description    Add download buttons to TC4Shell.com's 7-Zip plugins listing page, so one may download from the listing page directly, without opening plugin's page.
// @description:ru Добавьте кнопки загрузки на страницу списка плагинов 7-Zip TC4Shell.com, чтобы можно было загружать напрямую со страницы списка, не открывая страницу плагина.
// @namespace      RainSlide
// @author         RainSlide
// @match          *://www.tc4shell.com/en/7zip/
// @match          *://www.tc4shell.com/ru/7zip/
// @version        1.2
// @license        blessing
// @grant          none
// ==/UserScript==

const links = document.querySelectorAll('#content a');

if (links.length > 0) {

// const pageURL = location.origin + location.pathname;
const pageURL = new URL("./", location).href;

const match = (x, arr) => arr.some( y => y === x );

const plugins = Array.from(links).filter(
	plugin => (
		plugin.href.replace(/[^/]+\/$/, "") === pageURL &&
		match(plugin.parentNode.tagName, ["H2", "P"])
	)
);

if (plugins.length > 0) {

document.head.appendChild(document.createElement("style")).textContent =
`#content h1 ~ div > h2 { display: flex; justify-content: space-between; }
.button_download_small { height: 2em; line-height: 1; padding: .5em;
	font-family: inherit; font-size: .75em; background-color: #e84c3d; }
.button_download_small:hover { background-color: #ff605f; }`;

plugins.forEach(
	plugin => {

		const pluginName = plugin.textContent.split(" ", 1)[0];

		const fileName = match(pluginName, ["Asar7z", "Lzip7z"])
			? pluginName.replace(/7z$/, "")
			: pluginName;

		const i18nMap = new Map([["en", "Download"], ["ru", "Скачать"]]);

		const download = Object.assign(
			document.createElement("a"), {
				href: "/binary/" + fileName + ".zip",
				download: "",
				textContent: i18nMap.get(location.pathname.slice(1, 3)) || "Download"
			}
		);

		switch (plugin.parentNode.tagName) {
			case "H2":
				download.className = "button button_download_small";
				plugin.after(download);
				break;
			case "P":
				plugin.after(" (", download, ")");
				break;
		}

	}
);

}

}