Auto-Page Refresh

Automatically refresh the page if specific errors are detected in specified divs

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name         Auto-Page Refresh
// @version      4.0
// @description  Automatically refresh the page if specific errors are detected in specified divs
// @author       Misspent & OpenAI
// @namespace    https://chatbot.theb.ai
// @icon         https://i.imgur.com/xap0lZG.png
// @icon         https://icons.iconarchive.com/icons/zerode/plump/256/Document-write-icon.png
// @license      MIT
// @include      *upcloud.*
// @include      *mcloud.*
// @include      *mycloud.*
// @include      *vidplay.*
// @include      *mp4upload.*
// @include      *vidcloud.*
// @include      *vizcloud.*
// @include      *filemoon.*
// @include      *moviesapi.*
// @include      *vidsrc.*
// @include      *rapid-cloud.*
// @include      *a9bfed0818.*
// @include      *aniwave.*
// @include      *fmoviesz.*
// @grant        none
// ==/UserScript==

// Refresh the page if specific errors are detected in specified divs
(function () {
    'use strict';

    // Define an array of targets with div, text, and optional delay pairs
    const targets = [
        { div: '.message', text: 'Something went wrong', delay: 0 },
        { div: '.message', text: 'Unable to load episode, please try again.', delay: 0 },
        { div: '.message', text: 'Unable to load server, please try again.', delay: 0 },
        { div: '.message', text: 'Unable to load the server, please try again.', delay: 0 },
        { div: '.message', text: 'Unable to load episode list, please try again.', delay: 0 },
        { div: '#player', text: 'This video file cannot be played', delay: 0 },
        { div: '#cf-error-details h1', text: 'Bad gateway', delay: 0 },
        { div: '.message > .inner', text: 'Error while fetching chapter, please try again.', delay: 0 },
        { div: '.message', text: 'Unable to load the server, please try again.', delay: 0 },
        { div: '.message2', text: 'Error 2', delay: 0 },
        { div: '#player .loading', delay: 1000 }, // Aniwave | DO NOT go lower than 1000
    ];

    // Function to check if a target is present and trigger a reload
    const checkTarget = (target) => {
        const targetDiv = document.querySelector(target.div);
        if (targetDiv) {
            if (target.text) {
                if (targetDiv.innerText.includes(target.text)) {
                    // Refresh the page
                    location.reload();
                }
            } else {
                // Check if the div is present and has no text
                if (!targetDiv.innerText.trim()) {
                    // Refresh the page
                    location.reload();
                }
            }
        }
    };

    // Function to initiate checks with delay
    const initiateChecks = () => {
        targets.forEach(target => {
            if (target.delay === 0) {
                checkTarget(target);
            } else {
                setTimeout(() => {
                    checkTarget(target);
                }, target.delay || 0); // Use 0 if delay is not specified
            }
        });
    };

    // Create a MutationObserver
    const observer = new MutationObserver(initiateChecks);

    // Configure the observer to watch for changes in the body and its descendants
    const observerConfig = { childList: true, subtree: true };

    // Start observing the target node for configured mutations
    observer.observe(document.body, observerConfig);

    // Initial check when the script is executed
    initiateChecks();
})();


/*

### Description:
The script is designed to automatically refresh the page if specific errors are detected within specified div elements. It utilizes MutationObserver to monitor changes in the DOM and triggers a reload if the defined conditions are met.

### What You Can Do:
1. **Specify Target Divs**: You can specify the div elements (`div`) within the document to monitor for errors.
2. **Check for Specific Text**: You can optionally specify specific text (`text`) within the target divs to trigger a reload if found.
3. **Set Individual Delay**: You can optionally set a delay (`delay`) for each target, allowing you to customize the timing of checks.

### What You Can't Do:
1. **Customize Error Handling**: The script is designed specifically to refresh the page upon detecting errors within specified divs. It doesn't offer customization for handling errors beyond this functionality.
2. **Define Complex Conditions**: While you can specify text within target divs, the script doesn't support defining complex conditions or actions beyond checking for specific text.
3. **Monitor Dynamic Changes Beyond Divs**: The script is limited to monitoring changes within specified div elements and may not capture dynamic changes occurring elsewhere in the document.

### Usage Notes:
- Ensure that the specified div elements accurately represent error conditions on the page.
- Use the optional `text` parameter to narrow down specific error messages within the target divs.
- Customize the `delay` parameter to control the timing of checks for each target.
- Keep in mind that excessive use of delays or frequent page reloads may impact user experience negatively.

Overall, the script provides a straightforward solution for automatically refreshing the page based on predefined error conditions within specified div elements, offering flexibility through optional text checks and delay settings.

*/










