Theresmore自动升级建筑

自动升级建筑,需要开在建造的tab页

目前为 2022-12-12 提交的版本。查看 最新版本

// ==UserScript==
// @name        Theresmore自动升级建筑
// @namespace   Theresmore自动升级建筑
// @match       https://theresmoregame.g8hh.com/
// @grant       none
// @version     1.2
// @author      fbz
// @description 自动升级建筑,需要开在建造的tab页
// @license     MIT
// ==/UserScript==
(function(){
  const timeout = 1000 * 10 //10秒点一次

  let blackList = initBlackList() // 部分只能建造一个的建筑需要跳过
  const houseList = ['房屋', '市政厅', '宅邸'] // 会减少食物的建筑
  function initBlackList() {
    return ['雕像','神殿']
  }
  var css = `
    #auto_update_btn {
      position: fixed;
      right: 16px;
      bottom: 64px;
      background: #1d1e20;
      border-radius: 50%;
      width: 48px;
      height: 48px;
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center;
      font-size: 14px;
      border: 2px solid white;
      z-index: 100;
      color: white;
      cursor: pointer;
    }
  `
  /*添加样式*/
  function addStyle(css) {
    if (!css) return
    var head = document.querySelector('head');
    var style = document.createElement("style");
    style.type = 'text/css'
    style.innerHTML = css
    head.appendChild(style)
  }


  /*生成自动升级建筑的按钮*/
  function createBtn() {
    var btn = document.createElement("div")
    btn.title = '开启'
    var span = document.createElement("span");
    span.innerText = '开启'
    btn.appendChild(span);
    btn.id = 'auto_update_btn'
    document.body.appendChild(btn)

    /*初始化事件*/
    // 点击按钮启动定时器
    btn.addEventListener('click', function() {
      toggleBtnStatus()
      toggleBtnText()
    })
  }

  // 切换文字
  function toggleBtnText(){
    const node = document.querySelector('#auto_update_btn')
    const text = node.innerText
    node.innerText = text === '开启' ? '关闭' : '开启'
  }

  // 自动升级建筑
  function autoClickBuilding(){
    closeDialog()
    const container = document.querySelector(`div[aria-labelledby="headlessui-tabs-tab-:r0:"]`)
    if(!container) {
      // 自动切换到建造tab页
      const tabNode = document.getElementById('headlessui-tabs-tab-:r0:')
      tabNode && tabNode.click()
    }
    const list = container.querySelectorAll(`button.btn`)
    judgeFood() // 食物小于3时不建造房屋
    for(const node of list){
      if(!node.classList.value.includes('btn-off') && !blackList.some(word=>node.textContent.includes(word))){
        console.log(`${new Date().toLocaleString()}升级:`,node.textContent)
        node.click()
        break
      }
    }
  }
  let buildingInterval = null

  // 开启自动升级建筑定时器
  function handleAutoUpdateStart(){
    buildingInterval = setInterval(autoClickBuilding, timeout)
  }
  // 清除自动升级建筑定时器
  function handleAutoUpdateClear(){
    buildingInterval = clearInterval(buildingInterval)
  }
  // 切换自动升级建筑定时器状态
  function toggleBtnStatus(){
    if(buildingInterval) {
      handleAutoUpdateClear()
    } else {
      handleAutoUpdateStart()
    }
  }
  // 判断食物数量
  function judgeFood() {
    var list = document.querySelector('table').querySelectorAll('tr')
    for(var node of list){
      if(!node.innerText.includes('食物')) continue
    		// 获取食物数量
      var val = Number(node.childNodes[2].innerText.split('/')[0])
      if(val < 3) {
        blackList.push(...houseList)
      } else {
        blackList = initBlackList()
      }
    }
  }
  // 关闭dialog
  function closeDialog(){
    const dialogNode = document.querySelector('#headlessui-portal-root')
    if(!dialogNode) return
    dialogNode.querySelector('.sr-only').parentNode.click()
  }

  createBtn()
  addStyle(css)
})()

QingJ © 2025

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