Swap Ctrl+Enter and Enter

Swapping the input of Ctrl+Enter and Enter is particularly useful for websites that use the Enter key to send messages.

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 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         交换 Ctrl+Enter 和 Enter
// @name:en      Swap Ctrl+Enter and Enter
// @version      1.1
// @description  交换输入的Ctrl+Enter和Enter,对于某些使用回车发送的网站很有用。默认配置DeepSeek、飞书。可以自己增加额外的网站。
// @description:en  Swapping the input of Ctrl+Enter and Enter is particularly useful for websites that use the Enter key to send messages.
// @author       DeepSeek / SnDream
// @match        https://chat.deepseek.com/*
// @match        https://*.feishu.cn/next/messenger*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/466574
// ==/UserScript==


(function () {
    'use strict';

    let isSimulated = false; // 标记是否在模拟事件中

    function handleEnterKey(e) {
        // 如果是模拟事件则跳过
        if (isSimulated) return;

        // 仅处理Enter键
        if (e.key !== 'Enter') return;

        // 阻止默认行为和事件传播
        e.preventDefault();
        e.stopImmediatePropagation();

        // 设置标记防止递归
        isSimulated = true;

        // 创建模拟事件(反转Ctrl状态)
        const simulatedEvent = new KeyboardEvent('keydown', {
            key: 'Enter',
            code: 'Enter',
            keyCode: 13,
            ctrlKey: !(e.ctrlKey || e.metaKey), // 反转Ctrl状态
            bubbles: true,
            cancelable: true,
            composed: true
        });

        // 分派模拟事件
        e.target.dispatchEvent(simulatedEvent);

        // 重置标记
        isSimulated = false;
    }

    document.addEventListener('keydown', handleEnterKey, true);
})();