TORN: No Confirm Item Market

Allows buying items from item market with 1-click

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         TORN: No Confirm Item Market
// @namespace    http://torn.city.com.dot.com.com
// @version      1.0.1
// @description  Allows buying items from item market with 1-click
// @author       IronHydeDragon[2428902]
// @match        https://www.torn.com/imarket.php*
// @license      MIT
// ==/UserScript==

const green = '#678c00';
const greenTranslucent = 'rgba(103, 140, 0, .8)';

const stylesheetHTML = `
  <style>
    .quick-buy {
      background-color: ${greenTranslucent}
    }
  </style>`;

function renderStylesheet() {
  document.head.insertAdjacentHTML('beforeend', stylesheetHTML);
}

function getPrice(itemEl) {
  const priceEl = itemEl.querySelector('.cost');

  const price = priceEl.childNodes[2].textContent
    .match(/\d.*/)[0]
    .trim()
    .split(',')
    .join('');

  return price;
}

function buyItemController() {
  const buyBtnArray = [...document.querySelectorAll('li.buy .buy-link')];
  buyBtnArray.forEach((btn) => {
    const itemEl = btn.closest('li').parentElement.closest('li');
    btn.dataset.action = 'buyItemConfirm';
    btn.dataset.price = getPrice(itemEl);
    btn.classList.add('yes-buy');
    btn.parentElement.classList.add('quick-buy');
  });
}

function observerCallback(mutationList, observer) {
  try {
    for (const mutation of mutationList) {
      if (
        (mutation.target.id === 'item-market-main-wrap' ||
          mutation.target.classList.contains('buy-item-info-wrap')) &&
        mutation.addedNodes &&
        mutation.addedNodes.length > 0
      ) {
        buyItemController();
      }
    }
  } catch (error) {
    console.error(error);
  }
}

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

(async () => {
  console.log('👍 No confirm item market script is ON!');
  renderStylesheet();
  createObserver();
})();