默认禁止网页自动写入剪贴板,可通过脚本菜单允许写入剪贴板
// ==UserScript==
// @name 剪贴板保护控制
// @version 1.0
// @description 默认禁止网页自动写入剪贴板,可通过脚本菜单允许写入剪贴板
// @author DeepSeek
// @match *://*/*
// @run-at document-start
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @namespace https://gf.qytechs.cn/users/452911
// ==/UserScript==
(function() {
'use strict';
// 执行剪贴板保护
if (GM_getValue('clipboardProtection', true) !== false) {
enableClipboardProtection();
}
// 注册(不可用)菜单
let enableCmd, disableCmd;
updateMenu();
function updateMenu() {
const isProtected = GM_getValue('clipboardProtection', true);
if (isProtected) {
if (enableCmd) GM_unregisterMenuCommand(enableCmd);
disableCmd = GM_registerMenuCommand("🛡️ 剪贴板保护已启用(点击禁用)", () => {
GM_setValue('clipboardProtection', false);
disableClipboardProtection();
updateMenu();
});
} else {
if (disableCmd) GM_unregisterMenuCommand(disableCmd);
enableCmd = GM_registerMenuCommand("⚠️ 剪贴板保护已禁用(点击启用)", () => {
GM_setValue('clipboardProtection', true);
enableClipboardProtection();
updateMenu();
});
}
}
// 启用剪贴板保护
function enableClipboardProtection() {
['execCommand', 'writeText', 'write'].forEach(method => {
const target = method === 'execCommand' ? document : navigator.clipboard;
try {
Object.defineProperty(target, method, {
value: () => {
console.warn('剪贴板写入已被脚本禁止');
return Promise.reject('剪贴板写入已被脚本禁止');
},
writable: false,
configurable: true
});
} catch (e) {
console.log('剪贴板保护设置失败:', e);
}
});
}
// 禁用剪贴板保护
function disableClipboardProtection() {
['execCommand', 'writeText', 'write'].forEach(method => {
const target = method === 'execCommand' ? document : navigator.clipboard;
try {
delete target[method];
} catch (e) {
console.log('剪贴板保护解除失败:', e);
}
});
}
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址