Greasy Fork

全局监测网页中的磁力链接并复制到剪贴板

监测网页中的磁力链接并复制到剪贴板

目前为 2025-04-03 提交的版本。查看 最新版本

// ==UserScript==
// @name        全局监测网页中的磁力链接并复制到剪贴板
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  监测网页中的磁力链接并复制到剪贴板
// @match        *://*/*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    // 磁力链接正则表达式(支持大小写和常见参数)
    const MAGNET_REGEX = /magnet:\?xt=urn:btih:[a-fA-F0-9]{40}[^\s"'<>]*/g;
    let foundMagnets = new Set();
    let observer;

    // 创建悬浮通知面板
    const panel = document.createElement('div');
    panel.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        background: rgba(0,0,0,0.8);
        color: #fff;
        padding: 15px;
        border-radius: 5px;
        z-index: 999999;
        font-family: Arial, sans-serif;
        box-shadow: 0 2px 10px rgba(0,0,0,0.3);
        display: none;
    `;

    // 创建复制按钮
    const copyBtn = document.createElement('button');
    copyBtn.textContent = '复制磁力链接';
    copyBtn.style.cssText = `
        background: #2196F3;
        border: none;
        color: white;
        padding: 8px 15px;
        border-radius: 3px;
        cursor: pointer;
        margin-top: 10px;
    `;

    // 创建计数器显示
    const counter = document.createElement('div');
    counter.textContent = '已发现 0 个磁力链接';
    panel.appendChild(counter);
    panel.appendChild(copyBtn);
    document.body.appendChild(panel);

    // 更新界面显示
    function updateUI() {
        counter.textContent = `已发现 ${foundMagnets.size} 个磁力链接`;
        panel.style.display = foundMagnets.size ? 'block' : 'none';
    }

    // 扫描磁力链接
    function scanForMagnets() {
        // 扫描文本内容
        const textContent = document.body.textContent;
        let match;
        while ((match = MAGNET_REGEX.exec(textContent)) !== null) {
            foundMagnets.add(match.trim());
        }

        // 扫描链接属性
        const links = document.querySelectorAll('a[href]');
        links.forEach(link => {
            const href = link.href;
            if (MAGNET_REGEX.test(href)) {
                foundMagnets.add(href);
            }
        });

        updateUI();
    }

    // 初始化 MutationObserver
    function initObserver() {
        observer = new MutationObserver((mutations) => {
            mutations.forEach(() => scanForMagnets());
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true,
            attributes: true,
            characterData: true
        });
    }

    // 复制按钮点击事件
    copyBtn.addEventListener('click', () => {
        if (foundMagnets.size) {
            const links = Array.from(foundMagnets).join('\n');
            GM_setClipboard(links);
            panel.style.background = 'rgba(67,160,71,0.9)';
            setTimeout(() => {
                panel.style.background = 'rgba(0,0,0,0.8)';
            }, 1000);
        }
    });

    // 初始扫描和观察
    scanForMagnets();
    initObserver();

    // 防抖处理页面变化
    let resizeTimer;
    window.addEventListener('resize', () => {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(scanForMagnets, 200);
    });
})();