Created
February 8, 2025 18:58
-
-
Save Staninna/dccd789242b0e173f7b14a8bb1c320c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Auto-Retry (DS Style) | |
// @namespace http://tampermonkey.net/ | |
// @version 0.5 | |
// @description Automatically click the retry button when DS message blocks show "The server is busy. Please try again later." or "Thought for 0 seconds". Uses a cooldown so that repeated clicks occur if the error persists. | |
// @author Staninna | |
// @match https://chat.deepseek.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
// Toggle for whether auto-retry is enabled. | |
let autoRetryEnabled = true; | |
// Error trigger phrases: | |
const errorPhrase = "The server is busy. Please try again later."; | |
const altTrigger = "Thought for 0 seconds"; | |
// Return true if the provided text qualifies as an error message. | |
function isError(text) { | |
return text.includes(errorPhrase) || text.includes(altTrigger); | |
} | |
// Helper: Find the retry button. We assume DS’s retry/regenerate button contains an SVG <rect> whose id is "重新生成". | |
function findRetryButton() { | |
const buttons = Array.from(document.querySelectorAll("div.ds-icon-button")); | |
return buttons.find((btn) => btn.querySelector("svg rect#重新生成") !== null); | |
} | |
// Helper: Get the text from a DS markdown block. | |
function getMarkdownText(el) { | |
return el.innerText || ""; | |
} | |
// Main function: for each DS markdown block that may contain an error, trigger a retry click. | |
// We use a 15-second cooldown (adjustable) per element. | |
function checkErrorMarkdowns() { | |
if (!autoRetryEnabled) return; | |
const cooldown = 15000; // 15 seconds | |
// DS error text appears in DS markdown blocks – adjust the selector if needed. | |
// For example: <div class="ds-markdown ds-markdown--block">...</div> | |
const blocks = document.querySelectorAll("div.ds-markdown.ds-markdown--block"); | |
blocks.forEach((block) => { | |
const text = getMarkdownText(block); | |
if (isError(text)) { | |
const last = parseInt(block.dataset.lastRetry || "0", 10); | |
const now = Date.now(); | |
if (now - last > cooldown) { | |
block.dataset.lastRetry = now.toString(); | |
console.log("Auto-retry triggered due to error text in markdown block."); | |
const btn = findRetryButton(); | |
if (btn) { | |
btn.click(); | |
} else { | |
console.log("Retry button not found."); | |
} | |
} | |
} | |
}); | |
} | |
// Create a DS-style toggle UI so the user can enable/disable auto-retry. | |
function createToggleUI() { | |
const uiContainer = document.createElement("div"); | |
uiContainer.id = "autoRetryToggleUI"; | |
// Apply DS form item classes for consistency. | |
uiContainer.className = "ds-form-item ds-form-item--label-s"; | |
uiContainer.style.position = "fixed"; | |
uiContainer.style.bottom = "70px"; | |
uiContainer.style.right = "20px"; | |
uiContainer.style.zIndex = "9999"; | |
uiContainer.innerHTML = ` | |
<label class="ds-checkbox-wrapper ds-checkbox-wrapper--m" style="cursor: pointer; user-select: none;"> | |
<span class="ds-checkbox"> | |
<input type="checkbox" id="autoRetryCheckbox" class="ds-checkbox__input" checked> | |
<span class="ds-checkbox__box"></span> | |
</span> | |
<span class="ds-checkbox__label">Auto‑Retry</span> | |
</label> | |
`; | |
document.body.appendChild(uiContainer); | |
document.getElementById("autoRetryCheckbox").addEventListener("change", function () { | |
autoRetryEnabled = this.checked; | |
console.log("Auto‑Retry turned " + (autoRetryEnabled ? "ON" : "OFF")); | |
}); | |
} | |
// Initialize the toggle UI and start the periodic check (every 3 seconds). | |
createToggleUI(); | |
setInterval(checkErrorMarkdowns, 3000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment