防止未经授权的自动复制

并在非选词复制时显示小红点提示用户以防止未经授权的自动复制,脚本菜单还加入了禁止写入剪贴板功能

// ==UserScript==
// @name         防止未经授权的自动复制
// @version      25
// @description  并在非选词复制时显示小红点提示用户以防止未经授权的自动复制,脚本菜单还加入了禁止写入剪贴板功能
// @author       ChatGPT
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// @run-at       document-start
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==

(function() {
    'use strict';

    // 获取当前域名
    const domain = window.location.hostname;

    // 获取当前域名对应的脚本启用状态
    let isEnabledClipboard = GM_getValue(domain + '_clipboard', false);
    let isEnabledAutoCopy = GM_getValue(domain + '_autocopy', true);

    // 注册菜单命令
    GM_registerMenuCommand((isEnabledClipboard ? '允许' : '禁止') + '写入剪贴板', () => {
        isEnabledClipboard = !isEnabledClipboard; // 切换启用状态
        GM_setValue(domain + '_clipboard', isEnabledClipboard); // 保存启用状态
        location.reload(); // 刷新页面使变更生效
    });

    GM_registerMenuCommand((isEnabledAutoCopy ? '禁用' : '启用') + '复制监听', () => {
        isEnabledAutoCopy = !isEnabledAutoCopy;
        GM_setValue(domain + '_autocopy', isEnabledAutoCopy);
        location.reload();
    });

    // 如果脚本启用,禁用剪贴板相关操作
    if (isEnabledClipboard) {
        ['execCommand', 'writeText', 'write'].forEach(method => {
            const target = method === 'execCommand' ? document : navigator.clipboard;
            Object.defineProperty(target, method, {
                value: () => {},
                writable: false,
                configurable: false
            });
        });
    }

    if (!isEnabledAutoCopy) return; // 如果脚本被禁用,则不执行以下代码

    let hasCopied = false;
    let timeoutId = null;
    let dot = null;

    const handleCopy = function(event) {
        event.preventDefault();
        const selection = window.getSelection().toString();
        if (!hasCopied && selection.trim().length > 0) {
            hasCopied = true;
            createDot(selection);
        }
    };

    const createDot = function(selection) {
        dot = document.createElement('div');
        dot.style.width = '20px';
        dot.style.height = '20px';
        dot.style.zIndex = '9999';
        dot.style.background = 'rgba(255, 0, 0, 0.2)';
        dot.style.borderRadius = '50%';
        dot.style.position = 'fixed';
        dot.style.top = '50%';
        dot.style.right = '10px';
        dot.style.transform = 'translateY(-50%)';
        dot.style.cursor = 'pointer';
        dot.addEventListener('click', function() {
            const shouldCopy = confirm(selection);
            if (shouldCopy) {
                if (typeof GM_setClipboard === "function") {
                    GM_setClipboard(selection);
                } else {
                    copyToClipboard(selection);
                }
            }
            document.body.removeChild(dot);
            hasCopied = false;
        });
        document.body.appendChild(dot);

        timeoutId = setTimeout(function() {
            if (dot && dot.parentNode) {
                document.body.removeChild(dot);
            }
            hasCopied = false;
            timeoutId = null;
        }, 4000);
    };

    document.addEventListener('selectionchange', handleSelectionChange);

    const copyToClipboard = function(text) {
        const textArea = document.createElement('textarea');
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.select();
        document.execCommand('copy');
        document.body.removeChild(textArea);
    };

    navigator.clipboard.writeText = function(text) {
        return new Promise((resolve, reject) => {
            copyToClipboard(text);
            resolve();
        });
    };

    document.addEventListener('copy', handleCopy, { capture: true });

    window.addEventListener('beforeunload', function() {
        if (timeoutId) {
            clearTimeout(timeoutId);
        }
    });

    let startX = null;
    let startY = null;

    document.addEventListener('touchstart', function(e) {
        startX = e.touches[0].clientX;
        startY = e.touches[0].clientY;
    });

    document.addEventListener('touchmove', function(e) {
        const currentX = e.touches[0].clientX;
        const currentY = e.touches[0].clientY;
        const diffX = Math.abs(currentX - startX);
        const diffY = Math.abs(currentY - startY);

        if ((diffX > 10 || diffY > 10) && dot) {
            document.body.removeChild(dot);
            hasCopied = false;
            if (timeoutId) {
                clearTimeout(timeoutId);
                timeoutId = null;
            }
        }
    });

    function handleSelectionChange() {
        if (window.getSelection().toString().trim().length === 0) {
            document.addEventListener('copy', handleCopy, { capture: true });
        } else {
            document.removeEventListener('copy', handleCopy, { capture: true });
        }
    }
})();