Auto Close Inventory Tab

Automatically close the current inventory tab based on user-defined timer or when the inventory page expired

// ==UserScript==
// @name         Auto Close Inventory Tab
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Automatically close the current inventory tab based on user-defined timer or when the inventory page expired
// @author       Lucky11
// @match        https://fairview.deadfrontier.com/onlinezombiemmo/DF3D/DF3D_InventoryPage.php?page=31*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    // User-defined option to close even if not expired (true or false)
    const closeEvenIfNotExpired = false;
    // User-defined timer in seconds (set to 60 seconds by default) even if not expired
    const closeAfterSeconds = 60;

    // Function to check for the expired message
    function checkForExpiredMessage() {
        if (document.body.innerText.includes("This inventory page has expired.")) {
            window.close(); // Close the window if the message is found
        }
    }

    // Create a MutationObserver to watch for changes in the DOM
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(() => {
            checkForExpiredMessage(); // Check for the expired message on each DOM update
        });
    });

    // Start observing the body for changes
    observer.observe(document.body, { childList: true, subtree: true });
    checkForExpiredMessage();

    // If closeEvenIfNotExpired is true, set a timeout to close the tab after the user-defined time
    if (closeEvenIfNotExpired) {
        setTimeout(() => {
            window.close(); // Close the window after the specified time
        }, closeAfterSeconds * 1000); // Convert seconds to milliseconds
    }
})();