Territorial.io Auto-Join & Play (F6 Trigger + Proxy Support)

On F6 press, clicks Multiplayer/Ready, then auto-spams Space. Press F6 again to toggle spam. Works on territorial.io and CroxyProxy.

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

// ==UserScript==
// @name         Territorial.io Auto-Join & Play (F6 Trigger + Proxy Support)
// @namespace    Violentmonkey Scripts
// @version      5.0
// @description  On F6 press, clicks Multiplayer/Ready, then auto-spams Space. Press F6 again to toggle spam. Works on territorial.io and CroxyProxy.
// @author       Assistant
// @match        https://territorial.io/*
// @match        https://*.croxyproxy.com/*territorial.io*
// @match        https://*/*?__cpo=*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // --- State Variables ---
    let isAutomationRunning = false; // Prevents starting the click sequence multiple times
    let isSpammingActive = false;    // Tracks if the spacebar loop is active
    let spaceSpamIntervalId = null;  // Stores the ID of the setInterval loop

    /**
     * Checks if the current page is territorial.io, either directly or via a proxy.
     */
    function isTerritorialPage() {
        const href = window.location.href;
        if (href.includes('territorial.io')) return true;
        if (href.includes('?__cpo=')) {
            try {
                return atob(new URLSearchParams(window.location.search).get('__cpo')).includes('territorial.io');
            } catch (e) {
                return false;
            }
        }
        return false;
    }

    // Stop immediately if this isn't a territorial.io page.
    if (!isTerritorialPage()) return;

    console.log('[Auto-Join & Play] Script ready. Press F6 to start. Press F6 again to toggle space spam.');

    // Listen for the F6 key press to control the script
    document.addEventListener('keydown', (event) => {
        if (event.key === 'F6') {
            event.preventDefault();

            if (isSpammingActive) {
                // If spam is active, F6 stops it.
                stopSpaceSpam();
            } else if (!isAutomationRunning) {
                // If nothing is running, F6 starts the whole sequence.
                isAutomationRunning = true;
                startAutomation();
            } else {
                console.warn('[Auto-Join & Play] Automation sequence is already running.');
            }
        }
    });

    /**
     * Starts the initial sequence of finding and clicking buttons.
     */
    function startAutomation() {
        console.log('[Auto-Join & Play] F6 pressed. Starting automation...');
        waitForElementAndClick(findMultiplayerButton, () => {
            waitForElementAndClick(findReadyButton, () => {
                // Success! The "Ready" button was clicked.
                isAutomationRunning = false; // The initial click sequence is done.
                startSpaceSpam(); // Now, start the spacebar loop.
            }, "'Ready' button", () => { isAutomationRunning = false; });
        }, "'Multiplayer' button", () => { isAutomationRunning = false; });
    }

    /**
     * Starts the spacebar spamming loop.
     */
    function startSpaceSpam() {
        if (isSpammingActive) return; // Don't start if already running
        isSpammingActive = true;
        console.log('[Auto-Join & Play] Starting spacebar spam. Press F6 again to stop.');
        // Spam the spacebar every 150 milliseconds
        spaceSpamIntervalId = setInterval(simulateSpacePress, 150);
    }

    /**
     * Stops the spacebar spamming loop.
     */
    function stopSpaceSpam() {
        if (!isSpammingActive) return;
        clearInterval(spaceSpamIntervalId);
        spaceSpamIntervalId = null;
        isSpammingActive = false;
        console.log('[Auto-Join & Play] Spacebar spam stopped.');
    }

    /**
     * Creates and dispatches a Space key press event.
     */
    function simulateSpacePress() {
        // We dispatch the event on the document, which is where most games listen for input.
        document.dispatchEvent(new KeyboardEvent('keydown', {
            'key': ' ',
            'code': 'Space',
            'keyCode': 32,
            'which': 32,
            'bubbles': true,
            'cancelable': true
        }));
    }


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

    function waitForElementAndClick(findFunction, successCallback, description, timeoutCallback) {
        let attempts = 0;
        const maxAttempts = (TIMEOUT_SECONDS * 1000) / CHECK_INTERVAL_MS;
        console.log(`[Auto-Join & Play] Searching for: ${description}`);

        const interval = setInterval(() => {
            if (attempts >= maxAttempts) {
                clearInterval(interval);
                console.error(`[Auto-Join & Play] Timed out waiting for: ${description}`);
                if (timeoutCallback) timeoutCallback();
                return;
            }
            const element = findFunction();
            if (element) {
                clearInterval(interval);
                console.log(`[Auto-Join & Play] Found and clicking: ${description}`);
                element.click();
                if (successCallback) successCallback();
            }
            attempts++;
        }, CHECK_INTERVAL_MS);
    }

    const findMultiplayerButton = () => Array.from(document.querySelectorAll('button')).find(btn => btn.innerText.includes('Multiplayer'));
    const findReadyButton = () => Array.from(document.querySelectorAll('button')).find(btn => btn.innerText.trim().startsWith('Ready'));

})();

QingJ © 2025

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