Reliable Fast Simulated Typing on Paste

Intercept and replace paste behavior with fast simulated typing (works across most sites)

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reliable Fast Simulated Typing on Paste
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Intercept and replace paste behavior with fast simulated typing (works across most sites)
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const CHUNK_SIZE = 100;

    window.addEventListener('paste', function (e) {
        const target = e.target;

        // Check for editable targets
        const isTextInput = (
            target instanceof HTMLTextAreaElement ||
            (target instanceof HTMLInputElement && target.type === 'text') ||
            target.isContentEditable
        );

        if (!isTextInput) return;

        e.stopImmediatePropagation(); // prevent site handlers
        e.preventDefault(); // cancel native paste

        const text = e.clipboardData.getData('text');
        let index = 0;

        function insertChunk() {
            const chunk = text.slice(index, index + CHUNK_SIZE);

            if (target.isContentEditable) {
                const selection = window.getSelection();
                const range = selection.getRangeAt(0);
                range.deleteContents();
                range.insertNode(document.createTextNode(chunk));
                range.collapse(false);
                selection.removeAllRanges();
                selection.addRange(range);
            } else {
                const start = target.selectionStart;
                const end = target.selectionEnd;
                const before = target.value.slice(0, start);
                const after = target.value.slice(end);
                target.value = before + chunk + after;
                const cursorPos = start + chunk.length;
                target.setSelectionRange(cursorPos, cursorPos);
            }

            target.dispatchEvent(new Event('input', { bubbles: true }));
            index += CHUNK_SIZE;

            if (index < text.length) {
                requestAnimationFrame(insertChunk);
            }
        }

        target.focus();
        insertChunk();
    }, true); // <-- useCapture = true is critical
})();