Kagi Assistant Ctrl + Enter to submit

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

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         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);
})();