Aternos Auto START Bot (Fixed)

Adds a working AutoStart button and auto-clicks Start/Confirm on Aternos.

您需要先安裝使用者腳本管理器擴展,如 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 Bot (Fixed)
// @namespace    https://www.lstv.ml
// @version      1.0.0
// @description  Adds a working AutoStart button and auto-clicks Start/Confirm on Aternos.
// @author       LSTV + fix
// @match        https://aternos.org/server/*
// @grant        none
// @run-at       document-idle
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let botON = false;
    let loopTimer = null;
    let observer = null;

    function log(...args) {
        console.log('[Aternos AutoStart]', ...args);
    }

    function getServerActions() {
        return document.querySelector('.server-actions');
    }

    function getStartButton() {
        return document.querySelector('#start');
    }

    function getConfirmButton() {
        return document.querySelector('#confirm');
    }

    function getStatusBox() {
        return document.querySelector('.server-status .status') || document.querySelector('.status');
    }

    function isServerBusyOrOnline() {
        const actions = getServerActions();
        const statusBox = getStatusBox();

        if (!actions && !statusBox) return false;

        const actionClasses = actions ? actions.className : '';
        const statusClasses = statusBox ? statusBox.className : '';

        const combined = `${actionClasses} ${statusClasses}`.toLowerCase();

        return (
            combined.includes('online') ||
            combined.includes('loading') ||
            combined.includes('starting') ||
            combined.includes('queueing')
        );
    }

    function clickIfVisible(el) {
        if (!el) return false;
        if (el.disabled) return false;

        const style = window.getComputedStyle(el);
        if (style.display === 'none' || style.visibility === 'hidden') return false;

        el.click();
        return true;
    }

    function updateButton() {
        const btn = document.querySelector('#tm-autostart-btn');
        if (!btn) return;

        btn.textContent = botON ? 'AutoStart: ON' : 'AutoStart: OFF';
        btn.style.background = botON ? '#308fe3' : '#999999';
    }

    function tick() {
        if (!botON) return;

        try {
            if (!isServerBusyOrOnline()) {
                const confirmBtn = getConfirmButton();
                const startBtn = getStartButton();

                // Confirm first if present, then Start
                if (clickIfVisible(confirmBtn)) {
                    log('Clicked confirm');
                }

                if (clickIfVisible(startBtn)) {
                    log('Clicked start');
                }
            }
        } catch (err) {
            console.error('[Aternos AutoStart] Tick error:', err);
        }
    }

    function startLoop() {
        stopLoop();
        tick();
        loopTimer = setInterval(tick, 1200);
    }

    function stopLoop() {
        if (loopTimer) {
            clearInterval(loopTimer);
            loopTimer = null;
        }
    }

    function toggleBot() {
        botON = !botON;
        updateButton();

        if (botON) {
            log('Bot enabled');
            startLoop();
        } else {
            log('Bot disabled');
            stopLoop();
        }
    }

    function createButton() {
        if (document.querySelector('#tm-autostart-btn')) return;

        const actions = getServerActions();
        if (!actions) return;

        const btn = document.createElement('button');
        btn.id = 'tm-autostart-btn';
        btn.type = 'button';
        btn.textContent = 'AutoStart: OFF';

        btn.style.fontSize = '18px';
        btn.style.fontWeight = '700';
        btn.style.padding = '10px 16px';
        btn.style.marginTop = '10px';
        btn.style.marginLeft = '0';
        btn.style.border = 'none';
        btn.style.borderRadius = '6px';
        btn.style.cursor = 'pointer';
        btn.style.color = '#fff';
        btn.style.background = '#999999';
        btn.style.display = 'block';
        btn.style.width = '100%';
        btn.style.maxWidth = '260px';

        btn.addEventListener('click', toggleBot);

        actions.insertAdjacentElement('afterend', btn);
        updateButton();

        log('Button inserted');
    }

    function ensureUI() {
        createButton();
    }

    function initObserver() {
        if (observer) observer.disconnect();

        observer = new MutationObserver(() => {
            ensureUI();
        });

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

    function init() {
        ensureUI();
        initObserver();
        log('Loaded');
    }

    if (document.readyState === 'loading') {
        window.addEventListener('DOMContentLoaded', init, { once: true });
    } else {
        init();
    }
})();