Auto Reload Claimer

Automatically claims Stake.com reloads, handles errors without getting stuck, and displays a countdown timer on screen.

// ==UserScript==
// @name         Auto Reload Claimer
// @version      1.9
// @description  Automatically claims Stake.com reloads, handles errors without getting stuck, and displays a countdown timer on screen.
// @author       natah3
// @match        https://stake.com/*
// @match        https://stake.games/*
// @match        https://stake.bet/*
// @match        https://stake.link/*
// @match        https://stake1069.com/*
// @run-at       document-end
// @namespace https://greasyfork.org/users/1514052
// ==/UserScript==

(function() {
    'use strict';

    const TARGET_URL = 'https://' + window.location.hostname + '/?tab=rewards&modal=claimReload';
    const CLAIM_WAIT_TIME = 10 * 60;
    const ERROR_RETRY_WAIT_TIME = 10 * 1000;
    const REDIRECT_WAIT_TIME = 30 * 1000;

    let hasClicked = false;
    let countdownInterval;

    function createTimerDisplay() {
        const timerDiv = document.createElement('div');
        timerDiv.id = 'auto-reload-claimer-timer';
        timerDiv.style.position = 'fixed';
        timerDiv.style.bottom = '20px';
        timerDiv.style.right = '20px';
        timerDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.6)';
        timerDiv.style.color = '#fff';
        timerDiv.style.padding = '10px 15px';
        timerDiv.style.borderRadius = '5px';
        timerDiv.style.zIndex = '9999';
        timerDiv.style.fontSize = '1.2rem';
        timerDiv.style.display = 'none';
        document.body.appendChild(timerDiv);
    }

    function startCountdown() {
        let timeLeft = CLAIM_WAIT_TIME;

        const timerDisplay = document.getElementById('auto-reload-claimer-timer');
        if (!timerDisplay) return;

        timerDisplay.style.display = 'block';

        countdownInterval = setInterval(() => {
            const minutes = Math.floor(timeLeft / 60);
            const seconds = timeLeft % 60;
            const displayMinutes = String(minutes).padStart(2, '0');
            const displaySeconds = String(seconds).padStart(2, '0');
            timerDisplay.textContent = ` ${displayMinutes}:${displaySeconds}`;

            if (timeLeft <= 0) {
                clearInterval(countdownInterval);
                reloadPage();
            }
            timeLeft--;
        }, 1000);
    }

    function init() {
        createTimerDisplay();

        if (window.location.href !== TARGET_URL) {
            setTimeout(() => {
                window.location.href = TARGET_URL;
            }, REDIRECT_WAIT_TIME);
        } else {
            findAndClickButton();
        }
    }

    function findAndClickButton() {
        if (hasClicked) {
            return;
        }

        const claimButton = document.querySelector('button[data-testid="claim-reload"]');
        if (claimButton) {
            hasClicked = true;
            claimButton.click();
            waitForCompletionButton();
        } else {
            checkForErrorModal();
        }
    }

    function checkForErrorModal() {
        const modalTitle = Array.from(document.querySelectorAll('h2')).find(el => el.textContent.includes('Reload Unavailable'));

        if (modalTitle) {
             setTimeout(reloadPage, ERROR_RETRY_WAIT_TIME);
        } else {
            setTimeout(findAndClickButton, 1000);
        }
    }

    function waitForCompletionButton() {
        const completionButton = document.querySelector('button[data-testid="claim-reward-done"]');
        if (completionButton) {
            startCountdown();
        } else {
            setTimeout(waitForCompletionButton, 1000);
        }
    }

    function reloadPage() {
        window.location.reload();
    }

    window.addEventListener('load', init);
})();