Greasy Fork

Greasy Fork is available in English.

GGn Copy Links From Page

Copy Download links from https://gazellegames.net/torrents.php to clipboard based on a max file size limit (in MB) you specify. Also, calculates the total size of all torrents on the page.

当前为 2025-01-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GGn Copy Links From Page
// @namespace    http://greasyfork.icu
// @version      0.2
// @license      MIT
// @description  Copy Download links from https://gazellegames.net/torrents.php to clipboard based on a max file size limit (in MB) you specify. Also, calculates the total size of all torrents on the page.
// @author       yoshijulas
// @match        https://gazellegames.net/torrents.php*
// @grant        none

// ==/UserScript==

(() => {
	const SIZE_REGEX = /(\d+\.\d+|\d+)\s?(KB|MB|GB)/;

	const toMB = (value, unit) => {
		if (unit === "GB") return value * 1024;
		if (unit === "KB") return value / 1024;
		return value;
	};

	$(document).ready(() => {
		// If its not in the correct page, return
		const torrentbrowse = document.querySelector("div#torrentbrowse");
		if (torrentbrowse === null) {
			return;
		}

		const linkBox = $("#content .linkbox");
		linkBox.append(
			'<br><input type=button id="copyDownloadLinks" value="Copy Links">',
		);
		linkBox.append(
			'<br><input type=text id="maxFileSize" placeholder="Max file size (MB)">',
		);
		linkBox.append('<br><span id="totalSize" style="color: white;"></span>');

		const rows = document.querySelectorAll("tr.group_torrent");
		const torrentData = [];

		for (const row of rows) {
			const torrentLink = row.querySelector("td span a");
			const sizeElements = row.querySelectorAll("td.nobr");

			for (const sizeElement of sizeElements) {
				// Match size format (e.g., 42.50 KB, 1.2 MB, etc.)
				const match = sizeElement.innerHTML.match(SIZE_REGEX);

				if (torrentLink && match) {
					const size = toMB(Number.parseFloat(match[1]), match[2]);
					torrentData.push({ link: torrentLink, size });
				}
			}
		}

		const calculateTotalSize = (maxSizeMB = Number.POSITIVE_INFINITY) =>
			torrentData.reduce(
				(total, { size }) => (size <= maxSizeMB ? total + size : total),
				0,
			);

		const updateTotalSizeDisplay = (maxSizeMB) => {
			const total = calculateTotalSize(maxSizeMB);
			$("#totalSize").text(
				`Total size: ${total.toFixed(2)} MB${Number.isFinite(maxSizeMB) ? ` (limit: ${maxSizeMB} MB)` : ""}`,
			);
		};

		// Initial display
		updateTotalSizeDisplay();

		// Update total size on input
		$("#maxFileSize").on("input", (event) => {
			const maxSizeMB = Number.parseFloat(event.target.value);
			updateTotalSizeDisplay(
				Number.isNaN(maxSizeMB) ? Number.POSITIVE_INFINITY : maxSizeMB,
			);
		});

		$("#copyDownloadLinks").click(() => {
			const maxSizeMB = Number.parseFloat($("#maxFileSize").val());
			if (Number.isNaN(maxSizeMB)) {
				alert("Please enter a valid number for max file size.");
				return;
			}

			const filteredLinks = torrentData
				.filter(({ size }) => size <= maxSizeMB)
				.map(({ link }) => link);

			if (filteredLinks.length === 0) {
				alert("No links match the specified size limit.");
				return;
			}

			navigator.clipboard
				.writeText(filteredLinks.join("\n"))
				.then(() => {
					alert(`Copied ${filteredLinks.length} links to clipboard.`);
				})
				.catch(() => {
					alert("Failed to copy links to clipboard.");
				});
		});
	});
})();