/*


// Refresh the page if specific errors are detected in specified divs
(function () {
    'use strict';

    // Define an array of targets with div and text pairs
    const targets = [
        { div: '.message', text: 'Something went wrong' },
        { div: '.message', text: 'Unable to load episode, please try again.' },
        { div: '.message', text: 'Unable to load server, please try again.' },
        { div: '.message', text: 'Unable to load the server, please try again.' },
        { div: '.message', text: 'Unable to load episode list, please try again.' },
        { div: '#player', text: 'This video file cannot be played' },
        { div: '#cf-error-details h1', text: 'Bad gateway' },
        { div: '.message > .inner', text: 'Error while fetching chapter, please try again.' },
        { div: '.message', text: 'Unable to load the server, please try again.' },
        { div: '.message2', text: 'Error 2' },
        { div: '#player .loading' }, // Running pika
    ];

    // Function to check if a target is present and trigger a reload
    const checkTargets = () => {
        targets.forEach(target => {
            const targetDiv = document.querySelector(target.div);
            if (targetDiv && targetDiv.innerText.includes(target.text)) {
                // Refresh the page
                location.reload();
            }
        });
    };

    // Create a MutationObserver
    const observer = new MutationObserver(checkTargets);

    // Configure the observer to watch for changes in the body and its descendants
    const observerConfig = { childList: true, subtree: true };

    // Start observing the target node for configured mutations
    observer.observe(document.body, observerConfig);

    // Initial check when the script is executed
    checkTargets();
})();


*/





/*


// This one is ONLY for aniwave. It might be redundant now, but keep it just in case.
(function() {
    'use strict';

    let loadingDivFound = false;

    const checkForLoadingDiv = () => {
        const loadingDivs = document.querySelectorAll('#player div.loading');
        Array.from(loadingDivs).forEach((loadingDiv) => {
            if (getComputedStyle(loadingDiv)['background-image'].includes('url')) {
                loadingDivFound = true;
                setTimeout(() => {
                    if (loadingDivFound && getComputedStyle(loadingDiv)['background-image'].includes('url')) {
                        location.reload(); // Refresh the page
                    } else {
                        loadingDivFound = false; // Reset the flag if the image is no longer showing
                    }
                }, 1000); // Wait 1 seconds before refreshing the page
            }
        });
    }

    setInterval(() => {
        checkForLoadingDiv();
    }, 1000); // Check every second for the loading div
})();


// Can you make a TamperMonkey script that refreshes a page when the a selector called "div.loading" shows for longer than 6 seconds, make sure the div.loading has a "background: url" so it knows it's the right one
// Can you make it so it has to detect that the image is ALWAYS showing for those 6 seconds and if the image isn't showing, make it so the page won't refresh


// Reload and optionally notify when a certain message is shown on error message
(function() {
    'use strict';

    const enableNotifications = true; // Set this to true to enable notifications, or false to disable

    const errorMessages = [
        "Mysql error, could not connect",
        "max_user_connections",
        "502 Bad Gateway",
        "504 Gateway Time-out",
        "Something broke",
        "The database timed out running your query",
        "Server error, please try again",
        "we took too long to make this page for you",
        "A timeout occurred",
        "Connection timed out",
        "Server error, please refresh this page and try again",
        "Unable to load episode, please try again",
        "Too many connections. Please try again later",
        "Request is invalid.",
        "Unable to load episodes.",
        "Unable to load the server, please try again.",
        "This video file cannot be played.",
        "The media could not be loaded, either because the server or network failed or because the format is not supported.",
        "Unable to load server, please try again.",
        // Add more error messages here
    ];

    // Monitor for error messages and reload if found
    const observer = new MutationObserver(() => {
        const pageContent = document.body.textContent;
        for (const errorMessage of errorMessages) {
            if (pageContent.includes(errorMessage)) {
                if (enableNotifications) {
                    showNotification('Page is being refreshed due to an error');
                }
                location.reload();
                break;
            }
        }
    });

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

    // Show notification at the bottom center of the page
    function showNotification(message) {
        const notification = document.createElement('div');
        notification.textContent = message;
        notification.style.position = 'fixed';
        notification.style.bottom = '10px';
        notification.style.left = '50%';
        notification.style.transform = 'translateX(-50%)';
        notification.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';
        notification.style.padding = '10px';
        notification.style.color = 'white';
        notification.style.borderRadius = '5px';
        notification.style.zIndex = '9999';
        document.body.appendChild(notification);
        setTimeout(() => {
            notification.remove();
        }, 3000);
    }
})();


*/