Greasy Fork

Aternos Auto-Start and Restart Bot

Start Aternos server and handle actions automatically without countdown logic.

// ==UserScript==
// @name         Aternos Auto-Start and Restart Bot
// @namespace    https://aternos.org/
// @version      3.6
// @description  Start Aternos server and handle actions automatically without countdown logic.
// @author       Zephyr5929
// @match        https://aternos.org/server/*
// @grant        none
// @license      MIT
// @icon         https://media.forgecdn.net/avatars/375/55/637549609568691156.png
// ==/UserScript==

(function () {
    'use strict';

    // Constants
    const INITIAL_DELAY_MS = 10000; // 10 seconds

    // State tracking
    let startExecuted = false;
    let confirmSkipped = false;

    // Function to wait for the DOM to be ready
    function onDOMReady(callback) {
        if (document.readyState === "complete" || document.readyState === "interactive") {
            callback();
        } else {
            document.addEventListener("DOMContentLoaded", callback);
        }
    }

    // Function to check if an element is visible
    function isVisible(selector) {
        const element = document.querySelector(selector);
        return element && element.offsetParent !== null; // Ensures the element is visible in the DOM
    }

    // Function to simulate a click on an element
    function clickElement(selector) {
        const element = document.querySelector(selector);
        if (element) {
            console.log(`Clicking element: ${selector}`);
            element.click();
            return true;
        } else {
            console.warn(`Element not found: ${selector}`);
            return false;
        }
    }

    // Main logic to handle server start
    function handleServerStart() {
        // Check if the Restart button is visible
        if (isVisible("#restart")) {
            console.log("'Restart' button is visible. Skipping 'Start' and 'Confirm now!' actions.");
            return;
        }

        // Check if the Start button is visible and click it
        if (isVisible("#start")) {
            console.log("'Start' button is visible. Starting the server...");
            if (clickElement("#start")) {
                startExecuted = true; // Track that "Start" was clicked
                monitorStopOrConfirmButton(); // Start monitoring for the "Stop" or "Confirm now!" button
            }
        } else {
            console.warn("'Start' button is not visible. No action taken.");
        }
    }

    // Function to monitor for either the "Stop" or "Confirm now!" button
    function monitorStopOrConfirmButton() {
        console.log("Monitoring for 'Stop' or 'Confirm now!' button...");

        const observer = new MutationObserver(() => {
            if (isVisible("#stop")) {
                console.log("'Stop' button detected. Skipping 'Confirm now!' and proceeding...");
                confirmSkipped = true; // Mark that we skipped confirm
                observer.disconnect(); // Stop observing
            } else if (isVisible("#confirm")) {
                console.log("'Confirm now!' button detected. Clicking...");
                if (clickElement("#confirm")) {
                    observer.disconnect(); // Stop observing after clicking "Confirm now!"
                }
            }
        });

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

        // Safety fallback to stop observing after a timeout
        setTimeout(() => observer.disconnect(), 30000); // Stops monitoring after 30 seconds
    }

    // Initialize the script
    onDOMReady(() => {
        console.log("Page loaded. Waiting 10 seconds before starting...");

        setTimeout(() => {
            handleServerStart();
        }, INITIAL_DELAY_MS);
    });
})();