ChatGPT Command+Enter Send

Send messages with Command+Enter or Ctrl+Enter. Enter and Shift+Enter will create new lines.

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT Command+Enter Send
// @description  Send messages with Command+Enter or Ctrl+Enter. Enter and Shift+Enter will create new lines.
// @author       schweigen
// @license      MIT
// @version      1.0
// @namespace    ChatGPT.CommandEnterSend
// @icon         https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
// @match        https://chatgpt.com
// @match        https://chatgpt.com/?model=*
// @match        https://chatgpt.com/?temporary-chat=*
// @match        https://chatgpt.com/c/*
// @match        https://chatgpt.com/g/*
// @match        https://chatgpt.com/share/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    
    function setupCommandEnterSend() {
        function handleKeyDown(event) {
            if (event.key === 'Enter') {
                // 检查是否在输入框中
                const isPromptTextarea = event.target.matches('#prompt-textarea') || 
                                       event.target.closest('.ProseMirror') || 
                                       event.target.matches('[name="prompt-textarea"]') ||
                                       event.target.closest('[data-testid="composer"]') ||
                                       event.target.closest('[data-testid="textbox"]');
                
                if (!isPromptTextarea) return;
                
                // 支持Command+Enter和Ctrl+Enter两种组合
                const sendKey = event.metaKey || event.ctrlKey; // Command或Ctrl都可以
                
                if (sendKey) {
                    // Command+Enter或Ctrl+Enter: 发送消息
                    // 阻止原事件,创建一个普通的Enter事件来触发发送
                    event.preventDefault();
                    event.stopPropagation();
                    
                    const sendEvent = new KeyboardEvent('keydown', {
                        key: 'Enter',
                        code: 'Enter',
                        keyCode: 13,
                        which: 13,
                        shiftKey: false,
                        ctrlKey: false,
                        altKey: false,
                        metaKey: false,
                        bubbles: true,
                        cancelable: true
                    });
                    
                    // 临时移除监听器,分发事件,然后重新添加
                    document.removeEventListener('keydown', handleKeyDown, true);
                    event.target.dispatchEvent(sendEvent);
                    setTimeout(() => {
                        document.addEventListener('keydown', handleKeyDown, true);
                    }, 0);
                    
                } else {
                    // 单独的Enter或Shift+Enter: 换行
                    // 阻止原事件,创建一个带Shift的Enter事件来换行
                    event.preventDefault();
                    event.stopPropagation();
                    
                    const newlineEvent = new KeyboardEvent('keydown', {
                        key: 'Enter',
                        code: 'Enter',
                        keyCode: 13,
                        which: 13,
                        shiftKey: true, // 强制设为true以确保换行
                        ctrlKey: false,
                        altKey: event.altKey,
                        metaKey: false,
                        bubbles: true,
                        cancelable: true
                    });
                    
                    // 临时移除监听器,分发事件,然后重新添加
                    document.removeEventListener('keydown', handleKeyDown, true);
                    event.target.dispatchEvent(newlineEvent);
                    setTimeout(() => {
                        document.addEventListener('keydown', handleKeyDown, true);
                    }, 0);
                }
            }
        }
        
        // 添加事件监听器
        document.addEventListener('keydown', handleKeyDown, true);
        
        // 返回清理函数
        return () => {
            document.removeEventListener('keydown', handleKeyDown, true);
        };
    }
    
    // 初始化函数
    function init() {
        setupCommandEnterSend();
        console.log('ChatGPT Command+Enter Send initialized');
    }
    
    // 等待DOM加载完成
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init, { once: true });
    } else {
        init();
    }
})();