Merlin-R / Item Market Buy All

// ==UserScript==
// @name         Item Market Buy All
// @namespace    https://www.torn.com/imarket.php
// @version      1.0
// @description  Buys all items on the current market page
// @author       Merlin Reichwald
// @license MIT
// @copyright 2018, Merlin-R (https://openuserjs.org/users/Merlin-R)
// @match        https://www.torn.com/imarket.php
// @grant        none
// ==/UserScript==

setTimeout(function() {
    'use strict';
    function tornButton(text, id, handler) {
      let btn = $('<span class="btn-wrap silver" id="'+id+'"><span class="btn"><input type="submit" value="' + text + '"></input></span></span>');
      if (handler) btn.on('click', handler);
      return btn;
    };
    var buyAll = tornButton("Buy All", 'buyAll', function(e){
        e.preventDefault();
        e.stopPropagation();
        $('.items>li').find('.buy-icon').click();
        setTimeout(function(){
            $('.yes-buy').click();
            setTimeout(function(){
                location.reload(true);
            }, 5000);
        }, 2000);
    });
    setInterval(function(){
        if ( $('#buyAll').length ) return;
        if ( location.hash.indexOf('#/p=shop&type=') !== 0 ) return;
        $('form[action="imarket.php"]').append(buyAll);
    },2000);
},2000);