Max Buy

Auto-maxes the items in Torn shops and bazaars

安装此脚本
作者推荐脚本

您可能也喜欢Fill Bank Vault

安装此脚本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Max Buy
// @namespace    https://greasyfork.org/en/scripts/398361-max-buy/
// @version      1.1
// @description  Auto-maxes the items in Torn shops and bazaars
// @author       Cryosis7 [926640]
// @match        *www.torn.com/shops.php*
// @match        *www.torn.com/bazaar.php*
// ==/UserScript==

// Max Torn Shops
document.querySelectorAll('.buy-act-wrap').forEach(item => item.querySelector('input[value="1"]').value = 100)

// Places an observer on the bazaar root, when it has loaded the bazaar contents it will call observeItem on each listing in the bazaar.
if (document.querySelector('#bazaarroot'))
    new MutationObserver((mutations) => {
        mutations.forEach(mutation => mutation.addedNodes.forEach(node => {
            if (node.nodeType === Node.ELEMENT_NODE && node.classList && Array.from(node.classList).findIndex(element => element.includes("itemsContainner")) !== -1) {
                if (node.querySelector('button[class^="controlPanelButton"]')) // If full screen
                    node.querySelectorAll('div[class^="rowItems"] > [class^="item"]').forEach(observeItem);
                else // Smaller Screen
                    node.querySelectorAll('div[class^="rowItems"] > [class^="item"] input').forEach(setQuantity)
            }
        }))
    }).observe(document.querySelector('#bazaarroot'), { subtree: true, childList: true })

/**
 * Sets an observer on the item, to watch for when the user clicks on the shopping cart icon and set the quantity
 * @param {Node} item
 */
function observeItem(item) {
    new MutationObserver((mutations, observer) => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length === 1 && mutation.addedNodes[0].nodeType === Node.ELEMENT_NODE &&
                Array.from(mutation.addedNodes[0].classList).findIndex(element => element.includes("buyMenu")) !== -1) {
                setQuantity(mutation.addedNodes[0].querySelector('input'))
                observer.disconnect()
            }
        })
    }).observe(item, { subtree: true, childList: true })
}

/**
 * Sets the quantity of the item to the max value.
 * Uses horribly hacky work around since Torn's now using React.
 * @param {Element} item The input element to set the value of.
 */
function setQuantity(item) {
    let lastValue = item.value;
    item.value = item.getAttribute('max');
    let event = new Event('input', { bubbles: true });
    event.simulated = true;
    let tracker = item._valueTracker;
    if (tracker)
        tracker.setValue(lastValue);
    item.dispatchEvent(event);
}