Greasy Fork

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-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         GGn Copy Links From Page
// @namespace    https://greasyfork.org
// @version      0.1
// @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(() => {
    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.");
        });
    });
  });
})();