蜜柑計劃 一鍵複製全部磁連(短版)

在各字幕組分區新增一鍵『複製磁連』按鈕

当前为 2025-09-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         蜜柑計劃 一鍵複製全部磁連(短版)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  在各字幕組分區新增一鍵『複製磁連』按鈕
// @author       shanlan(ChatGPT o3-mini)
// @match        https://mikanani.me/Home/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mikanani.me
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
'use strict';

function notify(msg) {
    const n = document.createElement('div');
    n.style.position = 'fixed';
    n.style.left = '50%';
    n.style.top = '50%';
    n.style.transform = 'translate(-50%, -50%)';
    n.style.backgroundColor = 'rgba(0,0,0,0.7)';
    n.style.color = 'white';
    n.style.padding = '12px 24px';
    n.style.borderRadius = '8px';
    n.style.zIndex = '9999';
    n.style.fontSize = '30px';
    n.style.boxShadow = '0 2px 12px rgba(0,0,0,0.2)';
    n.textContent = msg;
    document.body.appendChild(n);
    setTimeout(() => {
        n.style.transition = 'opacity 1s';
        n.style.opacity = '0';
        setTimeout(() => { n.remove(); }, 1000);
    }, 3000);
}

function main() {
    const subgroupDivs = document.querySelectorAll('div.subgroup-text, span.header2-text');
    subgroupDivs.forEach(div => {
        const btn = document.createElement('button');
        btn.textContent = '複製磁連';
        btn.style.marginLeft = '10px';
        btn.style.padding = '2px 6px';
        btn.style.fontSize = '12px';
        // 避免重複加入(如果頁面原本就有按鈕)
        if (![...div.children].some(c=>c.tagName === 'BUTTON' && c.textContent.includes('複製磁連'))) {
            div.appendChild(btn);
        }

        btn.addEventListener('click', () => {
            // 更穩健的尋找 table 方法:
            // 1. 在同父元素內取出所有 table,找出位在此 div 之後 (FOLLOWING) 的第一個 table
            // 2. 若找不到,再找有 a.js-magnet 的 table 作備援
            let table = null;
            const parent = div.parentElement || document;
            const tables = Array.from(parent.querySelectorAll('table'));
            table = tables.find(t => (div.compareDocumentPosition(t) & Node.DOCUMENT_POSITION_FOLLOWING));
            if (!table) {
                table = tables.find(t => t.querySelector('a.js-magnet'));
            }
            // 最後備援:全文件裡第一個含 js-magnet 的 table
            if (!table) {
                table = document.querySelector('table table, table'); // quick fallback
                // 更精確一點的 fallback
                const anyWithMagnet = Array.from(document.querySelectorAll('table')).find(t => t.querySelector('a.js-magnet'));
                if (anyWithMagnet) table = anyWithMagnet;
            }

            if (!table) {
                console.log('未找到對應的磁連列表 (table)');
                notify('未找到對應的磁連列表,請稍候或重新載入頁面');
                return;
            }

            // 從該 table 中搜尋所有 class 為 js-magnet 的 a 元素 (並備援 href)
            const magnetAnchors = table.querySelectorAll('a.js-magnet, a.magnet-link, a[href^="magnet:"]');
            const magnets = [];
            magnetAnchors.forEach(anchor => {
                // 優先 data-clipboard-text,沒有就用 href
                const fullText = anchor.getAttribute('data-clipboard-text') || anchor.getAttribute('href') || '';
                if (fullText) {
                    const match = fullText.match(/^(magnet:\?xt=urn:btih:[^&]+)/);
                    if (match) {
                        magnets.push(match[1]);
                    } else {
                        // 若沒有 &,但整串就是 magnet,接受整串
                        if (fullText.startsWith('magnet:')) magnets.push(fullText);
                    }
                }
            });

            if (magnets.length === 0) {
                console.log('未找到任何磁連!');
                notify('未找到任何磁連');
                return;
            }

            const result = magnets.join('\n');
            if (typeof GM_setClipboard !== 'undefined') {
                GM_setClipboard(result, 'text');
            } else {
                const textarea = document.createElement('textarea');
                textarea.value = result;
                document.body.appendChild(textarea);
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
            }
            notify('已複製 ' + magnets.length + ' 個磁連,如果有沒複製到的請點擊右下角『顯示更多』');
        });
    });
}

if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', main);
} else {
    main();
}
})();