拦截豆包的回车键

GreaseMonkey脚本,自己写的,在firefox里暂时能用。

本人不护责维护,不回答问题,也不声明版权,Good Luck。

行为:

  1. 回车不发送内容,而是在输入的textarea里换行;
  2. Control+回车可以发送内容;

已知bug:

  1. 如果输入框旁边出现诸如“AI编程”之类的tag时,回车键会被拦截,但输入框似乎是id变了捕捉不到;目前不打算修复,把tag关掉即可。
// ==UserScript==
// @name        Swap Enter and Ctrl+Enter Keys
// @namespace   http://www.doubao.com/keyswap
// @description Swaps RETURN and CTRL+RETURN key behavior on specified website
// @include     https://www.doubao.com/chat/*
// @match       https://www.doubao.com/chat/*
// @version     1.0
// @run-at      document-start
// ==/UserScript==

(function () {
  'use strict';

  function swapEnterKeys(event) {
    if ((event.key === 'Enter' || event.keyCode === 13) && !event.ctrlKey) {
      event.preventDefault();
      event.stopPropagation();
      event.stopImmediatePropagation();
      const textarea = document.querySelector('textarea[data-testid="chat_input_input"]');
      if (textarea) {
        const cursorPos = textarea.selectionStart;
        const textBefore = textarea.value.substring(0, cursorPos);
        const textAfter = textarea.value.substring(textarea.selectionEnd);
        textarea.value = textBefore + '\n' + textAfter;
        textarea.selectionStart = textarea.selectionEnd = cursorPos + 1;
        textarea.dispatchEvent(new Event('input', { bubbles: true }));
      }
      return false;
    }
  }

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

你可能感兴趣的:(人工智能)