Item Page Total Value

Shows the market value of each item stack on your items page.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Item Page Total Value
// @namespace    https://arsonwarehouse.com
// @version      1.1
// @description  Shows the market value of each item stack on your items page.
// @author       Sulsay [2173590]
// @match        https://www.torn.com/item.php
// @grant        none
// @run-at       document-start
// ==/UserScript==

const settings = {
    tornApiKey: 'YOUR_KEY_HERE',
};

const marketValueByItemId = new Map();

(function() {
    onLoadItems(insertValueIntoItemsOfVisibleCategory);
})();

async function insertValueIntoItemsOfVisibleCategory() {
    const itemListItems = Array.from(getVisibleCategory().children).filter(li => li.hasAttribute('data-item'));
    const itemIds = itemListItems.map(item => parseInt(item.dataset.item, 10));

    try {
        await ensureMarketValueIsLoadedForItems(itemIds);
        itemListItems.forEach(insertValueIntoItemRow);
    } catch (error) {
        alert('The user script "Item Page Total Value" could not load items data. Did you enter your API key?\n\nTorn API says: ' + error.message);
    }
}

function insertValueIntoItemRow(itemListItem) {
    if (itemListItem.dataset.valueIsInserted === 'true') {
        return;
    }

    const itemId = parseInt(itemListItem.dataset.item, 10);
    const nameWrapSpan = itemListItem.querySelector('.name-wrap');
    const firstQuantitySpan = nameWrapSpan.querySelector('.qty');
    const quantity = parseInt(firstQuantitySpan.innerText.trim().replace('x', ''), 10) || 1;

    nameWrapSpan.insertAdjacentHTML('beforeend', `<span style="position:absolute;right:.5rem;color:#B53471">${formatCurrency(quantity * marketValueByItemId.get(itemId))}</span>`);
    itemListItem.dataset.valueIsInserted = 'true';
}

function ensureMarketValueIsLoadedForItems(itemIds) {
    const itemIdsToFetch = [];
    for (let itemId of itemIds) {
        if (! marketValueByItemId.has(itemId)) {
            itemIdsToFetch.push(itemId);
        }
    }

    if (itemIdsToFetch.length === 0) {
        return Promise.resolve();
    }

    return fetch('https://api.torn.com/torn/' + itemIdsToFetch.join(',') + '?selections=items&key=' + settings.tornApiKey)
        .then(response => response.json())
        .then(response => {
            if (response.items === undefined) {
                throw Error((response.error && response.error.error) || 'Unknown error');
            }
            return response.items;
        })
        .then(populateMarketValueByItemId);
}

function populateMarketValueByItemId(items) {
    Object.entries(items).forEach(([itemId, apiData]) => {
        marketValueByItemId.set(parseInt(itemId, 10), apiData.market_value);
    });
}

function onLoadItems(onLoadHandler) {
    const xhrOpen = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function (method, url) {
        if (method.toUpperCase() === 'POST' && url.substr(0, 14) === 'item.php?rfcv=') {
            this.onload = onLoadHandler;
        }
        xhrOpen.apply(this, arguments);
    };
}

function getVisibleCategory() {
    return Array.from(document.getElementById('category-wrap').children).find(child => {
        return child.classList.contains('items-cont') && child.style.display !== 'none';
    });
}

function formatCurrency(value) {
    return '$' + value.toLocaleString('en-US');
}