Ahab / 1d buy

// ==UserScript==
// @name         1d buy
// @namespace    torn
// @version      0.4
// @description  try to take over the world!
// @author       You
// @grant        GM_addStyle
// @match        *www.torn.com/bazaar.php?userId=*
// @updateURL    https://openuserjs.org/meta/Ahab/1d_buy.meta.js
// @license      MIT
// @require      https://jpillora.com/xhook/dist/xhook.js
// ==/UserScript==

GM_addStyle(`
.item___2GvHm {
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    height: 73px;
    justify-content: center;
    position: relative;
    width: 261px;
    background-color:#f2f2f2
}

.item___-mxOy {
    border-left: 1px solid transparent;
    border-right: 1px solid transparent;
    -webkit-box-sizing: border-box;
    box-sizing: border-box
}

.itemDescription___3bOmj {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    height: 100%;
    width: 100%
}

.imgBar___1Lq7R {
    -webkit-box-align: center;
    -ms-flex-align: center;
    -ms-flex-item-align: center;
    -ms-grid-row-align: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    align-items: center;
    align-self: center;
    border-radius: 5px;
    height: 50px;
    justify-content: center;
    margin-left: 10px;
    position: relative;
    width: 100px;
    display: flex
}

.imgContainer___1IzaV {
    -webkit-box-flex: 1;
    -ms-flex-positive: 1;
    flex-grow: 1;
    z-index: 7;
    position: inherit
}

.img___dBFof {
    -webkit-box-flex: 1;
    -ms-flex-positive: 1;
    flex-grow: 1
}

.description___2Nx64 {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 100%;
    padding: 11px 10px 10px;
    white-space: nowrap;
    width: calc(100% - 110px);
    text-align: center
}

.name___IJ_Q- ,.price___8AdTw {
    display: block;
    height: 15px;
    overflow-x: hidden;
    -o-text-overflow: ellipsis;
    text-overflow: ellipsis
}

.price___8AdTw {
    margin-top: 3px
}

.amount___3adQ- {
    display: -webkit-box;
    display: -ms-flexbox;
    display: block;
    margin-top: 2px;
}

.amountValue___2_SKj {
    height: 15px;
    overflow-x: hidden;
    -o-text-overflow: ellipsis;
    text-overflow: ellipsis
}

.item___2GvHm:before {
    border-bottom: 1px solid #fff;
    border-bottom-color: var(--default-panel-divider-inner-side-color);
    border-top: 1px solid #ccc;
    border-top-color: var(--default-panel-divider-outer-side-color);
    content: "";
    left: 0;
    position: absolute;
    top: -2px;
    width: 100%
}

.item___-mxOy:after {
    background: -webkit-gradient(linear,left bottom,left top,from(hsla(0,0%,80%,0)),color-stop(50%,#ccc),to(hsla(0,0%,80%,0)));
    background: -o-linear-gradient(bottom,hsla(0,0%,80%,0) 0,#ccc 50%,hsla(0,0%,80%,0) 100%);
    background: linear-gradient(0deg,hsla(0,0%,80%,0),#ccc 50%,hsla(0,0%,80%,0));
    background: var(--default-divider-left-linear-gradient);
    content: "";
    height: 100%;
    position: absolute;
    right: -1px;
    top: 0;
    width: 1px
}

.torn-item.item-plate,.thumbnail .torn-item.item-plate {
    height: 50px;
    width: 100px;
    padding: 3px 0 0;
    box-sizing: border-box;
    border-right: none;
    border-left: none;
    border-top: none;
    border-bottom: 1px solid #fff;
    border-radius: 6px;
    box-shadow: inset 0 3px 4px #1111113b;
    background: #e8e8e8 linear-gradient(0deg,#EBEBEB 0%,#DDDDDD 100%) 0 0 no-repeat;
    background: var(--items-plate-background-color) var(--items-plate-background);
    border: var(--items-plate-border);
    border-color: var(--items-plate-border-color);
    box-shadow: var(--items-plate-box-shadow);
    z-index: 1;
    position: absolute
}
`)

var items = []

xhook.after(function(request, response) {
  if(request.url.match(/getBazaarItems&start=/)){
      response.clone().text().then((text) => {
          items = JSON.parse(text)
          console.log(items)
          $('div[class*="ReactVirtualized__Grid"]').remove()
          listItems()
      })
  }
});

$('button[class*="item___"]').live("click", function(event) {
     $('div[class*="item___2GvHm"]').remove()
});

