TORN: Prefill Max Buy Bazaar

Prefills the bazaar buy to the max possible

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         TORN: Prefill Max Buy Bazaar
// @namespace    http://torn.city.com.dot.com.com
// @version      1.0.3
// @description  Prefills the bazaar buy to the max possible
// @author       IronHydeDragon[2428902]
// @match        https://www.torn.com/bazaar.php*
// @license      MIT
// ==/UserScript==

async function requireElement(
  selectors,
  conditionsCallback,
  queryElement = document
) {
  try {
    await new Promise((res, rej) => {
      const maxCycles = 500;
      let current = 1;
      const interval = setInterval(() => {
        if (queryElement.querySelector(selectors)) {
          if (!conditionsCallback) {
            clearInterval(interval);
            return res();
          }
          if (
            conditionsCallback &&
            conditionsCallback(queryElement.querySelector(selectors))
          ) {
            clearInterval(interval);
            return res();
          }
        }
        if (current === maxCycles) {
          clearInterval(interval);
          rej('Timeout: Could not find element on page');
        }
        current++;
      }, 10);
    });
  } catch (err) {
    console.error(err);
  }
}

function set_react_input(input, newval) {
  let old_value = input.value;
  input.value = newval;
  input._valueTracker.setValue(old_value);
  input.dispatchEvent(new Event('input', { bubbles: true, simulated: true }));
  input.select();
}

async function getMoneyOnHand() {
  await requireElement('#user-money');
  const onhand = +document.querySelector('#user-money').dataset.money;

  return onhand;
}

async function prefillMaxAmount(inputEl, price, qtyAvailable) {
  price = +price;
  qtyAvailable = +qtyAvailable;

  const onhand = await getMoneyOnHand();
  if (onhand === 0) {
    inputEl.value = 0;
    inputEl.dispatchEvent(new Event('input', { bubbles: true }));
    return;
  }

  if (onhand > 0) {
    const maxAmount = Math.floor(onhand / price);
    const newValue = qtyAvailable ? `${qtyAvailable}` : `${maxAmount}`;
    set_react_input(inputEl, newValue);
  }
}

async function bazaarObserver(mutationList, observer) {
  for (const mutation of mutationList) {
    // if (
    //   !mutation.target.classList.contains('progress-line-timer___uV1ZZ') &&
    //   !mutation.target.classList.contains('news-ticker-slide') &&
    //   !mutation.target.classList.contains('progress___z5tk3') &&
    //   mutation.target.id !== 'news-ticker-slider-wrapper'
    // ) {
    //   // console.log('👀', mutation); // TEST
    // }

    if (
      mutation.target.classList.contains('item___GYCYJ') &&
      mutation.addedNodes?.length > 0 &&
      mutation.addedNodes[0].classList.contains('buyMenu___PJqGH')
    ) {
      const amountInput = mutation.target.querySelector(
        '.buyAmountInput___CSV2n'
      );
      const price = mutation.target
        .querySelector('.price___q9kzH')
        .textContent.slice(1)
        .split(',')
        .join('');

      await requireElement('.amount___jF6kC', null, mutation.target);
      const qtyAvailable = mutation.target
        .querySelector('.amount___jF6kC')
        .textContent.match(/(\d.*\d|\d)/)[0]
        .split(',')
        .join('');

      prefillMaxAmount(amountInput, price, qtyAvailable);
      console.log(mutation); // TEST
    }
  }
}

function createBazaarObserver() {
  const observer = new MutationObserver(bazaarObserver);
  observer.observe(document, {
    attributes: false,
    childList: true,
    subtree: true,
  });
}

(async () => {
  console.log('🤑 Prefill max buy bazaar script is ON!'); // TEST
  createBazaarObserver();
  document.addEventListener('input', (e) => console.log(e)); // TEST)
})();