Ahab / market lols

// ==UserScript==
// @name market lols
// @namespace torn.com
// @include *torn.com/*
// @author Ahab [1735214]
// @version 0.6
// @updateURL https://openuserjs.org/meta/Ahab/market_lols.meta.js
// @license MIT
// ==/UserScript==

var itemID = {206:800000,160:99,190:99,209:600}
var audioPlayer = document.createElement('audio');
audioPlayer.src = 'https://www.torn.com/casino/keno/audio/roundwon.ogg';
audioPlayer.preload = 'auto';

$('#sidebar').before('<div><input id="shoploop" type="button" value="Start market check"/></div>');

$("#shoploop").click(function start() {
    localStorage.loopiterations = parseInt(prompt("How many loops through items?")*Object.keys(itemID).length)
    localStorage.loopnum = 0
    localStorage.loopstatus = 1
    localStorage.loopiterations -= 1
    window.location.href = "https://www.torn.com/imarket.php#/p=shop&type="+Object.keys(itemID)[localStorage.loopnum];
});

function loop(){
    var newit = localStorage.loopiterations
    var newnum = JSON.parse(localStorage.loopnum)
    if(parseInt(newnum) < Object.keys(itemID).length - 1){
        if (confirm('continue checking items?')) {
            localStorage.loopiterations = newit -= 1
            localStorage.loopnum = newnum += 1
            window.location.href = "https://www.torn.com/imarket.php#/p=shop&type="+Object.keys(itemID)[localStorage.loopnum];
        }
    }else{
        localStorage.loopnum = 0
        window.location.href = "https://www.torn.com/imarket.php#/p=shop&type="+Object.keys(itemID)[localStorage.loopnum]
    }
}

function check(){
    var cost = 0
    $('span[class^="price"]').each(function(index){
        if(cost === 0){
            cost = parseInt($(this).text().split(" ")[0].split("$")[1].replace(/,/g,""))
        }
        else if($(this).text().split(" ")[0].split("$")[1] < cost){
            cost = parseInt($(this).text().split(" ")[0].split("$")[1].replace(/,/g,""))
        }
    });
    $('li[class^="cost"]').each(function(index){
        if($(this).text().split("$")[1] < cost){
            cost = parseInt($(this).text().split("$")[1].replace(/,/g,""))
        }
    })
    if(localStorage.loopiterations != 0 & cost > itemID[parseInt(window.location.href.split('type=')[1])]){
        loop()
    }
    else{
        audioPlayer.play()
    }
}

if(parseInt(localStorage.loopiterations) != 0){
    if($("#contloop").length === 0){
        $('#shoploop').after('<div><input id="contloop" type="button" value="continue market check"/></div>');
        $("#contloop").click(function start() {
            loop()
        });
    }
}

var observerTarget = $('#item-market-main-wrap')[0];
var observerConfig = { attributes: false, childList: true, characterData: false, subtree: false };
var observer = new MutationObserver(function(mutations) {
    if($("#item-market-main-wrap > div.shop-market-page").length > 0){
        if(parseInt(localStorage.loopiterations) != 0){
            check()
            if($("#contloop").length === 0){
                $('#shoploop').after('<div><input id="contloop" type="button" value="continue market check"/></div>');
                $("#contloop").click(function start() {
                    loop()
                });
            }
        }else if(parseInt(localStorage.loopstatus) === 1){
            check()
            alert('Checking over')
            $("#contloop").remove()
            localStorage.loopstatus = 0
        }
    }
})
observer.observe(observerTarget, observerConfig);