function listItems(){
    $.each(items.list, function(index){
        var amount = Math.floor($('span[id*="user-money"]').attr('data-money')/items.list[index].price)
        if(amount > items.list[index].amount){
            amount = items.list[index].amount
        }
        if($('div[data-id^="'+items.list[index].bazaarID+'"]').length == 0  && items.list[index].isBlockedForBuying == false){
            $('div[class*="core-layout___"]').append('<div class="item___2GvHm item___-mxOy" style="float: left; display: block;" data-cat="'+items.list[index].category+'" data-name="'+items.list[index].name+'" data-quality="'+items.list[index].quality+'" data-price="'+items.list[index].price+'" data-median="'+items.list[index].medianprice+'"data-id="added"><div class="itemDescription___3bOmj" id="itemD" data-id='+items.list[index].bazaarID+' style="width: 100% !important;"><div class="imgBar___1Lq7R"><canvas class="torn-item item-plate item-converted" width="100" height="50" style="opacity: 1;"></canvas><div class="imgContainer___1IzaV"  id="imagebuy" data-html="true"><img class="img___dBFof" src="/images/items/'+items.list[index].ID+'/large.png"></div></div><div style="display: block; overflow: auto; width: inherit;"><div class="description___2Nx64" id="itemdescription" style="width: auto; display: block;"><p class="name___IJ_Q-">'+items.list[index].name+'</p><p class="price___8AdTw">'+'$'+parseInt(items.list[index].price).toLocaleString()+'</p><p class="amount___3adQ-">(<span class="amountValue___2_SKj">'+items.list[index].amount+'</span>&nbsp;in stock)</p></div><div id="buyStep" style="margin: auto; display: none"><div style="padding: 5px;"><p id="enterAmount" style="text-align-last: center;">Amount to buy</p><div style="text-align-last: center;"><input name="quantity" type="number" class="numberInput___2w5QL buyAmountInput___Aooaf" min="0" max='+items.list[index].amount+' step="1" value='+amount+' style="width: 50px; height: 5px;"></div><div style="text-align-last: center;"><p>Total Cost</p><p id="total">'+'$'+(amount*items.list[index].price).toLocaleString()+'</p></div></div></div></div><div id="buyresult" class="buyoutcome" style="background-color:#f2f2f2; padding: 1px 5px 1px; display: none; text-align: center; margin-block: auto;"></div></div>');
    }})}

$(document).on('click', '.item___2GvHm', function(event) {
    var amount = Math.floor($('span[id*="user-money"]').attr('data-money')/$(this)[0].dataset.price)
    if(amount > $(this).find('#itemD').find('.amountValue___2_SKj')[0].innerText){
        amount = $(this).find('#itemD').find('.amountValue___2_SKj')[0].innerText
    }
    if(amount > 0){
        var o = $(this)
        var price = $(this).find('.itemDescription___3bOmj').find('.price___8AdTw')[0].textContent.replace(/\$/g, '').replace(/,/g, '')
        var beforeval = amount*price
        var stock = $(this).find('#itemD').find('.amountValue___2_SKj')[0].innerText
        const XHR = new XMLHttpRequest(),
              data = new FormData();
        data.append('userID', $('div[class*="messageContent___"]')[0].children[0].href.split("=")[1]);
        data.append('id', $(this).find('.itemDescription___3bOmj')[0].attributes[2].value);
        data.append('itemID', $(this).find('.img___dBFof')[0].currentSrc.split("/")[5]);
        data.append('amount', amount);
        data.append('price', price);
        data.append('beforeval', beforeval)
        XHR.addEventListener('load', function(event) {
            o.find('.itemDescription___3bOmj')[0].children[1].style.display = "none";
            o.find('.itemDescription___3bOmj')[0].children[2].style.display = "block";
            o.find('.itemDescription___3bOmj')[0].children[2].innerHTML = JSON.parse(event.target.response)['text']
            if(JSON.parse(event.target.response)['success'] == false){
                $('div[class*="buyoutcome"]').css('color', 'rgb(216,53,0)');
            }
            else if(JSON.parse(event.target.response)['success'] == true){
                $('div[class*="buyoutcome"]').css('color', 'rgb(103,140,0)');
                stock = stock-amount
                o.find('#itemD').find('.amountValue___2_SKj')[0].innerText = stock
            }
        });
        XHR.addEventListener('error', function(event) {
            console.log(event);
        });
        XHR.open('POST', 'https://www.torn.com/bazaar.php?sid=bazaarData&step=buyItem');
        XHR.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        XHR.send(data);
    }
})

$(document).on('click', '#buyresult', function(event) {
    if($(this).closest('#itemD').find('p[class^="amount___"]')[0].innerText.split("in")[0].split("(")[1] == 0){
        $(this).closest('div[class^="item___"]').remove()
    }
    else{
        $(this)[0].style.display = "none";
        $(this)[0].parentElement.children[1].style.display = "block";
    }
})