TC Bazaar+ v2

description

当前为 2020-09-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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)
      }
    })
  })
}