Aternos Auto-Start and Restart Bot

Start Aternos server, confirm actions, and handle countdown for restart logic automatically.

目前為 2025-01-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Aternos Auto-Start and Restart Bot
// @namespace    https://aternos.org/
// @version      3.2
// @description  Start Aternos server, confirm actions, and handle countdown for restart logic automatically.
// @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 = 12000; // 12 seconds
    const COUNTDOWN_THRESHOLD_MS = 3.5 * 60 * 1000; // 3 minutes and 30 seconds in milliseconds

    // State tracking
    let startExecuted = false;
    let confirmExecuted = 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;
        }
    }

    // Function to monitor for the "Confirm now!" button and click it when visible
    function monitorConfirmButton() {
        console.log("Monitoring for 'Confirm now!' button...");

        const observer = new MutationObserver(() => {
            if (isVisible("#confirm")) {
                console.log("'Confirm now!' button detected. Clicking...");
                if (clickElement("#confirm")) {
                    confirmExecuted = true; // Track that "Confirm now!" was clicked
                    observer.disconnect(); // Stop observing after clicking the button
                    startCountdownMonitor(); // Start monitoring the countdown
                }
            }
        });

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

    // Function to monitor the countdown and click "Restart" if needed
    function startCountdownMonitor() {
        if (!startExecuted || !confirmExecuted) {
            console.warn("Countdown monitoring skipped. 'Start' or 'Confirm now!' not executed.");
            return;
        }

        console.log("Monitoring countdown for 'Restart' button...");

        const observer = new MutationObserver(() => {
            const countdownElement = document.querySelector(".end-countdown");
            if (countdownElement && isVisible(".end-countdown")) {
                const timerVisibleFor = Date.now() - countdownElement.dataset.appearTime;

                // Check if countdown has been visible for more than the threshold
                if (timerVisibleFor > COUNTDOWN_THRESHOLD_MS) {
                    console.log("Countdown threshold exceeded. Clicking 'Restart'...");
                    if (clickElement("#restart")) {
                        observer.disconnect(); // Stop observing after clicking "Restart"
                    }
                }
            }
        });

        // Add a custom attribute to track the time countdown became visible
        const countdownElement = document.querySelector(".end-countdown");
        if (countdownElement) {
            countdownElement.dataset.appearTime = Date.now();
        }

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

    // 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
                monitorConfirmButton(); // Start monitoring for the "Confirm now!" button
            }
        } else {
            console.warn("'Start' button is not visible. No action taken.");
        }
    }

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

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