Advertisement
Guest User

3rd Bazaar Auto Price

a guest
Feb 28th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Bazaar Auto Price
  3. // @namespace    tos
  4. // @version      0.6
  5. // @description  description
  6. // @author       tos
  7. // @match        *.torn.com/bazaar.php*
  8. // @grant        GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. const apikey = ''
  12.  
  13. const torn_api = async (args) => {
  14.   const a = args.split('.')
  15.   if (a.length!==3) throw(`Bad argument in torn_api(args, key): ${args}`)
  16.   return new Promise((resolve, reject) => {
  17.     GM_xmlhttpRequest ( {
  18.       method: "POST",
  19.       url: `https://api.torn.com/${a[0]}/${a[1]}?selections=${a[2]}&key=${apikey}`,
  20.       headers: {
  21.         "Content-Type": "application/json"
  22.       },
  23.       onload: (response) => {
  24.           try {
  25.             const resjson = JSON.parse(response.responseText)
  26.             resolve(resjson)
  27.           } catch(err) {
  28.             reject(err)
  29.           }
  30.       },
  31.       onerror: (err) => {
  32.         reject(err)
  33.       }
  34.     })
  35.   })
  36. }
  37.  
  38. var event = new Event('keyup')
  39. var APIERROR = false
  40.  
  41. async function lmp(itemID) {
  42.   var bPrices = [];
  43.  
  44.   if(APIERROR === true) return 'API key error'
  45.   const prices = await torn_api(`market.${itemID}.bazaar`)
  46.   if (prices.error) {APIERROR = true; return 'API key error'}
  47.   let lowest_market_price = null
  48.   for (const market in prices) {
  49.     for (const lid in prices[market]) {
  50.       if (lowest_market_price === null) {
  51.           lowest_market_price = prices[market][lid].cost
  52.  
  53.           bPrices.push(prices[market][lid].cost); // Prijs toevoegen aan array
  54.       }
  55.  
  56.       else if (prices[market][lid].cost < lowest_market_price) {
  57.           lowest_market_price = prices[market][lid].cost
  58.  
  59.           bPrices.push(prices[market][lid].cost); // Prijs toevoegen aan array
  60.       }
  61.       else {
  62.           bPrices.push(prices[market][lid].cost); // Prijs toevoegen aan array
  63.       }
  64.     }
  65.   }
  66.  
  67.   bPrices.sort(function(a, b){return a-b}); //Sort array laag naar hoog
  68.  
  69.   lowest_market_price = bPrices[2] // Set de prijs variable gelijk aan de 3rd bazaar
  70.  
  71.   //console.log("Prices Array: " + bPrices);
  72.  
  73.   return lowest_market_price - 1;
  74. }
  75.  
  76. const observer = new MutationObserver((mutations) => {
  77.   for (const mutation of mutations) {
  78.     for (const node of mutation.addedNodes) {
  79.       if (node.classList && node.classList.contains('input-money-group')) {
  80.         const li = node.closest('li.clearfix') || node.closest('li[id^=item]')
  81.         const input = node.querySelector('.input-money[type=text]')
  82.         if (li) {
  83.           const itemID = li.querySelector('img').src.split('items/')[1].split('/medium')[0]
  84.           input.addEventListener('focus', function(e) {
  85.             if (this.id.includes('price-item')) this.value = ''
  86.             if (this.value === '') {
  87.               lmp(itemID).then((price) => {
  88.                 this.value = price
  89.                 this.dispatchEvent(event)
  90.               })
  91.             }
  92.           })
  93.         }
  94.       }
  95.     }
  96.   }
  97. })
  98.  
  99. const wrapper = document.querySelector('#bazaar-page-wrap')
  100. observer.observe(wrapper, { subtree: true, childList: true })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement