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.4
// @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';
    const closeEvenIfNotExpired = false;
    const closeAfterSeconds = 10;
    const closeDelaySeconds = 3; // Add a delay before closing the window waits 3 seconds before closing the tab

    function checkForExpiredMessage() {
        if (document.body.innerText.includes("This inventory page has expired.")) {
            console.log("attempted to close window: inventory page has expired");
            setTimeout(() => {
                window.close(); // Close the window after the delay
            }, closeDelaySeconds * 1000);
        }
    }

    function checkForMissingElements() {
        const invHolder = document.getElementById("inventoryholder");
        if (!invHolder || !invHolder.querySelector("#invController")) {
            console.log("attempted to close window: inventoryholder or invController is missing");
            setTimeout(() => {
                window.close(); // Close the window after the delay
            }, closeDelaySeconds * 1000);
        }
    }

    const observer = new MutationObserver((mutations) => {
        mutations.forEach(() => {
            checkForMissingElements();
            checkForExpiredMessage();
        });
    });

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

    setTimeout(() => {
        checkForMissingElements();
        checkForExpiredMessage();
    }, 5000);

    if (closeEvenIfNotExpired) {
        setTimeout(() => {
            setTimeout(() => {
                window.close();
            }, closeDelaySeconds * 1000); // Close after the delay
        }, closeAfterSeconds * 1000);
    }
})();