TC Bazaar+ v2

description

目前為 2020-09-20 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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)
      }
    })
  })
}