Prevent Paste Hijacking

Prevents websites from hijacking paste events and forces native text insertion. Hides paste event from the website.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Prevent Paste Hijacking
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       none
// @version     1.1
// @description Prevents websites from hijacking paste events and forces native text insertion. Hides paste event from the website.
// @run-at      document-start
// @author      koza.dev
// @license     MIT
// ==/UserScript==



// Hide Ctrl+V keydown from the site unconditionally,
// since we cannot read the clipboard during a keydown event
window.addEventListener('keydown', function(e) {
    if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'v') {
        e.stopImmediatePropagation()
    }
}, true)

window.addEventListener('paste', function(e) {
    if (!e.clipboardData) return

    // If the clipboard contains files (like an image), abort the script
    // and let the website process the paste normally
    if (e.clipboardData.files.length > 0) return

    // If it's just text, hide the event from the website and force native paste
    e.stopImmediatePropagation()
}, true)