Brick Hill True Value

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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)
})()