// ==UserScript== // @name 超链接文本复制 // @namespace http://tampermonkey.net/ // @version 1.1 // @description 按住Shift右键点击超链接,可通过隔离样式的复制菜单进行复制文本 // @author Aomine // @match *://*/* // @icon data:image/svg+xml, // @grant GM_setClipboard // @license GPL License // @downloadURL https://update.greasyfork.icu/scripts/551238/%E8%B6%85%E9%93%BE%E6%8E%A5%E6%96%87%E6%9C%AC%E5%A4%8D%E5%88%B6.user.js // @updateURL https://update.greasyfork.icu/scripts/551238/%E8%B6%85%E9%93%BE%E6%8E%A5%E6%96%87%E6%9C%AC%E5%A4%8D%E5%88%B6.meta.js // ==/UserScript== let activeLink = null; document.addEventListener('contextmenu', function(e) { // 检查是否按住 Shift 键 if (!e.shiftKey) return; const link = e.target.closest('a'); if (!link) return; e.preventDefault(); // 阻止默认右键菜单 activeLink = link; // 创建 Shadow DOM 容器(彻底隔离样式) const menuContainer = document.createElement('div'); const shadow = menuContainer.attachShadow({ mode: 'closed' }); // 注入隔离样式 shadow.innerHTML = `
`; // 定位菜单 menuContainer.style.position = 'absolute'; menuContainer.style.left = `${e.pageX}px`; menuContainer.style.top = `${e.pageY}px`; document.body.appendChild(menuContainer); // 绑定事件 shadow.getElementById('copy-text').addEventListener('click', () => { GM_setClipboard(activeLink.innerText.trim()); menuContainer.remove(); }); shadow.getElementById('cancel').addEventListener('click', () => { menuContainer.remove(); }); // 点击其他地方关闭菜单 const closeMenu = (e) => { if (!menuContainer.contains(e.target)) { menuContainer.remove(); document.removeEventListener('click', closeMenu); } }; document.addEventListener('click', closeMenu); }, true);