Torn City - Bazaar Auto Price

 

Good day guys, I'm sorry if I bring you in an external link.





What does it do?





Focus on the text box and it will fetch you the item price.
It just a one API call.

How to install

1. go to tampermonkey.net
2. install it to your chrome     


4. Click the link to install the code be sure to change YOUR API KEY. then save



The code to copy and paste is in red be sure to change YOUR API KEY


""
// ==UserScript==
// @name         Bazaar Auto Price - PityYouWeak
// @namespace    PityYouWeak
// @version      0.9
// @description  description
// @author       PityYouWeak
// @match        *.torn.com/bazaar.php*
// @grant        GM_xmlhttpRequest
// @updateURL    https://github.com/PityYouWeak/Torn/raw/main/BazaarAutoPricer/bazaarAutoPricer.user.js
// ==/UserScript==

const apikey = 'YOUR API KEY'
const callFromItemMarket = true
const callFromBazaar = true
const lessToTheMarketPrice = 10;

const torn_api = async (args) => {
  const a = args.split('.');
  const b = a[1].split('/');
  //if (a.length!==4) throw(`Bad argument in torn_api(args, key): ${args}`)
  return new Promise((resolve, reject) => {
    GM_xmlhttpRequest ( {
      method: "GET",
      url: `https://api.torn.com/${a[0]}/${b[0]}?selections=${a[3]}&key=${apikey}`,
      headers: {
        "Content-Type": "application/json"
      },
      onload: (response) => {
          try {
            const resjson = JSON.parse(response.responseText)
            resolve(resjson)
          } catch(err) {
              alert(err)
            reject(err)
          }
      },
      onerror: (err) => {
          alert(err)
        reject(err)
      }
    })
  })
}


var event = new Event('keyup')
var APIERROR = false

async function lmp(itemID) {

  if(APIERROR === true) { alert("api ERROR"); return 'API key error' }

  let lowest_market_price = null

  if (callFromItemMarket == true)
  {
     const prices = await torn_api(`market.${itemID}.itemmarket`)
     if (prices.error) {APIERROR = true; return 'API key error'}
     for (const market in prices) {
      for (const lid in prices[market]) {
       if (lowest_market_price === null) lowest_market_price = prices[market][lid].cost
       else if (prices[market][lid].cost < lowest_market_price) lowest_market_price = prices[market][lid].cost
      }
    }
  }

  if (callFromBazaar == true)
  {
   const bazaarPrices = await torn_api(`market.${itemID}.bazaar`)

   if (bazaarPrices.error){APIERROR = true; return 'API key error'}
     for (const market in bazaarPrices) {
      for (const lid in bazaarPrices[market]) {
        if (lowest_market_price === null) lowest_market_price = bazaarPrices[market][lid].cost
        else if (bazaarPrices[market][lid].cost < lowest_market_price) lowest_market_price = bazaarPrices[market][lid].cost
      }
    }
  }

  return lowest_market_price - lessToTheMarketPrice;
}

const observer = new MutationObserver((mutations) => {
  for (const mutation of mutations) {
    for (const node of mutation.addedNodes) {
      if (node.classList) {
        const input = node.querySelector('.input-money[type=text]')
        if (input) {
          const itemID = input.parentElement?.parentElement.parentElement.parentElement.parentElement.querySelector('img').src.split('items/')[1];//li.querySelector('img').src.split('items/')[1]
          input.addEventListener('focus', function(e) {
              let hasRemove = this.parentElement.parentElement.parentElement.querySelector('[class^=remove]');
              if (this.value === '' || hasRemove) {
              lmp(itemID).then((price) => {
                  let itemAmount = this.parentElement?.parentElement.parentElement.parentElement.parentElement.querySelector(".item-amount")?.textContent;
                  let amount = this.parentElement.parentElement.parentElement.querySelector('.clear-all');
                  hack(this,price);
                  if (amount !== null)
                    hack(amount,itemAmount);
                  this.dispatchEvent(event);
              })
            }
          })
        }
      }
    }
  }
})

function hack(inp,price)
{
    const input = inp;
    input.value = price;
    let event = new Event('input', { bubbles: true });
    event.simulated = true;
    let tracker = input._valueTracker;
    if (tracker) {
        tracker.setValue(1);
    }
    input.dispatchEvent(event);
}

const wrapper = document.querySelector('#bazaarRoot')
observer.observe(wrapper, { subtree: true, childList: true })
""
5. Go to your bazaar test if it's working.

6. Change the price based on your liking. Find this line lessToTheMarketPrice =  then change the 10 to any you want, you can do -10 to add to the lowest market price.

If you have any request just post. If you give some donations I'm more than willing to accept.


Edited Jan 25, 2021

Line 84 
from 
const wrapper = document.querySelector('#bazaarroot')
to
const wrapper = document.querySelector('#react-root')


Edited Jan 26, 2021
Added call for bazaar. Please recopy and paste the code.

lines 12 and 13 just set what true where you want the call to be in.

if you want to call from call from item market
const callFromItemMarket = true
const callFromBazaar = false

if you want to call from call from bazaar
const callFromItemMarket = false
const callFromBazaar = true

Edited April 16, 2021
Line 94 removed
this.value = price
this.dispatchEvent(event)

Replace with
const hidden = node.querySelector('.input-money[type=hidden]')
hidden.remove();
var ev = new Event('input', { bubbles: true});
ev.simulated = true;
this.value = price;
this.dispatchEvent(ev);

Update May 04, 2021
Also Works on Yandex Browser with violent monkey

Updated 08/06/2022
  • Added Manage Page Updating'
  • Auto Input on max quantity when hover to price
  • Created a variable for less market price
  • Added Git Hub link 

Comments

Popular Posts