Greasy Fork

Greasy Fork is available in English.

剪贴板保护控制

默认禁止网页自动写入剪贴板,可通过脚本菜单允许写入剪贴板

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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 http://greasyfork.icu/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);
            }
        });
    }
})();