OnlyFaucet (UPDATE)

Claim with ✓ Correct!, detect Processing freeze, reload if idle 30s, reload if freeze

// ==UserScript==
// @name         OnlyFaucet (UPDATE)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Claim with ✓ Correct!, detect Processing freeze, reload if idle 30s, reload if freeze
// @author       👽
// @license      MIT
// @match        *://*.onlyfaucet.com/*
// @grant        none
// ==/UserScript==

//   OOOOO   TTTTT  RRRRR    EEEEE   W   W   OOOOO
//  O     O    T    R    R   E       W   W  O     O
//  O     O    T    RRRRR    EEEE    W W W  O     O
//  O     O    T    R  R     E       WW WW  O     O
//   OOOOO     T    R   R    EEEEE   W   W   OOOOO

(function () {
    'use strict';

    console.log(`
  OOOOO   TTTTT  RRRRR    EEEEE   W   W   OOOOO
 O     O    T    R    R   E       W   W  O     O
 O     O    T    RRRRR    EEEE    W W W  O     O
 O     O    T    R  R     E       WW WW  O     O
  OOOOO     T    R   R    EEEEE   W   W   OOOOO
    `);

    let correctDivVisibleSince = null;
    let claimClicked = false;
    let processingVisibleSince = null;

    // Tracks last time any relevant action happened to reset idle timer
    let lastActionTimestamp = Date.now();

    const resetIdleTimer = () => {
        lastActionTimestamp = Date.now();
    };

    const checkInterval = setInterval(() => {
        // Check ✓ Correct! div
        const divs = Array.from(document.querySelectorAll('div'));
        const correctDiv = divs.find(div =>
            div.textContent.trim() === '✓ Correct!' &&
            div.style.minHeight === '22px' &&
            div.style.fontSize === '1em' &&
            div.style.fontWeight === '500' &&
            div.style.textAlign === 'center' &&
            div.style.color === 'rgb(22, 163, 74)'
        );

        if (correctDiv) {
            if (!correctDivVisibleSince) {
                correctDivVisibleSince = Date.now();
                resetIdleTimer();
                console.log('✓ Correct! appeared.');

                // Wait 5-8 seconds before clicking
                const delay = Math.floor(Math.random() * (8000 - 5000 + 1)) + 5000;
                console.log(`Waiting ${delay / 1000}s before clicking Claim Now...`);

                setTimeout(() => {
                    if (claimClicked) return;

                    const claimButton = Array.from(document.querySelectorAll('span')).find(
                        span => span.textContent.trim() === 'Claim Now'
                    );

                    if (claimButton) {
                        console.log('Clicking Claim Now button...');
                        claimClicked = true;
                        resetIdleTimer();
                        claimButton.click();
                    } else {
                        console.warn('Claim Now button not found.');
                    }
                }, delay);
            } else {
                // If div has stayed visible more than 15 seconds, reload
                const visibleTime = Date.now() - correctDivVisibleSince;
                if (visibleTime > 15000 && !claimClicked) {
                    console.warn('✓ Correct! stayed visible >15s. Reloading...');
                    clearInterval(checkInterval);
                    location.reload();
                }
            }
        } else {
            correctDivVisibleSince = null; // reset timer if div disappears
        }

        // Check Processing button freeze
        const processingButton = document.querySelector('button#subbutt.btn.btn-primary[disabled]');
        if (processingButton) {
            const span = processingButton.querySelector('span');
            if (span && span.textContent.trim() === 'Processing...') {
                if (!processingVisibleSince) {
                    processingVisibleSince = Date.now();
                    resetIdleTimer();
                    console.log('"Processing..." button detected and disabled.');
                } else {
                    const processingTime = Date.now() - processingVisibleSince;
                    if (processingTime > 15000) {
                        console.warn('"Processing..." button frozen >15s. Reloading...');
                        clearInterval(checkInterval);
                        location.reload();
                    }
                }
            } else {
                processingVisibleSince = null;
            }
        } else {
            processingVisibleSince = null;
        }

        // Check idle for 30 seconds (no actions)
        if (Date.now() - lastActionTimestamp > 30000) {
            console.warn('No activity detected for 30 seconds. Reloading...');
            clearInterval(checkInterval);
            location.reload();
        }

    }, 500);
})();