ChatGPT Message Queue

Queue messages when ChatGPT is still composing responses

目前為 2025-03-18 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        ChatGPT Message Queue
// @match       https://chat.openai.com/*
// @match       https://chatgpt.com/*
// @description Queue messages when ChatGPT is still composing responses

// @version 0.0.1.20250318094810
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==
(function() {
  'use strict';
  
  let messageQueue = [];
  let queueIndicator = null;
  let isProcessing = false;
  
  function createQueueIndicator() {
    if (queueIndicator) return;
    
    queueIndicator = document.createElement('div');
    queueIndicator.style.position = 'absolute';
    queueIndicator.style.bottom = '60px';
    queueIndicator.style.right = '20px';
    queueIndicator.style.backgroundColor = '#343541';
    queueIndicator.style.color = 'white';
    queueIndicator.style.padding = '8px 12px';
    queueIndicator.style.borderRadius = '8px';
    queueIndicator.style.fontSize = '14px';
    queueIndicator.style.fontWeight = 'bold';
    queueIndicator.style.zIndex = '1000';
    queueIndicator.style.display = 'none';
    queueIndicator.textContent = 'Queued: 0';
    
    document.body.appendChild(queueIndicator);
  }
  
  function updateQueueDisplay() {
    if (!queueIndicator) createQueueIndicator();
    
    if (messageQueue.length > 0) {
      queueIndicator.textContent = `Queued: ${messageQueue.length}`;
      queueIndicator.style.display = 'block';
    } else {
      queueIndicator.style.display = 'none';
    }
  }
  
  function isComposing() {
    return !!document.querySelector('[data-testid="stop-button"]');
  }
  
  // Main observer to watch for ChatGPT's state changes
  const stateObserver = new MutationObserver(() => {
    // If not composing and we have messages, process the next one
    if (!isComposing() && messageQueue.length > 0 && !isProcessing) {
      processNextMessage();
    }
  });

  function processNextMessage() {
    if (messageQueue.length === 0 || isComposing()) return;
    
    isProcessing = true;
    const message = messageQueue.shift();
    updateQueueDisplay();
    
    const textarea = document.getElementById('prompt-textarea');
    if (!textarea) {
      isProcessing = false;
      return;
    }
    
    // Set the message in the textarea
    textarea.value = message;
    
    // Trigger input event to ensure ChatGPT recognizes the value
    const inputEvent = new Event('input', { bubbles: true });
    textarea.dispatchEvent(inputEvent);
    
    // Click the send button
    setTimeout(() => {
      const sendButton = document.querySelector('[data-testid="send-button"]');
      if (sendButton) {
        sendButton.click();
      }
      isProcessing = false;
    }, 10);
  }
  
  // Setup observer to initialize the textarea listener
  const setupObserver = new MutationObserver(() => {
    const textarea = document.getElementById('prompt-textarea');
    if (textarea && !textarea.dataset.queueEnabled) {
      textarea.dataset.queueEnabled = 'true';
      createQueueIndicator();
      
      textarea.addEventListener('keydown', (e) => {
        // Check if Enter pressed without modifiers
        if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
          // Only queue if ChatGPT is composing
          if (isComposing()) {
            e.preventDefault();
            e.stopPropagation();
            
            // Add message to queue
            if (textarea.value.trim()) {
              messageQueue.push(textarea.value);
              textarea.value = '';
              updateQueueDisplay();
            }
          }
        }
      });
      
      // Start observing state changes once we've set up the textarea
      stateObserver.observe(document.body, { childList: true, subtree: true });
    }
  });
  
  // Start the setup observer
  setupObserver.observe(document.body, { childList: true, subtree: true });
})();