TC Bazaar+ v2

description

2020-09-20 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         TC Bazaar+ v2
// @namespace    namespace
// @version      0.2
// @description  description
// @author       tos
// @match        *.torn.com/bazaar.php*
// @match        *.torn.com/index.php*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

const api_key = 'YOUR_API_KEY'

const event = new Event('input', {bubbles: true, simulated: true})

document.addEventListener('dblclick', (e) => {
  console.log(e)
  if (e.target && e.target.tagName && e.target.tagName === 'INPUT') {
    if (e.target.className.includes('buyAmountInput')) max_buy(e.target) //other bazaar buy
    else if (e.target.id.includes('item')) foriegn_max(e.target) //foreign buy
    else if (e.target.className.includes('input-money')) auto_price(e.target) //my bazaar add
    else if (e.target.className.includes('priceInput')) auto_price_manage(e.target) //my bazaar manage
    else if (e.target.className === 'clear-all') max_qty(e.target) //my bazaar qty add
    else if (e.target.className.includes('numberInput')) max_qty_rem(e.target) //my bazaar qty remove
  }
})

//other bazaar buy
function max_buy(input) {
  let old_value = input.value
  input.value = input.max
  input._valueTracker.setValue(old_value)
  input.dispatchEvent(event)
  input.select()
}

//foreign buy
function foriegn_max (input) {
  const i = document.querySelector('div.user-info div.msg').innerText.match(/(\d+).\/.(\d+)/)
  input.value = parseInt(i[2]) - parseInt(i[1])
  input.select()
}

//my bazaar add
let torn_items = null
async function auto_price(input) {
  if (!torn_items) torn_items = await torn_api('torn..items').then(r => Object.fromEntries(Object.entries(r.items).map(([itemID, properties]) => [properties.name, itemID])))
  const item_name = input.closest('LI').querySelector('canvas.item-converted').getAttribute('aria-label')
  const market_prices = await torn_api(`market.${parseInt(torn_items[item_name])}.bazaar,itemmarket`)
  const lowest_price = Object.values(market_prices).reduce((acc, cur) => acc.concat(cur)).reduce((a, c) => a < c.cost ? a : c.cost)
  input.value = lowest_price - 1
  input.dispatchEvent(event)
  input.select()
}

//my bazaar manage
async function auto_price_manage (input) {
  const itemID = input.closest('div[class^=row]').querySelector('img').src.split('items/')[1].split('/')[0]
  const market_prices = await torn_api(`market.${itemID}.bazaar,itemmarket`)
  const lowest_price = Object.values(market_prices).reduce((acc, cur) => acc.concat(cur)).reduce((a, c) => a < c.cost ? a : c.cost)
  let old_value = input.value
  input.value = lowest_price - 1
  input._valueTracker.setValue(old_value)
  input.dispatchEvent(event)
  input.select()
}

//my bazaar qty add
function max_qty (input) {
  const qty = input.closest('LI').querySelector('div.name-wrap').innerText.match(/x(\d+)/)
  input.value = qty ? qty[1] : 1
  input.dispatchEvent(event)
  input.select()
}

//my bazaar qty remove
function max_qty_rem (input) {
  const qty = input.closest('div[class^=row]').querySelector('div[class^=desc]').innerText.match(/x(\d+)/)
  let old_value = input.value
  input.value = qty ? qty[1] : 1
  input._valueTracker.setValue(old_value)
  input.dispatchEvent(event)
  input.select()
}



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