Max Buy

Auto-maxes the items in Torn shops and bazaars

安裝腳本?
作者推薦腳本

您可能也會喜歡 Fill Bank Vault

安裝腳本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
}