// ==UserScript== // @name 防止未经授权的自动复制 // @version 6 // @description 在非选词复制时提示用户以防止未经授权的自动复制。 // @grant GM_setClipboard // @run-at document-start // @match *://*/* // @namespace https://greasyfork.org/users/452911 // @downloadURL none // ==/UserScript== (function() { 'use strict'; let hasCopied = false; let allowCopy = true; const handleCopy = function(event) { if (!allowCopy || event.type !== 'copy') return; event.preventDefault(); const selection = window.getSelection().toString(); if (hasCopied || selection.length === 0) { document.execCommand = function() { return true; // 点击取消时,模拟执行成功 }; document.execCommand('copy'); hasCopied = false; // 重置hasCopied标志 document.execCommand = function(command) { return document.execCommandOrig(command); }; return; } const shouldCopy = confirm('是否复制?\n' + selection); if (shouldCopy) { hasCopied = true; GM_setClipboard(selection); } else { document.execCommand = function() { return true; // 点击取消时,模拟执行成功 }; document.execCommand('copy'); hasCopied = false; // 重置hasCopied标志 document.execCommand = function(command) { return document.execCommandOrig(command); }; } }; const handleSelectionChange = function() { allowCopy = false; setTimeout(() => { allowCopy = true; }, 300); const selection = window.getSelection().toString(); if (selection.length > 0) { document.removeEventListener('copy', handleCopy); } else { document.addEventListener('copy', handleCopy); } }; // 保存原始的execCommand函数 document.execCommandOrig = document.execCommand; document.addEventListener('selectionchange', handleSelectionChange); document.addEventListener('copy', handleCopy); })();