ChatGPT Ctrl+Enter to Send

在 ChatGPT 网䈎按下 Ctrl+Enter 或 Cmd+Enter 發送訊息,單獨按 Enter 只換行

目前為 2024-07-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name         ChatGPT Ctrl+Enter to Send
// @namespace    http://tampermonkey.net/
// @version      6.1
// @description  在 ChatGPT 网䈎按下 Ctrl+Enter 或 Cmd+Enter 發送訊息,單獨按 Enter 只換行
// @author       SoizoKtantas & ChatGPT
// @match        https://chatgpt.com/*
// @icon         https://www.google.com/s2/favicons?domain=openai.com
// @license      Apache License 2.0
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://gf.qytechs.cn/scripts/411512-gm-createmenu/code/GM_createMenu.js?version=851631
// ==/UserScript==

(function () {
    "use strict";

    // 初始化開關狀態
    let isEnterEnabled = GM_getValue("isEnterEnabled", true);
    let isCtrlEnterEnabled = GM_getValue("isCtrlEnterEnabled", true);

    // 定義開關菜單
    GM_createMenu.add([
        {
            on: {
                name: "啓用 Enter 換行",
                callback: function () {
                    isEnterEnabled = true;
                    GM_setValue("isEnterEnabled", true);
                    //alert("Enter 換行已啓用");
                },
            },
            off: {
                name: "停用 Enter 換行",
                callback: function () {
                    isEnterEnabled = false;
                    GM_setValue("isEnterEnabled", false);
                    //alert("Enter 換行已停用");
                },
            },
            load: function (menuStatus) {
                if (menuStatus === "on") {
                    isEnterEnabled = true;
                } else {
                    isEnterEnabled = false;
                }
            },
            default: isEnterEnabled,
        },
        {
            on: {
                name: "啓用 Ctrl+Enter 發送",
                callback: function () {
                    isCtrlEnterEnabled = true;
                    GM_setValue("isCtrlEnterEnabled", true);
                    //alert("Ctrl+Enter 發送已啓用");
                },
            },
            off: {
                name: "停用 Ctrl+Enter 發送",
                callback: function () {
                    isCtrlEnterEnabled = false;
                    GM_setValue("isCtrlEnterEnabled", false);
                    //alert("Ctrl+Enter 發送已停用");
                },
            },
            load: function (menuStatus) {
                if (menuStatus === "on") {
                    isCtrlEnterEnabled = true;
                } else {
                    isCtrlEnterEnabled = false;
                }
            },
            default: isCtrlEnterEnabled,
        },
    ]);
    GM_createMenu.create({ storage: true });

    // 添加事件監聽器到文檔,使用 capture 階段
    document.addEventListener(
        "keydown",
        function (e) {
            // 獲取焦點元素
            const activeElement = document.activeElement;

            // 檢查焦點是否在 #prompt-textarea 上
            if (activeElement && activeElement.id === "prompt-textarea") {
                if (e.key === "Enter") {
                    if (isCtrlEnterEnabled && (e.ctrlKey || e.metaKey)) {
                        e.preventDefault(); // 防止默認行為
                        e.stopImmediatePropagation(); // 阻止其他事件處理器

                        // 查找發送按鈕
                        const sendButton = document.querySelector(
                            "#__next > div:nth-of-type(1) > div:nth-of-type(2) > main > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > form > div:nth-of-type(1) > div:nth-of-type(2) > div:nth-of-type(1) > div:nth-of-type(1) > button"
                        );

                        if (sendButton) {
                            // 模擬點擊發送按鈕
                            sendButton.click();
                        }
                    } else if (isEnterEnabled) {
                        e.preventDefault(); // 防止默認行為
                        e.stopImmediatePropagation(); // 阻止其他事件處理器
                        // 插入換行
                        const textarea = activeElement;
                        const start = textarea.selectionStart;
                        const end = textarea.selectionEnd;
                        textarea.value =
                            textarea.value.substring(0, start) +
                            "\n" +
                            textarea.value.substring(end);
                        // 將光標位置調整到換行後
                        textarea.selectionStart = textarea.selectionEnd =
                            start + 1;

                        if (textarea.selectionStart === textarea.value.length) {
                            // 滑动到最底部
                            textarea.scrollTop = textarea.scrollHeight;
                        }
                        // 手動觸發 input 事件
                        const event = new Event("input", { bubbles: true });
                        textarea.dispatchEvent(event);
                    }
                }
            }
        },
        true
    ); // 使用 capture 階段
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址