// ==UserScript== // @name 自动复制 // @name:zh 自动复制 // @name:en Auto-Copy // @namespace gura8390/copy/1 // @version 2.1 // @license MIT // @description 自动复制,鼠标选中,直接复制,省去你的左手。 // @description:en Automatically copy, just select with the mouse and it copies, saving your left hand // @author lbihhe // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAilBMVEX////YHybXGCDWAAnXCBTXDRfplZfxvb7mhondS1DVAADWAA3XFB3usrTgY2bjcnXrpabham366ur219jbOj/lgIL99PTqm53++fn44OHokZPvt7jyxsf32tv0zs/zyMnniozaNDncR0veV1vtqqzfWl755ubroaPZKC/haGvcQUXkeXvZKzHicXSjlFikAAAHOklEQVR4nO2baXviOgyFs5nFdoCyQ9la1int//97N4GSSI6zMDN9IHPP+20S1+g4siQrGccBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8nGU4adScY7OfK2/a8KQKao8vh6FdYCgDz/0n8OTn0iLwLC83Rd3R3kVj9jF2/eiGkMNzq+Z8uVJHUuSrIXAXP0E5yt+kNWKwvYiZsYvjy7XVg0z660wiOd6QXWqJSODgQfb8AI1oz8kmvRJpDjYPsuZHePdcvSX/XkcK5fRh5vwApqJJ4OrO48z5CSSPK2fhBo3HWfMTtD3XJwmjq92g9zhrfoKOdhXJ+hUU9tdhYzMatRq75qx4ZMyy2Zu3RqPNJFw/KMXeqbDZFTIqyuOiKFC+lJ2wICz1j20p/eB7tJSi27SM2itbyeW9bUfhIR3VkuSm/2XMMQ3o3/IUf4/C6dz3BS9ttZKtsX30aiuVUcBrX7YyT7Kj7UWzFkoO0w30SecyM/YooDeP5vxVFfZkYJrhXkpY2xGlv5dWy4WcV1N4lSmHtxWZSXr9jU3B771n5q+mcNqW5u8ni3bOjG7a9cWo04ENLVIYa7wdf+aKXPaZke/s+RpeUlXh2BXmj5PfezFGN3JX42I0K3uLFUajbxa/MR1kb7zSXzN8tLLCsVd4JpYbNnp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jQLtL6duFZ1DIQiBJr6y3lHSiaHJxxXCWjmazJA78FAp5E9v/nKyXy/XxjbfgbsGPtaLinB0OZrPVhI/20gL3KRTyDmEcVTJHnCDtpXzxdzhaWTrCpIPzHApntkMngySGafmXjbTL+BwKr1+7FSBpYTMoWw+PHsKeRKEzKrTaeLv2WizRU1TIsyh0ugVWy64xmMdNA+EyHU+j0GnkWe0ZTzBmIKzv/C/2GAei51HoDE5WjUrYjjjTkbS9FvdUYH7Q+kQKo7w4lIqd5jwh1SRncL/lS2FmmVN28n2gE6orHPnkr/6ewiht9Doiym6+UvH3QvJ0zj2ixqw37/Hg2+h2w2bK5tdLwr6ywl4n/atO4bdZ93/15Yxnq2YYhs31sspnmv3BIhr92hw86rvq31BYM/53CuPvS81PbmrO1nMVid/RkVab6bvmaP61T/w2tLTrWyuWkvdA40aSX/YWtFa8aOM/XMQVhip7SVgj4g6MYk3/+KF6n/+MxMtBzth2IxWXV7bPlWvI5StQ84sQZxjXtPK0CZs1Z3f24wOOzLx/nA6DS02t/JqjLsePzNk1plt0fq0Xwvqpm+Os2lIJ7dUcLXzZza33D7vztl1z9q3XfyYnAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQX/4DUVSWG2ZqFvwAAAAASUVORK5CYII= // @match *://*/* // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant GM_addStyle // @downloadURL none // ==/UserScript== (function() { 'use strict'; const STORAGE_KEY = 'auto_copy_enabled'; let isEnabled = GM_getValue(STORAGE_KEY, true); // 用于记录当前油猴菜单命令ID(便于更新) let menuCmdId = null; const updateMenuCommand = () => { if (menuCmdId !== null) { GM_unregisterMenuCommand(menuCmdId); menuCmdId = null; } if (document.querySelector('.control-panel')) { menuCmdId = GM_registerMenuCommand('关闭控制面板', () => { const panel = document.querySelector('.control-panel'); if (panel) { panel.remove(); updateMenuCommand(); } }); } else { menuCmdId = GM_registerMenuCommand('打开控制面板', () => { if (!document.querySelector('.control-panel')) { document.body.appendChild(createControlPanel()); updateMenuCommand(); } }); } }; // ======================== // 美观的视觉反馈系统 // ======================== const createFeedback = (text, e) => { const feedback = document.createElement('div'); feedback.innerHTML = `
${isEnabled ? '' : ''} ${text}
`; Object.assign(feedback.style, { position: 'fixed', left: `${Math.min(e.clientX + 10, window.innerWidth - 200)}px`, top: `${e.clientY + 20}px`, zIndex: 2147483647, pointerEvents: 'none' }); document.body.appendChild(feedback); setTimeout(() => feedback.remove(), 2000); }; // ======================== // 状态指示图标系统(右下角悬浮菜单) // ======================== const createStatusIcon = () => { const icon = document.createElement('div'); icon.className = 'status-icon'; icon.innerHTML = ` `; // 初始样式(默认贴边隐藏一部分,鼠标悬停时按钮需立即定位到正确位置) Object.assign(icon.style, { position: 'fixed', bottom: '20px', right: '-10px', // 部分隐藏 width: '40px', height: '40px', cursor: 'pointer', zIndex: 2147483647, transition: 'right 0.3s ease, transform 0.3s ease, opacity 0.3s ease', opacity: '0.8' }); // 点击事件:若控制面板存在则关闭之,否则切换自动复制状态 icon.addEventListener('click', () => { const panel = document.querySelector('.control-panel'); if (panel) { panel.remove(); updateMenuCommand(); } else { toggleFeature(); } }); // 鼠标移入时:直接瞬移到目标位置(取消过渡) icon.addEventListener('mouseenter', () => { icon.style.transition = 'none'; icon.style.right = '20px'; icon.style.transform = 'scale(1.1)'; icon.style.opacity = '1'; }); // 鼠标离开时:恢复过渡效果 icon.addEventListener('mouseleave', () => { icon.style.transition = 'right 0.3s ease, transform 0.3s ease, opacity 0.3s ease'; icon.style.right = '-10px'; icon.style.transform = 'scale(1)'; icon.style.opacity = '0.8'; }); updateIconStyle(icon); return icon; }; const updateIconStyle = (icon) => { const color = isEnabled ? '#4CAF50' : '#F44336'; icon.style.filter = `drop-shadow(0 2px 4px ${color}40)`; icon.querySelector('.status-indicator').style.stroke = color; icon.querySelector('.status-base').style.stroke = color; }; // ======================== // 美观的控制面板 // ======================== const createControlPanel = () => { const panel = document.createElement('div'); panel.className = 'control-panel'; panel.innerHTML = `

