Kagi Assistant Ctrl + Enter to submit

Changes Enter key behavior in Kagi assistant to submit message only on Ctrl + Enter

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Kagi Assistant Ctrl + Enter to submit
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Changes Enter key behavior in Kagi assistant to submit message only on Ctrl + Enter
// @author       nothingbird
// @match        https://kagi.com/assistant*
// @grant        none
// @license      GPL-3.0-only
// ==/UserScript==

(function() {
  'use strict';

  function waitForElement(selector, callback) {
    const element = document.querySelector(selector);
    if (element) {
      callback(element);
      return;
    }
    setTimeout(() => waitForElement(selector, callback), 100);
  }

  function modifyKeyBehavior(textarea) {
    textarea.removeEventListener('keydown', handleKeydown, true);

    textarea.addEventListener('keydown', handleKeydown, true);
  }

  function handleKeydown(event) {
    // Skip IME composition events
    if (event.isComposing || event.keyCode === 229) return;

    if (event.key === 'Enter') {
      // Ctrl+Enter (without other modifiers, Shift+Enter remains unchanged)
      if (event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
        event.preventDefault();
        event.stopImmediatePropagation();

        const form = event.target.closest('form');
        if (form) {
          form.requestSubmit();
        }
      }
      // Regular Enter (without modifiers)
      else if (!event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) {
        // Allow default behavior (new line)
        event.stopImmediatePropagation();
      }
    }
  }

  waitForElement('textarea', modifyKeyBehavior);
})();