Max Buy

Auto-maxes the items in Torn shops and bazaars

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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);
}