// ==UserScript== // @name Retry Helper // @namespace https://github.com/KanbanTrainer/tampermonkey // @version 2.0.0 // @description Prompts before retrying Retry or Try again buttons, with optional continuous retry for the current tab. // @match *://*/* // @grant none // @run-at document-idle // @updateURL https://raw.githubusercontent.com/KanbanTrainer/tampermonkey/main/auto-retry.user.js // @downloadURL https://raw.githubusercontent.com/KanbanTrainer/tampermonkey/main/auto-retry.user.js // ==/UserScript== (() => { 'use strict'; const RETRY_INTERVAL_SECONDS = 30; const CONTINUOUS_RETRY_KEY = 'tampermonkey-retry-helper-continuous'; const RETRY_LABELS = new Set(['retry', 'try again']); let retryWasPresent = false; let dismissedForCurrentAppearance = false; let secondsRemaining = RETRY_INTERVAL_SECONDS; function isContinuousRetryEnabled() { try { return sessionStorage.getItem(CONTINUOUS_RETRY_KEY) === 'true'; } catch { return false; } } function setContinuousRetryEnabled(enabled) { try { if (enabled) { sessionStorage.setItem(CONTINUOUS_RETRY_KEY, 'true'); } else { sessionStorage.removeItem(CONTINUOUS_RETRY_KEY); } } catch { // Keep working even when sessionStorage is unavailable. } } function normalizedButtonText(button) { return (button.innerText || button.textContent || '') .replace(/\s+/g, ' ') .trim() .toLowerCase(); } function findRetryButtons() { return [...document.querySelectorAll('button')].filter(button => !button.disabled && button.getAttribute('aria-disabled') !== 'true' && RETRY_LABELS.has(normalizedButtonText(button)) ); } function showToast(message) { const toast = document.createElement('div'); toast.textContent = message; Object.assign(toast.style, { position: 'fixed', right: '16px', bottom: '72px', zIndex: '2147483647', padding: '8px 11px', borderRadius: '9px', background: 'rgba(25, 25, 25, 0.94)', color: '#fff', fontFamily: 'system-ui, sans-serif', fontSize: '13px', fontWeight: '600', boxShadow: '0 4px 18px rgba(0, 0, 0, 0.25)', backdropFilter: 'blur(8px)', opacity: '0', transform: 'translateY(6px)', transition: 'opacity 150ms ease, transform 150ms ease', pointerEvents: 'none', }); document.documentElement.appendChild(toast); requestAnimationFrame(() => { toast.style.opacity = '1'; toast.style.transform = 'translateY(0)'; }); setTimeout(() => { toast.style.opacity = '0'; toast.style.transform = 'translateY(6px)'; setTimeout(() => toast.remove(), 200); }, 2200); } function clickRetryButtons(buttons, message = 'Retried') { for (const button of buttons) { button.click(); } if (buttons.length > 0) { showToast(buttons.length === 1 ? message : `${message} ${buttons.length} items`); } } function createControl() { const host = document.createElement('div'); host.id = 'tampermonkey-retry-helper'; Object.assign(host.style, { all: 'initial', position: 'fixed', right: '16px', bottom: '16px', zIndex: '2147483647', display: 'none', }); const shadow = host.attachShadow({ mode: 'open' }); shadow.innerHTML = `
Retry?
`; const message = shadow.querySelector('.message'); const retryButton = shadow.querySelector('.retry'); const continuousButton = shadow.querySelector('.continuous'); const closeButton = shadow.querySelector('.close'); retryButton.addEventListener('click', () => { const buttons = findRetryButtons(); clickRetryButtons(buttons); dismissedForCurrentAppearance = true; host.style.display = 'none'; }); continuousButton.addEventListener('click', () => { setContinuousRetryEnabled(true); secondsRemaining = RETRY_INTERVAL_SECONDS; const buttons = findRetryButtons(); clickRetryButtons(buttons, 'Continuous retry enabled'); render(); }); closeButton.addEventListener('click', () => { setContinuousRetryEnabled(false); dismissedForCurrentAppearance = true; secondsRemaining = RETRY_INTERVAL_SECONDS; host.style.display = 'none'; showToast('Retry helper dismissed'); }); document.documentElement.appendChild(host); return { host, message, retryButton, continuousButton, }; } const control = createControl(); function render() { const buttons = findRetryButtons(); const retryIsPresent = buttons.length > 0; if (!retryIsPresent) { retryWasPresent = false; dismissedForCurrentAppearance = false; secondsRemaining = RETRY_INTERVAL_SECONDS; control.host.style.display = 'none'; return buttons; } if (!retryWasPresent) { retryWasPresent = true; dismissedForCurrentAppearance = false; secondsRemaining = RETRY_INTERVAL_SECONDS; } if (dismissedForCurrentAppearance) { control.host.style.display = 'none'; return buttons; } const continuous = isContinuousRetryEnabled(); control.host.style.display = 'block'; control.retryButton.hidden = continuous; control.continuousButton.hidden = continuous; control.message.textContent = continuous ? `Retrying in ${secondsRemaining}s` : 'Retry?'; return buttons; } const observer = new MutationObserver(render); observer.observe(document.documentElement, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['disabled', 'aria-disabled'], }); render(); setInterval(() => { const buttons = render(); if (buttons.length === 0 || !isContinuousRetryEnabled() || dismissedForCurrentAppearance) { return; } secondsRemaining -= 1; if (secondsRemaining <= 0) { clickRetryButtons(buttons, 'Retried automatically'); secondsRemaining = RETRY_INTERVAL_SECONDS; } render(); }, 1000); })();