Greasy Fork

DeepSeek自动重试

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

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

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

    const BUSY_MSG = '服务器繁忙,请稍后再试';
    const LIMIT_MSG = '你发送消息的频率过快,请稍后再发';
    const CHECK_TIME = 1000;
    let tryCount = 0;
    let showTip = false;

    // 创建可关闭的提示框
    function showMsg(msg, forever = false) {
        if (showTip) return null;
        showTip = true;

        const tip = document.createElement('div');
        tip.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 text = document.createElement('div');
        text.textContent = msg;
        tip.appendChild(text);

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

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

    // 创建临时提示
    function showTempMsg(msg) {
        if (showTip) return null;
        const tip = showMsg(msg);
        if (tip) {
            setTimeout(() => {
                tip.style.opacity = '0';
                setTimeout(() => {
                    tip.remove();
                    showTip = false;
                }, 300);
            }, 3000);
        }
        return tip;
    }

    // 查找重试按钮
    function findBtn(err) {
        let parent = err;
        while (parent && !parent.querySelector('.ds-icon-button')) {
            parent = parent.parentElement;
        }
        if (!parent) return null;

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

    // 自动重试主函数
    function autoRetry() {
        const limitTip = Array.from(document.querySelectorAll('.ds-toast__content')).find(el => 
            el.textContent && el.textContent.trim() === LIMIT_MSG
        );

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

        const errList = Array.from(document.querySelectorAll('.ds-markdown--block')).filter(el => {
            const text = el.textContent?.trim() || '';
            if (text !== BUSY_MSG) {
                return false;
            }
            
            let parent = el;
            while (parent) {
                if (parent.classList.contains('fa81')) {
                    return false;
                }
                parent = parent.parentElement;
            }
            return true;
        });

        if (errList.length > 0) {
            const retryBtn = findBtn(errList[0]);
            
            if (retryBtn) {
                tryCount++;
                
                if (tryCount > 10) {
                    showMsg('检测到多次失败,DeepSeek可能当前算力不足', true);
                    return;
                }

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

                const wait = 1500 + Math.random() * 1500;
                setTimeout(() => {
                    console.log('找到重试按钮,自动点击');
                    retryBtn.dispatchEvent(new MouseEvent('click', {
                        view: window,
                        bubbles: true,
                        cancelable: true
                    }));
                }, wait);
            }
        }
    }

    // 定期检查
    setInterval(autoRetry, CHECK_TIME);
})();