成人交流 · text · 0 分 · 评论 0 · 阅读 2

JavBus 磁力链接复制

本帖最後由 wlan654321 於 2026-10-5 23:10 編輯

前几天让AI写了一个复制磁力的脚本,今天分享给大家,优点是脚本很轻
效果如下:

需要开启 Tampermonkey 执行用户脚本的权限。Chrome 138 及以上按下面操作:developer.chrome.com
在浏览器地址栏输入 chrome://extensions,回车。找到 Tampermonkey,点击 详情。找到 允许用户脚本(Allow User Scripts),打开开关。回到 JavBus 页面,刷新后再试。

// ==UserScript==
// [url=home.php?mod=space&uid=264574]@name[/url]         JavBus 磁力链接复制
// @namespace    local.javbus.copy-magnet
// [url=home.php?mod=space&uid=361048]@Version[/url]      1.0.0
// @description  在每条磁力名称旁添加复制按钮,支持异步加载列表。
// @match        *://javbus.com/*
// @match        *://*.javbus.com/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// @run-at       document-end
// @noframes
// ==/UserScript==

(function () {
    'use strict';

    const buttonClass = 'javbus-copy-magnet';
    const timers = new WeakMap();
    let scheduled = false;

    GM_addStyle(`
        #magnet-table .${buttonClass} {
            display: inline-block;
            margin: 4px 0;
            padding: 3px 9px;
            border: 1px solid #1686cf;
            border-radius: 4px;
            background: #fff;
            color: #1686cf;
            font: 12px/1.5 sans-serif;
            white-space: nowrap;
            cursor: pointer;
        }
        #magnet-table .${buttonClass}:hover {
            background: #edf7ff;
        }
        #magnet-table .${buttonClass}:disabled {
            cursor: wait;
            opacity: .7;
        }
        #magnet-table .${buttonClass}[data-result="success"] {
            border-color: #268a43;
            color: #268a43;
        }
        #magnet-table .${buttonClass}[data-result="error"] {
            border-color: #c33;
            color: #c33;
        }
    `);

    function showResult(button, message, result) {
        clearTimeout(timers.get(button));
        button.disabled = false;
        button.textContent = message;
        button.dataset.result = result;

        timers.set(button, setTimeout(() => {
            button.textContent = '复制磁力';
            delete button.dataset.result;
            timers.delete(button);
        }, 1800));
    }

    function addButtons() {
        document.querySelectorAll('#magnet-table tr').forEach(row => {
            const link = row.querySelector('a[href^="magnet:"]');

            if (row.querySelector('.javbus-copy-cell')) return;
            if (!link && (row.rowIndex !== 0 || row.cells.length < 3)) return;

            // 在每行最后添加一个单元格
            const cell = row.insertCell(-1);
            cell.className = 'javbus-copy-cell';
            cell.style.textAlign = 'center';
            cell.style.whiteSpace = 'nowrap';

            // 表头
            if (!link) {
                cell.textContent = '操作';
                return;
            }

            const button = document.createElement('button');
            button.type = 'button';
            button.className = buttonClass;
            button.textContent = '复制磁力';
            button.title = '复制本行完整磁力链接';
            cell.appendChild(button);
        });
    }

    // 捕获阶段拦截按钮点击,避免触发单元格上的 window.open。
    document.addEventListener('click', event => {
        if (!(event.target instanceof Element)) return;

        const button = event.target.closest(`button.${buttonClass}`);
        if (!button || !button.closest('#magnet-table')) return;

        event.preventDefault();
        event.stopImmediatePropagation();
        if (button.disabled) return;

        const row = button.closest('tr');
        const link = row && row.querySelector('a[href^="magnet:"]');

        // DOM 自动将 HTML 中的 & 还原为 &。
        const magnet = link && link.getAttribute('href');
        if (!magnet) {
            showResult(button, '链接已失效', 'error');
            return;
        }

        clearTimeout(timers.get(button));
        button.disabled = true;
        button.textContent = '复制中…';
        delete button.dataset.result;

        try {
            GM_setClipboard(magnet, 'text', () => {
                showResult(button, '已复制 ✓', 'success');
            });
        } catch (error) {
            console.error('[JavBus 磁力复制]', error);
            showResult(button, '复制失败,请重试', 'error');
        }
    }, true);

    addButtons();

    // 支持 AJAX 加载、刷新及整张表格替换。
    new MutationObserver(() => {
        if (scheduled) return;
        scheduled = true;

        requestAnimationFrame(() => {
            scheduled = false;
            addButtons();
        });
    }).observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['href']
    });
})();複製代碼

评论 (0)

登录 后参与讨论。

还没有评论,来抢沙发吧。