Greasy Fork

Greasy Fork is available in English.

磁力链接批量选择工具(优化按季全选)

在磁力链接旁添加复选框,支持按季批量选择、复制或打开

当前为 2025-02-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         磁力链接批量选择工具(优化按季全选)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  在磁力链接旁添加复选框,支持按季批量选择、复制或打开
// @author       你的名字
// @match        *://*/*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    function addCheckboxes() {
        let links = document.querySelectorAll('a[href^="magnet:?"]');
        if (links.length === 0) return;

        // 避免重复添加
        if (document.getElementById("magnet-toolbar")) return;

        // 🌟 创建悬浮科技风格的工具栏
        let toolbar = document.createElement("div");
        toolbar.id = "magnet-toolbar";
        toolbar.style.position = "fixed";
        toolbar.style.bottom = "20px";
        toolbar.style.right = "20px";
        toolbar.style.background = "rgba(30, 30, 30, 0.9)";
        toolbar.style.border = "1px solid #4CAF50";
        toolbar.style.padding = "10px";
        toolbar.style.borderRadius = "10px";
        toolbar.style.boxShadow = "0 0 15px rgba(0, 255, 0, 0.5)";
        toolbar.style.zIndex = "9999";
        toolbar.style.fontFamily = "Arial, sans-serif";
        toolbar.innerHTML = `
            <button id="copy-selected" style="margin-right:5px;color:white;background:#4CAF50;border:none;padding:5px 10px;border-radius:5px;cursor:pointer;">📋 复制选中</button>
            <button id="open-selected" style="color:white;background:#2196F3;border:none;padding:5px 10px;border-radius:5px;cursor:pointer;">🚀 打开选中</button>
        `;
        document.body.appendChild(toolbar);

        // 🌟 遍历所有磁力链接,按标题分类
        let lastTitle = null;
        links.forEach(link => {
            let title = getNearestTitle(link); // 获取该磁力链接的标题
            if (title && title !== lastTitle) {
                // 检查是否已添加过全选框
                if (!title.querySelector(".season-checkbox")) {
                    // 标题右侧添加“全选”复选框
                    let titleCheckbox = document.createElement("input");
                    titleCheckbox.type = "checkbox";
                    titleCheckbox.classList.add("season-checkbox");
                    titleCheckbox.style.marginLeft = "10px";
                    titleCheckbox.style.transform = "scale(1.5)"; // 让它比普通复选框大 1.5 倍
                    titleCheckbox.dataset.group = title.innerText.trim();
                    title.appendChild(titleCheckbox); // 插入到标题右侧
                    titleCheckbox.addEventListener("change", function() {
                        let group = this.dataset.group;
                        document.querySelectorAll(`a[href^="magnet:?"][data-group="${group}"] + input`).forEach(cb => {
                            cb.checked = this.checked;
                        });
                    });
                }
                lastTitle = title;
            }

            // 磁力链接旁添加复选框,并标记所属分组
            let checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            checkbox.style.marginLeft = "5px";
            checkbox.dataset.group = lastTitle ? lastTitle.innerText.trim() : "";
            link.setAttribute("data-group", checkbox.dataset.group);
            link.after(checkbox);
        });

        // 复制选中的磁力链接
        document.getElementById("copy-selected").addEventListener("click", () => {
            let selectedLinks = [...document.querySelectorAll('a[href^="magnet:?"] + input:checked')]
                .map(cb => cb.previousSibling.href)
                .join("\n");
            if (selectedLinks) {
                GM_setClipboard(selectedLinks);
                alert("已复制选中的磁力链接!");
            } else {
                alert("未选择任何磁力链接!");
            }
        });

        // 打开选中的磁力链接
        document.getElementById("open-selected").addEventListener("click", () => {
            let selectedLinks = [...document.querySelectorAll('a[href^="magnet:?"] + input:checked')]
                .map(cb => cb.previousSibling.href);
            if (selectedLinks.length > 0) {
                selectedLinks.forEach(link => window.open(link, "_blank"));
            } else {
                alert("未选择任何磁力链接!");
            }
        });
    }

    function getNearestTitle(element) {
        let node = element;
        while (node && node !== document.body) {
            if (node.previousElementSibling && /^h[1-6]$/i.test(node.previousElementSibling.tagName)) {
                return node.previousElementSibling;
            }
            node = node.parentElement;
        }
        return null;
    }

    // 监听页面变化,动态添加复选框(适用于 AJAX 加载)
    let observer = new MutationObserver(addCheckboxes);
    observer.observe(document.body, { childList: true, subtree: true });

    // 初次运行
    addCheckboxes();
})();