Startpage Auto Retry or Refresh

Automatically clicks "Click here" or refreshes page if no results found on Startpage

// ==UserScript==
// @name         Startpage Auto Retry or Refresh
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Automatically clicks "Click here" or refreshes page if no results found on Startpage
// @author       Rishabh
// @match        https://www.startpage.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const observer = new MutationObserver(() => {
        const bodyText = document.body.innerText.toLowerCase();
        if (bodyText.includes("no search results were found for") && bodyText.includes("click here")) {
            const retryElement = document.querySelector('a[href*="do/search?query"]');
            if (retryElement && retryElement.offsetParent !== null) {
                console.log("Auto-clicking 'Click here'...");
                setTimeout(() => {
                    retryElement.click();
                    observer.disconnect();
                }, 1000);
            } else {
                console.warn("Retry element not found or not clickable. Refreshing page...");
                setTimeout(() => {
                    location.reload();
                    observer.disconnect();
                }, 1500);
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();