Greasy Fork

Greasy Fork is available in English.

阻止claude输入发送

解决在输入法状态下按Enter键导致消息直接发送的问题;Solve the problem that pressing the Enter key in the input method state causes the message to be sent directly.

当前为 2025-04-13 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        阻止claude输入发送
// @description 解决在输入法状态下按Enter键导致消息直接发送的问题;Solve the problem that pressing the Enter key in the input method state causes the message to be sent directly.
// @match       https://claude.ai/*
// @author       sinoftj
// @version      1.0.
// @namespace http://greasyfork.icu/users/1457414
// ==/UserScript==

(function() {
    'use strict';
    
    // 输入法状态追踪
    let imeActive = false;
    let lastImeEndTime = 0;
    const IME_BUFFER_TIME = 100; // 输入法结束后的缓冲时间(毫秒)
    
    // 检查是否处于输入法状态
    function isInImeContext(e) {
        return imeActive || 
               e.isComposing || 
               e.keyCode === 229 || 
               (Date.now() - lastImeEndTime < IME_BUFFER_TIME);
    }
    
    // 处理输入法事件
    document.addEventListener('compositionstart', function() {
        imeActive = true;
    }, true);
    
    document.addEventListener('compositionend', function() {
        lastImeEndTime = Date.now();
        setTimeout(() => { imeActive = false; }, IME_BUFFER_TIME);
    }, true);
    
    // 拦截Enter键事件
    document.addEventListener('keydown', function(e) {
        if ((e.key === 'Enter' || e.keyCode === 13) && isInImeContext(e)) {
            e.stopImmediatePropagation();
            e.preventDefault();
            return false;
        }
    }, true);
    
    // 备份保护:keypress事件拦截
    document.addEventListener('keypress', function(e) {
        if ((e.key === 'Enter' || e.keyCode === 13) && isInImeContext(e)) {
            e.stopImmediatePropagation();
            e.preventDefault();
            return false;
        }
    }, true);
    
    // 处理输入框元素
    function protectInputElement(input) {
        const originalKeyDown = input.onkeydown;
        input.onkeydown = function(e) {
            if ((e.key === 'Enter' || e.keyCode === 13) && isInImeContext(e)) {
                e.stopPropagation();
                e.preventDefault();
                return false;
            }
            if (originalKeyDown) return originalKeyDown.call(this, e);
        };
    }
    
    // 页面加载后处理现有输入框
    window.addEventListener('load', function() {
        setTimeout(() => {
            document.querySelectorAll('textarea, input[type="text"]').forEach(protectInputElement);
            
            // 监控DOM变化,处理新添加的输入框
            if (window.MutationObserver) {
                const observer = new MutationObserver((mutations) => {
                    mutations.forEach((mutation) => {
                        if (mutation.addedNodes && mutation.addedNodes.length > 0) {
                            mutation.addedNodes.forEach((node) => {
                                if (node.nodeType === 1) {
                                    if (node.tagName === 'TEXTAREA' || 
                                        (node.tagName === 'INPUT' && node.type === 'text')) {
                                        protectInputElement(node);
                                    }
                                    
                                    const inputs = node.querySelectorAll ? 
                                        node.querySelectorAll('textarea, input[type="text"]') : [];
                                    if (inputs.length > 0) {
                                        inputs.forEach(protectInputElement);
                                    }
                                }
                            });
                        }
                    });
                });
                
                observer.observe(document.body, {
                    childList: true,
                    subtree: true
                });
            }
        }, 1000);
    });
})();