恢复被网站禁用的复制功能,例如飞书、钉钉、百度文库等。支持右键菜单复制,支持ctrl+c、command+c复制
当前为
// ==UserScript==
// @name 为什么不让我复制?
// @namespace http://tampermonkey.net/
// @version 2.1.2
// @description 恢复被网站禁用的复制功能,例如飞书、钉钉、百度文库等。支持右键菜单复制,支持ctrl+c、command+c复制
// @author HuSheng
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-start
// @license GPL-2.0-only
// ==/UserScript==
(function() {
'use strict';
// 获取用户设置
let enabled = GM_getValue('copy_enabled_hs', true);
let enabled_contextmenu = GM_getValue('copy_enabled_contextmenu_hs', false);
// 注册菜单
GM_registerMenuCommand(enabled ? "✅已启用(点我关闭)" : "❌已禁用(点我开启)", function() {
GM_setValue('copy_enabled_hs', !enabled);
location.reload();
});
if (!enabled){
return
}
GM_registerMenuCommand(enabled_contextmenu ? "✅右键菜单已解锁(点我关闭)" : "❌右键菜单未解锁(点我开启)", function() {
GM_setValue('copy_enabled_contextmenu_hs', !enabled_contextmenu);
location.reload();
});
// 原始事件
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);
};
// 添加右键菜单复制支持
if (enabled_contextmenu) {
document.addEventListener('contextmenu', function(e) {
e.stopImmediatePropagation();
}, true);
}
})();