ChatGPT Message Queue

Queue messages when ChatGPT is still composing responses

当前为 2025-03-18 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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 });
})();