Torn Item Market Max Quantity Calculator

Calculates maximum affordable quantity when using max button in item market

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn Item Market Max Quantity Calculator
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Calculates maximum affordable quantity when using max button in item market
// @author       Weav3r
// @match        https://www.torn.com/page.php?sid=ItemMarket*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Helper function to parse element text based on a regex
    function getElementValue(element, regex) {
        if (!element) return null;
        const match = element.textContent.match(regex);
        if (!match) return null;
        return parseInt(match[1].replace(/,/g, ''));
    }

    // Get user's money from either mobile or desktop
    function getUserMoney() {
        const userMoneyElement = document.querySelector('#user-money');
        if (!userMoneyElement) return null;
        return parseInt(userMoneyElement.dataset.money);
    }

    function handleMaxQuantityClick(event) {
        const maxButton = event.target.closest('.input-money-symbol');
        if (!maxButton) return;

        const moneyGroup = maxButton.closest('.input-money-group');
        const sellerRow = moneyGroup?.closest('.sellerRow___AI0m6');
        const quantityInput = moneyGroup?.querySelector('input.input-money:not([type="hidden"])');

        if (!moneyGroup || !sellerRow || !quantityInput) return;

        const userMoney = getUserMoney();
        if (!userMoney) return;

        const priceElement = sellerRow.querySelector('.price___Uwiv2');
        if (!priceElement) return;
        const price = getElementValue(priceElement, /\$([0-9,]+)/);

        if (!price) return;

        const affordableQuantity = Math.floor(userMoney / price);
        quantityInput.value = affordableQuantity;
        quantityInput.dispatchEvent(new Event('input', { bubbles: true }));
    }

    document.addEventListener('click', handleMaxQuantityClick);
})();