ChatGPT Command+Enter Send

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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();
    }
})();