ChatGPT Message Queue

Queue messages when ChatGPT is still composing responses

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

您需要先安裝使用者腳本管理器擴展,如 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.20250319105457
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
    'use strict';
    
    let queueCount = 0;
    let queueObserver;
    let processingMessage = false;
    const queueDisplay = document.createElement('div');
    
    // Minimal UI setup
    queueDisplay.innerHTML = `
        <span>Queue: ${queueCount}</span>
        <button id="queue-reset">×</button>
    `;
    queueDisplay.style.cssText = `
        position:fixed; bottom:100px; right:20px;
        background:rgba(0,0,0,0.7); color:white;
        padding:5px 10px; border-radius:4px; display:flex; gap:8px;
    `;
    document.body.appendChild(queueDisplay);
    
    // Reset queue count when button is clicked
    document.getElementById('queue-reset').addEventListener('click', () => {
        queueCount = 0;
        queueDisplay.children[0].textContent = `Queue: ${queueCount}`;
    });
    
    // Function to start queue processing
    function startQueueObserver() {
        if (queueObserver) return;  // Prevent duplicate observers
        
        queueObserver = new MutationObserver(() => {
            // Only proceed if we have messages in queue and we're not currently processing
            if (queueCount > 0 && !processingMessage) {
                const sendButton = document.querySelector('[data-testid="send-button"]');
                const textArea = document.getElementById('prompt-textarea');
                
                // Only click if the send button is enabled and textarea is accessible
                if (sendButton && !sendButton.disabled && textArea) {
                    processingMessage = true;
                    
                    // Wait for the current response to complete
                    const checkCompletion = setInterval(() => {
                        // Look for indicators that the AI has finished responding
                        const isResponding = document.querySelector('.result-streaming') || 
                                          document.querySelector('[data-testid="stop-generating"]');
                        
                        if (!isResponding) {
                            clearInterval(checkCompletion);
                            
                            // Wait a bit for the UI to fully update
                            setTimeout(() => {
                                // Now we can send the next message
                                sendButton.click();
                                queueCount--;
                                queueDisplay.children[0].textContent = `Queue: ${queueCount}`;
                                processingMessage = false;
                                
                                // Stop observing when queue is empty
                                if (queueCount === 0) {
                                    queueObserver.disconnect();
                                    queueObserver = null;
                                }
                            }, 500);
                        }
                    }, 1000);
                }
            }
        });
        
        queueObserver.observe(document.body, {childList: true, subtree: true});
    }
    
    // Core logic: Monitor input field
    new MutationObserver((_, observer) => {
        const textarea = document.getElementById('prompt-textarea');
        if (textarea && !textarea.dataset.queuer) {
            textarea.dataset.queuer = true;
            textarea.addEventListener('keydown', e => {
                if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
                    // Allow DOM update
                    setTimeout(() => {
                        if (textarea.textContent.trim()) {
                            queueCount++;
                            queueDisplay.children[0].textContent = `Queue: ${queueCount}`;
                            startQueueObserver(); // Start processing queue
                        }
                    }, 100);
                }
            });
        }
    }).observe(document.body, {childList: true, subtree: true});
})();