Greasy Fork

Greasy Fork is available in English.

Mistral Chat Prompt Injector (Contenteditable Fix)

Inject prompt into Mistral's contenteditable div

当前为 2025-08-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Mistral Chat Prompt Injector (Contenteditable Fix)
// @description  Inject prompt into Mistral's contenteditable div
// @match        https://chat.mistral.ai/*
// @run-at       document-idle
// @version 0.0.1.20250802133520
// @namespace http://greasyfork.icu/users/1435046
// ==/UserScript==

(function () {
  window.addEventListener('message', event => {
    if (event.data?.type === 'newChatButtonClicked') {
      document.querySelector('button[aria-label="New chat"]')?.click();
      return;
    }

    if (event.data.type === "prompt" && event.data.content.trim()) {
      const editor = document.querySelector('[contenteditable="true"].ProseMirror');
      if (editor) {
        // Focus the editor first
        editor.focus();

        // Create a new text node with the prompt
        const textNode = document.createTextNode(event.data.content);

        // Remove any existing children
        while (editor.firstChild) {
          editor.removeChild(editor.firstChild);
        }

        // Append a paragraph <p> with the text content
        const p = document.createElement('p');
        p.appendChild(textNode);
        editor.appendChild(p);

        // Manually dispatch an input event to trigger updates
        const inputEvent = new Event('input', { bubbles: true });
        editor.dispatchEvent(inputEvent);

        // submit
        // Select the submit button
        const submitButton = document.querySelector('button[type="submit"][aria-disabled="false"]');

        // If the button exists initially, click it
        if (submitButton) {
          submitButton.click();
        } else {
          // Create a MutationObserver to detect when the specific button appears in the DOM
          const mutationObserver = new MutationObserver((mutationsList, observer) => {
            // Look for the specific submit button by matching both the button type and aria-disabled attributes
            const newSubmitButton = document.querySelector('button[type="submit"][aria-disabled="false"]');

            if (newSubmitButton) {
              // If the button is found, click it and disconnect the observer
              newSubmitButton.click();
              observer.disconnect();  // Disconnect the observer after it's done
            }
          });

          // Start observing the document for changes to add the specific button
          mutationObserver.observe(document.body, {
            childList: true,
            subtree: true
          });
        }
      }
    }
  });
})();