飞向未来

自动砍树,自动升级建筑脚本

// ==UserScript==
// @name         飞向未来
// @namespace    http://tampermonkey.net/
// @description  自动砍树,自动升级建筑脚本
// @version      2024-11-15
// @description  try to take over the world!
// @author       zhowiny
// @match        http://182.43.19.5:9999/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=19.5
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    const interval = 100
    const CONFIG = {
        '自动砍树': '一只大树',
        '升级采集': '采集小屋',
        '升级伐木': '伐木小屋',
        '升级居所': '简陋居所',
    }

    const sleep = async (time) => new Promise(resolve => setTimeout(resolve, time))
    const waitUtil = async (condition) => new Promise((resolve) => {
        const raf = () => condition() ? resolve() : requestAnimationFrame(raf)
        requestAnimationFrame(raf)
    })

    const taskList = new Map()

    const rafFn = async () => {
        taskList.forEach(task => {
            if (!task.running) return
            task.callback?.()
        })
        await sleep(interval)
        requestAnimationFrame(rafFn)
    }

    const addTool = (toolName = '自动砍树', ele) => {
        if (!ele) return
        const container = [...document.querySelectorAll('.header-box .main .left')][0]

        taskList.set(toolName, {
            running: false,
            callback: () => ele?.click()
        })

        const div = document.createElement('div')
        div.innerText = toolName
        div.classList.add('el-button', 'el-button--small')
        div.addEventListener('click', () => {
            const task = taskList.get(toolName)
            task.running = !task.running
            div.innerText = `${task.running ? '关闭' : '开启'}${toolName}`
        })
        container.appendChild(div)
    }

    const init = async (tools) => {
        rafFn()
        await waitUtil(() => [...document.querySelectorAll('.header-box .main .left')][0])
        await sleep(300)

        const buttons = [...document.querySelectorAll('button:has(span:not(:empty):only-child)')]

        Object.entries(tools).forEach(([name, text]) => {
            addTool(name, buttons.filter(btn => btn.innerText.includes(text))[0])
        })
    }
    init(CONFIG)
})();