Greasy Fork

Greasy Fork is available in English.

阻止阻止复制

干烂剪切板接口,这样就不能阻止我复制了

目前为 2021-06-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         阻止阻止复制
// @namespace    https://lab.wsl.moe/
// @version      0.2
// @description  干烂剪切板接口,这样就不能阻止我复制了
// @author       MisakaMikoto
// @match        http://*/*
// @match        https://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {

    const realExecCommand = document.execCommand;
    const realAddEventListener = Element.prototype.addEventListener;

    let userSelected = false;
    let userAllowed = false;

    const setExecuteCommand = () => {
        window.Clipboard = undefined;
        try {
            navigator.clipboard.writeText = undefined;
        } catch (e) {}
        if (document && document.oncopy) {
            if (!userSelected) {
                userSelected = true;
                userAllowed = confirm('该网站希望在全局页面上添加复制监听器,是否允许?');
            }
            if (!userAllowed) {
                console.warn('Permission denied to control clipboard.');
                document.oncopy = null;
                return;
            }
        }
        if (document && document.body && document.body.oncopy) {
            if (!userSelected) {
                userSelected = true;
                userAllowed = confirm('该网站希望在全局页面上添加复制监听器,是否允许?');
            }
            if (!userAllowed) {
                console.warn('Permission denied to control clipboard.');
                document.body.oncopy = undefined;
                return;
            }
        }
        Document.prototype.execCommand = (cmd) => {
            switch (cmd) {
                case "copy":
                case "cut":
                case "paste":
                    if (!userSelected) {
                        userSelected = true;
                        userAllowed = confirm('该网站希望请求一次修改剪贴板的权限,是否允许?');
                    }
                    if (!userAllowed) {
                        console.warn('Permission denied to control clipboard.');
                        return;
                    }
                    break;
            }
            realExecCommand(cmd);
        };
        Element.prototype.realAddEventListener = realAddEventListener;
        Element.prototype.addEventListener = function(t, p) {
            //const functionCode = p.toString();
            switch (t) {
                case "copy":
                case "cut":
                case "paste":
                    if (!userSelected) {
                        userSelected = true;
                        userAllowed = confirm('该网站希望在某个元素上添加复制事件监听器,是否允许?');
                    }
                    if (!userAllowed) {
                        console.warn('Permission denied to control clipboard.');
                        return;
                    }
                    break;
            }
            return this.realAddEventListener(t, p);
        }
    };
    setExecuteCommand();
    const intervalId = setInterval(setExecuteCommand, 1000);
})();