Greasy Fork

Greasy Fork is available in English.

磁力链接一键复制工具

为页面中的磁力链接添加复制按钮,点击即可复制

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         磁力链接一键复制工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  为页面中的磁力链接添加复制按钮,点击即可复制
// @author       豆包编程助手
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建复制提示框样式
    const style = document.createElement('style');
    style.textContent = `
        .magnet-copy-btn {
            margin-left: 5px;
            padding: 2px 6px;
            border: none;
            border-radius: 3px;
            background: #4CAF50;
            color: white;
            font-size: 12px;
            cursor: pointer;
        }
        .magnet-copy-btn:hover {
            background: #45a049;
        }
        .copy-toast {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 8px 16px;
            background: #333;
            color: white;
            border-radius: 4px;
            opacity: 0;
            transition: opacity 0.3s;
            pointer-events: none;
        }
        .copy-toast.show {
            opacity: 1;
        }
    `;
    document.head.appendChild(style);

    // 创建复制提示框
    const toast = document.createElement('div');
    toast.className = 'copy-toast';
    toast.textContent = '复制成功!';
    document.body.appendChild(toast);

    // 显示提示
    function showToast() {
        toast.classList.add('show');
        setTimeout(() => {
            toast.classList.remove('show');
        }, 2000);
    }

    // 复制文本到剪贴板
    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(showToast).catch(err => {
            console.error('复制失败:', err);
        });
    }

    // 为所有磁力链接添加复制按钮
    function addCopyButtons() {
        // 查找所有磁力链接(a标签且href以magnet:开头)
        const magnetLinks = document.querySelectorAll('a[href^="magnet:"]');
        
        magnetLinks.forEach(link => {
            // 避免重复添加按钮
            if (!link.nextElementSibling || !link.nextElementSibling.classList.contains('magnet-copy-btn')) {
                const btn = document.createElement('button');
                btn.className = 'magnet-copy-btn';
                btn.textContent = '复制';
                btn.addEventListener('click', (e) => {
                    e.preventDefault();
                    copyToClipboard(link.href);
                });
                
                // 在磁力链接后面添加复制按钮
                link.parentNode.insertBefore(btn, link.nextSibling);
            }
        });
    }

    // 初始加载时添加按钮
    addCopyButtons();

    // 监听页面变化,动态添加按钮(应对动态加载的内容)
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) {
                addCopyButtons();
            }
        });
    });

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