Greasy Fork

DeepSeek自动重试

检测到"服务器繁忙"时自动点击重试按钮

目前为 2025-02-06 提交的版本。查看 最新版本

// ==UserScript==
// @name         DeepSeek自动重试
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  检测到"服务器繁忙"时自动点击重试按钮
// @author       dy
// @match        https://chat.deepseek.com/*
// @grant        none
// @license      none
// ==/UserScript==

(function() {
    'use strict';

    const BUSY_TEXT = '服务器繁忙,请稍后再试';
    const RATE_LIMIT_TEXT = '你发送消息的频率过快,请稍后再发';
    const CHECK_INTERVAL = 1000;
    let retryCount = 0;
    let isNotifying = false;

    function createClosableNotification(message, permanent = false) {
        if (isNotifying) return null;
        isNotifying = true;

        const notification = document.createElement('div');
        notification.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            background: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            z-index: 9999;
            transition: opacity 0.3s;
            display: flex;
            align-items: center;
            gap: 10px;
        `;

        const messageDiv = document.createElement('div');
        messageDiv.textContent = message;
        notification.appendChild(messageDiv);

        const closeButton = document.createElement('button');
        closeButton.innerHTML = '✕';
        closeButton.style.cssText = `
            background: none;
            border: none;
            color: white;
            cursor: pointer;
            font-size: 16px;
            padding: 0 5px;
        `;
        closeButton.onclick = () => {
            notification.style.opacity = '0';
            setTimeout(() => {
                notification.remove();
                isNotifying = false;
                retryCount = 0;
            }, 300);
        };
        notification.appendChild(closeButton);

        document.body.appendChild(notification);
        return notification;
    }

    // 创建临时提示
    function createTempNotification(message) {
        if (isNotifying) return null;
        const notification = createClosableNotification(message);
        if (notification) {
            setTimeout(() => {
                notification.style.opacity = '0';
                setTimeout(() => {
                    notification.remove();
                    isNotifying = false;
                }, 300);
            }, 3000);
        }
        return notification;
    }

    function findRetryButton(errorElement) {
        let parent = errorElement;
        while (parent && !parent.querySelector('.ds-icon-button')) {
            parent = parent.parentElement;
        }
        if (!parent) return null;

        const buttons = Array.from(parent.querySelectorAll('.ds-icon-button'));
        return buttons.find(button => {
            const svg = button.querySelector('svg');
            return svg && svg.querySelector('#重新生成');
        });
    }

    function autoRetry() {
        const rateLimitElement = Array.from(document.querySelectorAll('.ds-toast__content')).find(el =>
            el.textContent && el.textContent.includes(RATE_LIMIT_TEXT)
        );

        if (rateLimitElement) {
            createClosableNotification('检测到频率过快,稍等一会吧', true);
            return;
        }

        const elements = Array.from(document.querySelectorAll('.ds-markdown--block')).filter(el =>
            el.textContent && el.textContent.includes(BUSY_TEXT)
        );

        if (elements.length > 0) {
            const retryButton = findRetryButton(elements[0]);

            if (retryButton) {
                retryCount++;

                if (retryCount > 10) {
                    createClosableNotification('检测到多次失败,DeepSeek可能当前算力不足', true);
                    return;
                }

                const notification = createTempNotification('检测到服务器繁忙,即将自动重试...');
                if (!notification) return;

                // 随机延时1.5-3秒
                const delay = 1500 + Math.random() * 1500;
                setTimeout(() => {
                    console.log('找到重试按钮,自动点击');
                    retryButton.dispatchEvent(new MouseEvent('click', {
                        view: window,
                        bubbles: true,
                        cancelable: true
                    }));
                }, delay);
            }
        }
    }

    setInterval(autoRetry, CHECK_INTERVAL);
})();