智能复制控制

×
当前状态: ${isEnabled ? '✓ 运行中' : '✗ 已暂停'}
`; panel.querySelector('.toggle-btn').addEventListener('click', toggleFeature); panel.querySelector('.close-btn').addEventListener('click', () => { // 采用滑出动画后再移除 panel.style.transform = 'translateY(100%)'; setTimeout(() => { panel.remove(); updateMenuCommand(); }, 300); }); return panel; }; // ======================== // 核心功能逻辑 // ======================== const handleSelection = (e) => { if (!isEnabled) return; const selection = window.getSelection().toString().trim(); if (!selection) return; navigator.clipboard.writeText(selection) .then(() => createFeedback('文本已复制', e)) .catch(err => { createFeedback('复制失败', e); console.error('复制失败:', err); }); }; const toggleFeature = () => { isEnabled = !isEnabled; GM_setValue(STORAGE_KEY, isEnabled); updateIconStyle(statusIcon); showGlobalNotification(); refreshControlPanel(); }; // ======================== // 辅助功能 // ======================== const showGlobalNotification = () => { const notification = document.createElement('div'); notification.className = 'global-notification'; notification.textContent = `自动复制功能已${isEnabled ? '启用' : '禁用'}`; document.body.appendChild(notification); setTimeout(() => notification.remove(), 1500); }; const refreshControlPanel = () => { document.querySelector('.control-panel')?.remove(); document.body.appendChild(createControlPanel()); updateMenuCommand(); }; // ======================== // 初始化系统 // ======================== GM_addStyle(` /* 反馈提示样式 */ .feedback-container { background: linear-gradient(145deg, #f8f9fa, #ffffff); border-radius: 12px; padding: 12px 16px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); display: flex; align-items: center; gap: 8px; animation: slideIn 0.3s ease, fadeOut 0.5s ease 1.5s forwards; } .feedback-icon { width: 20px; height: 20px; color: ${isEnabled ? '#4CAF50' : '#F44336'}; } .feedback-text { color: #2d3436; font-family: -apple-system, system-ui; font-size: 14px; } @keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } /* 状态图标样式 */ .status-icon { /* 样式已在内联设置 */ } /* 控制面板样式 */ .control-panel { position: fixed; bottom: 80px; right: 20px; background: rgba(255,255,255,0.95); border-radius: 16px; box-shadow: 0 8px 24px rgba(0,0,0,0.1); padding: 16px; width: 240px; transform: translateY(0); transition: transform 0.3s ease; backdrop-filter: blur(8px); } .panel-header { position: relative; padding-bottom: 8px; border-bottom: 1px solid #e0e0e0; margin-bottom: 12px; } .panel-header h3 { margin: 0; font-size: 16px; } .close-btn { position: absolute; top: 4px; right: 4px; font-size: 20px; cursor: pointer; user-select: none; } .toggle-btn { background: #e0e0e0; border: none; padding: 8px 24px; border-radius: 20px; cursor: pointer; transition: all 0.3s ease; } .toggle-btn.active { background: #4CAF50; color: white; } .status-indicator-text { margin-top: 12px; font-size: 14px; color: #555; } /* 全局通知样式 */ .global-notification { position: fixed; top: 20px; left: 50%; transform: translateX(-50%); background: #333; color: #fff; padding: 8px 16px; border-radius: 8px; z-index: 2147483647; opacity: 0.9; } `); // 挂载核心元素 const statusIcon = createStatusIcon(); document.body.appendChild(statusIcon); document.addEventListener('mouseup', handleSelection); // 初始化时更新菜单 updateMenuCommand(); })();