AllenAI Auto-Submit Listener

Listens for postMessage events on playground.allenai.org, enters into chat input, and auto-submits

As of 2025-08-02. See the latest version.

// ==UserScript==
// @name         AllenAI Auto-Submit Listener
// @description  Listens for postMessage events on playground.allenai.org, enters into chat input, and auto-submits
// @match        https://playground.allenai.org/*
// @version 0.0.1.20250802205751
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function () {
  'use strict';

  function setNativeValue(element, value) {
    const lastValue = element.value;
    element.value = value;
    const tracker = element._valueTracker;
    if (tracker) {
      tracker.setValue(lastValue);
    }
    element.dispatchEvent(new Event('input', { bubbles: true }));
  }

  window.addEventListener('message', event => {

    if (event.data?.type === 'newChatButtonClicked') {
      const newChatLink = document.querySelector('a[aria-label="Create a new thread"]');
      if (newChatLink) {
        newChatLink.click();
      }
      return;
    }

    let chatMessageInput = document.querySelector('div:has(> div > form > div > div > div > textarea[name="content"])');
    //if event data type is defaultChatMessageInputDisplay
    if (event.data?.type === 'defaultChatMessageInputDisplay') {
      console.log('default');
      if (chatMessageInput) {

        chatMessageInput.style.removeProperty('display');

        //return
        return;
      }
    }

    if (event.data?.type === 'customizeChatMessageInputDisplay') {
      console.log('customize');
      if (chatMessageInput) {

        chatMessageInput.style.display = 'none';

        //return
        return;
      }
    }

    if (event.data.type !== 'prompt' || event.data.content === 'recaptcha-setup') return;

    const message = event.data.content;

    // find the MUI/React textarea
    const textarea = document.querySelector(
      'textarea[name="content"], textarea[aria-label="Message OLMo"]'
    );
    if (!textarea) return;

    // inject into React state
    setNativeValue(textarea, message);

    // find and click the submit button
    const sendButton = document.querySelector('button[aria-label="Submit prompt"]');
    if (sendButton) {
      sendButton.click();
    }
  });
})();