Territorial.io Auto-Join (F6 Trigger)

On F6 key press, clicks the Multiplayer and then the Ready button to join a game on territorial.io.

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

// ==UserScript==
// @name         Territorial.io Auto-Join (F6 Trigger)
// @namespace    Violentmonkey Scripts
// @version      3.0
// @description  On F6 key press, clicks the Multiplayer and then the Ready button to join a game on territorial.io.
// @author       Assistant
// @match        https://territorial.io/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // --- AUTOMATION SCRIPT (F6 TRIGGER) ---
    console.log('[Auto-Join] Script ready. Press F6 to start the automation sequence.');
    let isAutomationRunning = false;

    // Listen for the F6 key press anywhere on the page
    document.addEventListener('keydown', (event) => {
        if (event.key === 'F6') {
            event.preventDefault(); // Prevent the default browser action for F6
            if (isAutomationRunning) {
                console.warn('[Auto-Join] Automation is already in progress.');
                return;
            }
            isAutomationRunning = true;
            startAutomation();
        }
    });

    /**
     * Starts the sequence of finding and clicking the buttons.
     */
    function startAutomation() {
        console.log('[Auto-Join] F6 pressed. Starting automation...');
        // 1. Wait for and click the "Multiplayer" button.
        waitForElementAndClick(findMultiplayerButton, () => {
            // 2. After "Multiplayer" is clicked, wait for and click the "Ready" button.
            waitForElementAndClick(findReadyButton, () => {
                console.log("[Auto-Join] Sequence complete.");
                isAutomationRunning = false; // Allow the script to run again
            }, "'Ready' button", () => { isAutomationRunning = false; }); // Callback for timeout
        }, "'Multiplayer' button", () => { isAutomationRunning = false; }); // Callback for timeout
    }

    // --- HELPER FUNCTIONS FOR CLICKING ---
    const TIMEOUT_SECONDS = 15;
    const CHECK_INTERVAL_MS = 200;

    /**
     * A robust function that waits for an element to appear, then clicks it.
     * Includes callbacks for success and timeout to manage the 'isAutomationRunning' flag.
     */
    function waitForElementAndClick(findFunction, successCallback, description, timeoutCallback) {
        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 waiting for: ${description}`);
                if (timeoutCallback) timeoutCallback();
                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 by its text content.
     */
    const findMultiplayerButton = () => Array.from(document.querySelectorAll('button')).find(btn => btn.innerText.includes('Multiplayer'));

    /**
     * Finds the "Ready" button by its text content.
     */
    const findReadyButton = () => Array.from(document.querySelectorAll('button')).find(btn => btn.innerText.trim().startsWith('Ready'));

})();

QingJ © 2025

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