Greasy Fork

Greasy Fork is available in English.

为什么不让我复制?(解除如飞书、钉钉、百度文库等的复制限制)

恢复被网站禁用的复制功能,例如飞书、钉钉、百度文库等。支持右键菜单复制,支持ctrl+c、command+c复制

// ==UserScript==
// @name         为什么不让我复制?(解除如飞书、钉钉、百度文库等的复制限制)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  恢复被网站禁用的复制功能,例如飞书、钉钉、百度文库等。支持右键菜单复制,支持ctrl+c、command+c复制
// @author       HuSheng
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @license      GPL-2.0-only
// ==/UserScript==

(function() {
    'use strict';

    // 原始事件
    const originalAddEventListener = EventTarget.prototype.addEventListener;
    const originalPreventDefault = Event.prototype.preventDefault;
    const originalStopPropagation = Event.prototype.stopPropagation;
    const originalStopImmediatePropagation = Event.prototype.stopImmediatePropagation;

    // 覆盖addEventListener
    EventTarget.prototype.addEventListener = function(type, listener, options) {
        if (type === 'copy') {
            return;
        }

        // 拦截Ctrl+C、Command+C
        if (type === 'keydown') {
            const listenerStr = listener.toString();
            if (listenerStr.includes('keyCode:67') ||
                listenerStr.includes('keyCode===67') ||
                listenerStr.includes('key:"c"') ||
                (listenerStr.includes('ctrlKey') && listenerStr.includes('67')) ||
                (listenerStr.includes('metaKey') && listenerStr.includes('67'))) {
                return;
            }
        }
        originalAddEventListener.call(this, type, listener, options);
    };

    // 覆盖事件方法
    Event.prototype.preventDefault = function() {
        if (this.type === 'copy' || (this.type === 'keydown' && this.keyCode === 67 && (this.ctrlKey || this.metaKey))) {
            return;
        }
        originalPreventDefault.call(this);
    };

    Event.prototype.stopPropagation = function() {
        if (this.type === 'copy' || (this.type === 'keydown' && this.keyCode === 67 && (this.ctrlKey || this.metaKey))) {
            return;
        }
        originalStopPropagation.call(this);
    };

    Event.prototype.stopImmediatePropagation = function() {
        if (this.type === 'copy' || (this.type === 'keydown' && this.keyCode === 67 && (this.ctrlKey || this.metaKey))) {
            return;
        }
        originalStopImmediatePropagation.call(this);
    };

    // 添加右键菜单复制支持
    document.addEventListener('contextmenu', function(e) {
        e.stopImmediatePropagation();
    }, true);
})();