Max Buy

Auto-maxes the items in Torn shops and bazaars

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Max Buy
// @namespace    https://greatest.deepsurf.us/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);
}