Torn Item Market Max Quantity Calculator

Calculates maximum affordable quantity when using max button in item market

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

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

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

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

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