TheresMoreHelp

Helper for TheresMoreGame

目前為 2022-12-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        TheresMoreHelp
// @namespace   TheresMoreGame.com
// @match       https://www.theresmoregame.com/play/
// @grant       none
// @version     1.0
// @description Helper for TheresMoreGame
// @license     MIT
// ==/UserScript==

window.addEventListener('DOMContentLoaded', function () {
  ;(async () => {
    const formatTime = (timeToFormat) => {
      const timeValues = {
        seconds: 0,
        minutes: 0,
        hours: 0,
        days: 0,
      }

      let timeShort = ''
      let timeLong = ''

      timeValues.seconds = timeToFormat % 60
      timeToFormat = (timeToFormat - (timeToFormat % 60)) / 60
      timeValues.minutes = timeToFormat % 60
      timeToFormat = (timeToFormat - (timeToFormat % 60)) / 60
      timeValues.hours = timeToFormat % 24
      timeToFormat = (timeToFormat - (timeToFormat % 24)) / 24
      timeValues.days = timeToFormat

      if (timeValues.days) {
        timeShort += `${timeValues.days}d `
        timeLong += `${timeValues.days} days `
      }
      if (timeValues.hours) {
        timeShort += `${timeValues.hours}h `
        timeLong += `${timeValues.hours} hrs `
      }
      if (timeValues.minutes) {
        timeShort += `${timeValues.minutes}m `
        timeLong += `${timeValues.minutes} min `
      }
      if (timeValues.seconds) {
        timeShort += `${timeValues.seconds}s `
        timeLong += `${timeValues.seconds} sec `
      }

      timeShort = timeShort.trim()
      timeLong = timeLong.trim()

      return {
        timeShort,
        timeLong,
        timeValues,
      }
    }

    const findResource = (resourceName = 'Gold') => {
      let resourceFound = false
      let resourceToFind = { name: resourceName, current: 0, max: 0, speed: 0, ttf: null, ttz: null }
      const resources = document.querySelectorAll('#root div > div > div > table > tbody > tr > td:nth-child(1) > span')
      resources.forEach((resource) => {
        if (resource.textContent.includes(resourceName)) {
          resourceFound = true
          const values = resource.parentNode.parentNode.childNodes[1].textContent.split('/').map((x) =>
            Number(
              x
                .replace(/[^0-9\-,\.]/g, '')
                .replace(',', '.')
                .trim()
            )
          )
          resourceToFind.current = values[0]
          resourceToFind.max = values[1]

          resourceToFind.speed = Number(
            resource.parentNode.parentNode.childNodes[2].textContent
              .replace(/[^0-9\-,\.]/g, '')
              .replace(',', '.')
              .trim()
          )

          resourceToFind.ttf =
            resourceToFind.speed > 0 && resourceToFind.max !== resourceToFind.current
              ? formatTime(Math.ceil((resourceToFind.max - resourceToFind.current) / resourceToFind.speed))
              : null
          resourceToFind.ttz =
            resourceToFind.speed < 0 && resourceToFind.current ? formatTime(Math.ceil(resourceToFind.current / (resourceToFind.speed * -1))) : null
        }
      })

      return resourceFound ? resourceToFind : null
    }

    const calculateTTF = () => {
      const resourceTrNodes = document.querySelectorAll('#root > div > div:not(#maintabs-container) > div > div > div > table:not(.hidden) > tbody > tr')
      resourceTrNodes.forEach((row) => {
        const cells = row.querySelectorAll('td')
        const resourceName = cells[0].textContent.trim()
        const resource = findResource(resourceName)
        let ttf = ''

        if (resource && resource.current < resource.max && resource.speed) {
          ttf = resource.ttf ? resource.ttf.timeLong : resource.ttz.timeLong
        }

        if (!cells[3]) {
          const ttfElement = document.createElement('td')
          ttfElement.className = 'px-3 3xl:px-5 py-3 lg:py-2 3xl:py-3 whitespace-nowrap w-1/3 text-right'
          ttfElement.textContent = ttf
          row.appendChild(ttfElement)
        } else {
          cells[3].textContent = ttf
        }
      })
    }

    const calculateTippyTTF = () => {
      let potentialResourcesToFillTable = document.querySelectorAll('div.tippy-box > div.tippy-content > div > div > table')
      if (potentialResourcesToFillTable.length) {
        potentialResourcesToFillTable = potentialResourcesToFillTable[0]
        const rows = potentialResourcesToFillTable.querySelectorAll('tr')
        rows.forEach((row) => {
          const cells = row.querySelectorAll('td')
          const resourceName = cells[0].textContent.trim()

          const resource = findResource(resourceName)
          if (resource) {
            let ttf = '✅'

            const target = Number(
              cells[1].textContent
                .split(' ')
                .shift()
                .replace(/[^0-9\-,\.]/g, '')
                .replace(',', '.')
                .trim()
            )

            if (target > resource.max || resource.speed <= 0) {
              ttf = 'never'
            } else if (target > resource.current) {
              ttf = formatTime(Math.ceil((target - resource.current) / resource.speed)).timeShort
            }

            if (!cells[2]) {
              const ttfElement = document.createElement('td')
              ttfElement.className = 'px-4 3xl:py-1 text-right'
              ttfElement.textContent = ttf
              row.appendChild(ttfElement)
            } else {
              cells[2].textContent = ttf
            }
          }
        })
      }
    }

    const performRoutineTasks = () => {
      calculateTTF()
    }

    const performFastTasks = () => {
      calculateTippyTTF()
    }

    this.setInterval(performRoutineTasks, 1000)
    this.setInterval(performFastTasks, 100)
  })()
})