Brick Hill True Value

Replaces average values with brick hill trade values in the trade tab.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Brick Hill True Value
// @version      0.2
// @description  Replaces average values with brick hill trade values in the trade tab.
// @author       Noah Cool Boy
// @match        https://www.brick-hill.com/trades
// @match        https://www.brick-hill.com/trades/
// @icon         https://www.google.com/s2/favicons?domain=brick-hill.com
// @namespace https://greasyfork.org/users/725966
// ==/UserScript==

(function () {

    let api = "https://brick-hill.trade/v1/api/extension/item/"
    let cache = {}
    let queue = []
    let busy = false
    let observer = new MutationObserver(refreshValues)

    function getItems() {
        let items = [...document.querySelectorAll(".item-card-tile")]
        return items.map(item => {
            return {
                avg: item.querySelector(".light-gray-text"),
                value: item.querySelector("span:last-child"),
                itemId: parseInt(item.querySelector("a").href.match(/\d+/)),
                item: item
            }
        })
    }

    function getValue() {
        if (queue.length == 0) {
             busy = false
            doTotal()


            return
        }

        let item = queue.shift()
        console.log(cache)
        if(cache[item.itemId]) {
            item.avg.innerText = "Val."
            item.value.innerText = cache[item.itemId]
            getValue()
            return
        }

        let xhr = new XMLHttpRequest()
        xhr.open("GET", api + item.itemId)
        xhr.responseType = "json"
        xhr.onload = function () {
            console.log("Requesting value for: " + xhr.response.item.value)
            if (xhr.response.status == "success") {
                item.avg.innerText = "Val."
                item.value.innerText = xhr.response.item.value
                cache[item.itemId] = xhr.response.item.value
            }
            getValue()
        }
        xhr.send()
    }

    function doTotal() {
        let items = getItems()
        let side1 = items.filter(v => document.querySelector("#viewtrades-v > .col-2-3 > div > :nth-child(2)").contains(v.avg))
        let side2 = items.filter(v => document.querySelector("#viewtrades-v > .col-2-3 > div > :nth-child(4)").contains(v.avg))
        document.querySelectorAll(".smedium-text")[0].innerText = "Total Value: " + side1.reduce((acc, cur) => acc + cache[cur.itemId], 0)
        document.querySelectorAll(".smedium-text")[1].innerText = "Total Value: " + side2.reduce((acc, cur) => acc + cache[cur.itemId], 0)

        observer.observe(document.querySelector("#viewtrades-v > .col-2-3"), {childList: true, subtree:true})
    }

    function refreshValues() {
            if (!busy) {
                busy = true
                queue = getItems()
                observer.disconnect();
                getValue()
            }
    }

    let a = setInterval(() => {
        if(!document.querySelector("#viewtrades-v > .col-2-3")) return
        clearInterval(a);
        observer.observe(document.querySelector("#viewtrades-v > .col-2-3"), {childList: true, subtree:true})
    }, 100)
})()