Territorial.io Auto-Join Multiplayer

Automatically clicks the Multiplayer and Ready buttons to join a game on territorial.io. Works on CroxyProxy.

当前为 2025-06-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Territorial.io Auto-Join Multiplayer
// @namespace    Violentmonkey Scripts
// @version      1.1
// @description  Automatically clicks the Multiplayer and Ready buttons to join a game on territorial.io. Works on CroxyProxy.
// @author       Assistant
// @match        https://territorial.io/*
// @match        https://*.croxyproxy.com/*territorial.io*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const TIMEOUT_SECONDS = 15; // How many seconds to wait for each button before giving up.
    const CHECK_INTERVAL_MS = 200; // How often to check for the button (in milliseconds).

    /**
     * A helper function that waits for an element to appear in the DOM and then clicks it.
     * @param {function} findFunction - A function that returns the DOM element when found, or null/undefined if not found.
     * @param {function} successCallback - A function to call after the element has been successfully clicked.
     * @param {string} description - A description of the element being searched for, used for logging.
     */
    function waitForElementAndClick(findFunction, successCallback, description) {
        let attempts = 0;
        const maxAttempts = (TIMEOUT_SECONDS * 1000) / CHECK_INTERVAL_MS;

        console.log(`[Auto-Join] Searching for: ${description}`);

        const interval = setInterval(() => {
            if (attempts >= maxAttempts) {
                clearInterval(interval);
                console.error(`[Auto-Join] Timed out after ${TIMEOUT_SECONDS} seconds waiting for: ${description}`);
                return;
            }

            const element = findFunction();

            if (element) {
                clearInterval(interval);
                console.log(`[Auto-Join] Found and clicking: ${description}`);
                element.click();
                if (successCallback) {
                    successCallback();
                }
            }
            attempts++;
        }, CHECK_INTERVAL_MS);
    }

    /**
     * Finds the "Multiplayer" button. It checks the text content, which is more reliable than styles.
     * @returns {HTMLElement|null} The button element or null.
     */
    const findMultiplayerButton = () => {
        const buttons = document.querySelectorAll('button');
        // The innerText of the button is "⚔️\nMultiplayer", so we check if it includes "Multiplayer".
        return Array.from(buttons).find(btn => btn.innerText.includes('Multiplayer'));
    };

    /**
     * Finds the "Ready" button. It checks if the text content starts with "Ready".
     * @returns {HTMLElement|null} The button element or null.
     */
    const findReadyButton = () => {
        const buttons = document.querySelectorAll('button');
        // The button contains other text in spans, so we check if it starts with "Ready".
        return Array.from(buttons).find(btn => btn.innerText.trim().startsWith('Ready'));
    };


    // --- Main Execution Logic ---

    // 1. Start by waiting for the "Multiplayer" button.
    waitForElementAndClick(findMultiplayerButton, () => {
        // 2. Once the "Multiplayer" button is clicked, this callback runs.
        //    Now, we wait for the "Ready" button to appear on the next screen.
        waitForElementAndClick(findReadyButton, () => {
             // 3. This callback runs after the "Ready" button is clicked.
            console.log("[Auto-Join] Successfully clicked 'Ready'. Script sequence complete.");
        }, "'Ready' button");
    }, "'Multiplayer' button");

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址