DeepSeek Ctrl+Enter Send

允许在DeepSeek网页端使用Ctrl+Enter发送,任意其他Enter换行,同时支持底部输入框和历史编辑框

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Advertisement:

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

Advertisement:

// ==UserScript==
// @name         DeepSeek Ctrl+Enter Send
// @name:zh_CN   DeepSeek仅Ctrl+Enter发送
// @namespace    https://greasyfork.org/zh-CN/users/1405279-knighthana
// @author       Knighthana;DeepSeek
// @version      [email protected]
// @description  允许在DeepSeek网页端使用Ctrl+Enter发送,任意其他Enter换行,同时支持底部输入框和历史编辑框
// @match        https://chat.deepseek.com/*
// @icon         https://www.deepseek.com/favicon.ico
// @grant        none
// @license      GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    function isVisible(el) {
        return el && el.offsetParent !== null;
    }

    function findSendButton(textarea) {
        let bestBtn = null;
        let bestDepth = Infinity;
        let parent = textarea.parentElement;
        let depth = 0;
        const MAX_DEPTH = 10;

        while (parent && parent !== document.body && depth < MAX_DEPTH) {
            const btns = parent.querySelectorAll('.ds-button--primary');
            for (const btn of btns) {
                if (!isVisible(btn)) continue;
                const isCircle = btn.classList.contains('ds-button--circle');
                const isCapsuleSend = btn.classList.contains('ds-button--capsule') && btn.textContent.trim() === '发送';
                if (!isCircle && !isCapsuleSend) continue;

                const textareas = parent.querySelectorAll('textarea');
                if (textareas.length === 1 && textareas[0] === textarea) {
                    if (depth < bestDepth) {
                        bestDepth = depth;
                        bestBtn = btn;
                    }
                }
            }
            parent = parent.parentElement;
            depth++;
        }

        if (bestBtn) return bestBtn;

        // 后备全局查找
        const allBtns = document.querySelectorAll('.ds-button--primary');
        for (const btn of allBtns) {
            if (isVisible(btn) &&
                (btn.classList.contains('ds-button--circle') ||
                 (btn.classList.contains('ds-button--capsule') && btn.textContent.trim() === '发送'))) {
                return btn;
            }
        }
        return null;
    }

    function insertNewline(element) {
        if (element.tagName === 'TEXTAREA' || element.tagName === 'INPUT') {
            const start = element.selectionStart;
            const end = element.selectionEnd;
            const value = element.value;
            element.value = value.substring(0, start) + '\n' + value.substring(end);
            element.selectionStart = element.selectionEnd = start + 1;
            element.dispatchEvent(new Event('input', { bubbles: true }));
        } else if (element.isContentEditable) {
            document.execCommand('insertLineBreak');
        }
    }

    function simulateClick(el) {
        if (el) el.click();
    }

    let lastSendTime = 0;

    function handleEnter(event) {
        if (event.key !== 'Enter') return;
        const target = event.target;
        if (target.tagName !== 'TEXTAREA') return;

        event.preventDefault();
        event.stopPropagation();
        event.stopImmediatePropagation();

        if (event.type === 'keydown') {
            if (event.ctrlKey) {
                const now = Date.now();
                if (now - lastSendTime < 300) return;
                lastSendTime = now;

                const btn = findSendButton(target);
                if (btn) {
                    simulateClick(btn);
                } else {
                    console.warn('[脚本] 未找到关联发送按钮');
                }
            } else {
                insertNewline(target);
            }
        }
    }

    document.addEventListener('keydown', handleEnter, true);
    document.addEventListener('keyup', handleEnter, true);
})();