Merlin-R / Torn Bazaar Buttons

// ==UserScript==
// @name         Torn Bazaar Buttons
// @version      0.1
// @description  Add Items Quickly
// @author       Merlin Reichwald
// @match        https://www.torn.com/bazaar.php
// @copyright 2018, Merlin-R (https://openuserjs.org/users/Merlin-R)
// @license MIT
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  $(() => {
    // Set Value of Torn Inputs and refresh their state. May throw react errors but achieves its goal regardless
    $.fn.tornInputVal = function (value) {
      return $(this).val(value).each((i, e) => e.dispatchEvent(new Event("input", {
        bubbles: true
      })));
    }
    // Triggers Torn Button Click
    $.fn.tornClick = function () {
      return $(this).each((i, e) => e.dispatchEvent(new Event("click", {
        bubbles: true
      })));
    }

    function delay(ms) {
      return new Promise((r, j) => setTimeout(r, ms))
    }

    // Create Torn Button
    function tornButton(text, handler) {
      let btn = $('<span class="btn-wrap silver"><span class="btn"><input type="submit" value="' + text + '"></input></span></span>');
      if (handler) btn.on('click', handler);
      return btn;
    }

    // Get all items of a certain type on the add items to bazaar page.
    async function bazaarAllItemsOfType(type) {
      let rows = $('li.clearfix.no-mods').filter((i, li) => $(li).data('reactid').indexOf(type) !== -1);
      if (!rows.length) {
        $('.ui-tabs-anchor[href=#' + type + ']').tornClick();
        await delay(100);
        let result = await bazaarAllItemsOfType(type);
        $('.ui-tabs-anchor[href=#All]').tornClick();
        return result;
      }
      return rows.map((i, li) => {
        let $li = $(li);
        let split = $li.find('.name-wrap').text().split('  ');
        let name = split[1];
        let amount = +split[2].substr(1);
        let value = +$li.find('.info-main-wrap').text().substr(1).replace(/,/g, '');
        let price = (value - 1); // - (value % 100);
        let $amount = $li.find('.amount input');
        let $price = $li.find('.price input');
        return {
          $li: $li,
          name: name,
          amount: amount,
          value: value,
          price: price,
          $amount: $amount,
          $price: $price
        }
      });
    }

    // Sells all goods of a given category for 500$ below market value, rounded to 100s
    async function bazaarSellAllItemsOfType(type) {
      return (await bazaarAllItemsOfType(type)).each((i, e) => {
        e.$amount.tornInputVal(e.amount);
        e.$price.tornInputVal(e.price);
      });
    }

    // Sells all Plushies for 500$ below market value, rounded to 100s
    async function bazaarSellAllPlushies() {
      return await bazaarSellAllItemsOfType("Plushie");
    }

    // Sells all Flowers for 500$ below market value, rounded to 100s
    async function bazaarSellAllFlowers() {
      return await bazaarSellAllItemsOfType("Flower");
    }

    // Extend UI to create buttons for selling flowers or plushies
    function bazaarAddButtons() {
      let $top = $('.items-wrap.view-2 .items-footer.clearfix').first();
      if ($top.data('extra-buttons')) return;
      let $btnFlowers = tornButton("All Flowers", bazaarSellAllFlowers);
      let $btnPlushies = tornButton("All Plushies", bazaarSellAllPlushies);
      $top.prepend($btnFlowers, $btnPlushies);
      $top.data('extra-buttons', 'true');
    }

    setInterval(bazaarAddButtons, 1000);
  });
})();