// ==UserScript== // @name 自动转换 ed2k 和磁力链接并一键复制 // @namespace http://tampermonkey.net/ // @version 1.2 // @description 自动检测网页中的所有 ed2k 和磁力链接并将其转换为可点击的超链接,同时提供一键复制所有链接的功能 // @author 98-liu** // @match *://*/* // @grant GM_setClipboard // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 自动检测并转换 ed2k 和磁力链接为超链接 function convertLinks() { // 正则表达式匹配 ed2k 链接 const ed2kRegex = /ed2k:\/\/\|file\|[^\|]+\|\d+\|[a-fA-F0-9]+\|\//g; // 正则表达式匹配磁力链接 const magnetRegex = /magnet:\?xt=urn:[a-zA-Z0-9:.&=]+/g; // 遍历网页中的所有文本节点 const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false); let node; while (node = walker.nextNode()) { if (node.nodeValue.match(ed2kRegex) || node.nodeValue.match(magnetRegex)) { const parent = node.parentNode; // 避免重复处理(如果已经是超链接则跳过) if (parent.tagName !== 'A') { let newHTML = node.nodeValue; newHTML = newHTML.replace(ed2kRegex, '$&'); newHTML = newHTML.replace(magnetRegex, '$&'); const temp = document.createElement('div'); temp.innerHTML = newHTML; // 将新内容插入到 DOM 中 while (temp.firstChild) { parent.insertBefore(temp.firstChild, node); } // 移除原始文本节点 parent.removeChild(node); } } } } // 获取网页中的所有 ed2k 和磁力链接 function getAllLinks() { const ed2kRegex = /ed2k:\/\/\|file\|[^\|]+\|\d+\|[a-fA-F0-9]+\|\//g; const magnetRegex = /magnet:\?xt=urn:[a-zA-Z0-9:.&=]+/g; const textContent = document.body.textContent; const ed2kLinks = textContent.match(ed2kRegex) || []; const magnetLinks = textContent.match(magnetRegex) || []; return [...ed2kLinks, ...magnetLinks].join('\n'); } // 创建一键复制按钮 function createCopyButton() { const button = document.createElement('button'); button.textContent = '复制所有链接'; button.style.position = 'fixed'; button.style.top = '10px'; button.style.right = '10px'; button.style.zIndex = '10000'; button.style.padding = '10px'; button.style.backgroundColor = '#007bff'; button.style.color = '#fff'; button.style.border = 'none'; button.style.borderRadius = '5px'; button.style.cursor = 'pointer'; // 点击按钮时复制所有链接 button.addEventListener('click', () => { const links = getAllLinks(); if (links) { GM_setClipboard(links); alert('已复制所有链接到剪贴板!'); } else { alert('未找到链接!'); } }); document.body.appendChild(button); } // 在页面加载完成后执行 window.addEventListener('load', () => { convertLinks(); createCopyButton(); }); // 监听动态内容加载(例如 AJAX) const observer = new MutationObserver(() => { convertLinks(); }); observer.observe(document.body, { childList: true, subtree: true }); })();