Google AI Studio Auto Retry (Fix)

自动检测 Google AI Studio 的 API 报错并点击重试,修复语法报错问题

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Google AI Studio Auto Retry (Fix)
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  自动检测 Google AI Studio 的 API 报错并点击重试,修复语法报错问题
// @author       YourName
// @match        https://aistudio.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // === 配置参数 ===
    var CHECK_INTERVAL = 2000; // 检查频率
    var COOLDOWN_TIME = 5000;  // 冷却时间
    // ================

    var isCoolingDown = false;

    // 主检测函数
    function checkAndRetry() {
        if (isCoolingDown) return;

        var shouldRetry = false;

        // 1. 检测顶部弹出报错 (Popup Message)
        var popupMessages = document.querySelectorAll('.main-content .message');
        for (var i = 0; i < popupMessages.length; i++) {
            var msgEl = popupMessages[i];
            var text = msgEl.textContent || "";
            
            if (text.indexOf("Failed to generate content") !== -1) {
                shouldRetry = true;
                
                // 尝试移除报错弹窗
                var popupContainer = msgEl.closest('.main-content');
                if (popupContainer) {
                    var parent = popupContainer.closest('mat-snack-bar-container') || popupContainer.parentElement;
                    if (parent) {
                        parent.remove();
                    } else {
                        popupContainer.remove();
                    }
                    console.log("[AutoRetry] Removed popup error.");
                }
            }
        }

        // 2. 检测对话流中的报错 (Inline Error)
        var inlineErrors = document.querySelectorAll('.model-error');
        for (var j = 0; j < inlineErrors.length; j++) {
            var errEl = inlineErrors[j];
            var errText = errEl.textContent || "";
            if (errText.indexOf("An internal error has occurred") !== -1) {
                shouldRetry = true;
            }
        }

        // 3. 执行重试
        if (shouldRetry) {
            tryClickRetry();
        }
    }

    // 点击重试按钮的逻辑
    function tryClickRetry() {
        var rerunButtons = document.querySelectorAll('button[name="rerun-button"]');

        if (rerunButtons.length > 0) {
            var lastButton = rerunButtons[rerunButtons.length - 1];

            if (!lastButton.disabled && lastButton.offsetParent !== null) {
                console.log("[AutoRetry] Error detected. Clicking retry...");
                
                lastButton.click();
                
                isCoolingDown = true;
                showToast("AutoRetry: Error handled");

                setTimeout(function() {
                    isCoolingDown = false;
                    console.log("[AutoRetry] Cooldown finished.");
                }, COOLDOWN_TIME);
            }
        }
    }

    // 屏幕提示
    function showToast(message) {
        var div = document.createElement('div');
        div.style.position = 'fixed';
        div.style.bottom = '20px';
        div.style.right = '20px';
        div.style.backgroundColor = 'red';
        div.style.color = 'white';
        div.style.padding = '5px 10px';
        div.style.borderRadius = '4px';
        div.style.zIndex = '999999';
        div.style.fontSize = '12px';
        div.textContent = message;
        document.body.appendChild(div);
        
        setTimeout(function() {
            div.remove();
        }, 3000);
    }

    // 启动脚本
    console.log("[AutoRetry] Script started.");
    setInterval(checkAndRetry, CHECK_INTERVAL);

})();