MarketToShops

Show NPC shop price compared to market value

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MarketToShops
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Show NPC shop price compared to market value
// @author       Gravity(2131364)
// @match        https://www.torn.com/bigalgunshop.php*
// @match        https://www.torn.com/shops.php*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function formatNumber(n) {
        return "$" + n.toLocaleString();
    }

    function addPlaceholder(desc) {
        let compareLine = desc.querySelector(".shop-market-compare");
        if (!compareLine) {
            compareLine = document.createElement("span");
            compareLine.className = "shop-market-compare t-gray-6";
            compareLine.style.display = "block";
            compareLine.textContent = "MvS(loading...)";
            desc.appendChild(compareLine);
        }
        return compareLine;
    }

    async function fetchValue(itemId, compareLine) {
        try {
            let resp = await fetch("https://www.torn.com/page.php?sid=inventory", {
                method: "POST",
                headers: { "Content-Type": "application/x-www-form-urlencoded" },
                body: `itemID=${itemId}`
            });

            let data = await resp.json();

            let rawValue = data.itemValue || "";
            let rawSell = data.itemSell || "";

            let marketValue = parseInt(rawValue.replace(/[$,]/g, ""));
            let sellValue = parseInt(rawSell.replace(/[$,]/g, ""));

            if (isNaN(marketValue) || isNaN(sellValue)) {
                compareLine.textContent = "MvS: (unavailable)";
                return;
            }

            // Profit = sellValue - marketValue
            let diff = sellValue - marketValue;

            compareLine.style.color = diff >= 0 ? "green" : "red";
            compareLine.textContent =
                `MarketDiff: ${formatNumber(diff)}`;

            console.log(`Item ${itemId}: Market=${marketValue}, Sell=${sellValue}, Diff=${diff}`);
        } catch (err) {
            console.error(`Error fetching value for ${itemId}:`, err);
            compareLine.textContent = "Market vs Sell: (error)";
        }
    }

    function init() {
        console.log("Running Torn Shop Sell vs Market script with staggered fetch...");

        const items = document.querySelectorAll(".items-list li");
        let delay = 0;

        items.forEach(li => {
            const desc = li.querySelector(".desc");
            const nameSpan = li.querySelector(".name");

            if (!desc || !nameSpan) return;

            const itemId = nameSpan.id.split("-")[0];
            if (!itemId) return;

            console.log(`Scheduled fetch for ${nameSpan.textContent.trim()} (ID ${itemId}) after ${delay}ms`);

            const compareLine = addPlaceholder(desc);

            // Stagger requests
            setTimeout(() => {
                fetchValue(itemId, compareLine);
            }, delay);

            delay += 250; // 250ms between each request
        });
    }

    init();
})();