One Bazaar

try to take over the world!

目前为 2024-10-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         One Bazaar
// @namespace    onebazaar.zero.nao
// @version      1.1
// @description  try to take over the world!
// @author       nao
// @match        https://www.torn.com/bazaar.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant        none

// ==/UserScript==

let percentage = 5;

let toBuy = {};

let done = [];

let itemsdata = [];
let bought=[];

function update() {
    let content = ``;
    for (let val of itemsdata){
        if (!bought.includes(val[2])){
            content += (val[1]);
        }


    }
    $("#itemList").html(content);
    $(".itembuynao").off("click");
    $(".itembuynao").on("click", async function () {
        let iid = $(this).attr("id");
        let iitemid = $(this).attr("itemid");
        let iamount = $(this).attr("amount");
        let iuid = $(this).attr("userid");
        let ip = $(this).attr("price");

        await buy(iuid, iid, iitemid, iamount, ip);

    });


}

function insert() {
    let cont = `<div id="displayContainer" style="
    display: grid;
        grid-template-columns: 40% auto;
        background: rgb(51 177 227);
        width: 40vw;
        height: 150px;
        color: yellow;
        border-radius: 10px;
        padding: 10px;

z-index:10;



        ::-webkit-scrollbar {
                width: 5px;
            }

            ::-webkit-scrollbar-track {
                border-radius: 8px;
                background-color: #e7e7e7;
                border: 1px solid #cacaca;
            }

            ::-webkit-scrollbar-thumb {
                border-radius: 8px;
                background-color: #3b9ab7;
            }


    ">
        <div id="itemList" style=" overflow-y: scroll;">
        </div>
        <div id = "actionsHistory" style=" overflow-y: scroll;">
            <p class="actionsElement">{result}</p>



        </div>`;

    if ($("div[class^='topSection']").length == 0) {
        setTimeout(insert, 300);
        return;
    }

    if ($("#displayContainer").length == 0) {
        $(".content-wrapper").prepend(cont);

    }



}


async function buy(userid, id, itemid, amount, price) {

    await $.post(`https://www.torn.com/bazaar.php?sid=bazaarData&step=buyItem&rfcv=${getRFC()}`, {
        userID: userid,
        id: id,
        itemid: itemid,
        amount: amount,
        price: price,
        beforeval: price * amount

    }, function (response) {
        console.log(response);
        response = JSON.parse(response);
        console.log(response);
        addResult(response.text, response.success ? "green" : "red");
        if (response.success && response.text.includes("bought")){
            $(`#${id}`).remove();
            bought.push(id);
        }
    })

}

function addResult(resultMsg, rescol) {
    let curtime = $(".server-date-time").html().split("-")[0];
    let resmsg = `<p style="color: ${rescol}">${curtime} ${resultMsg}</p>`;
    $("#actionsHistory").prepend(resmsg);


}

function getRFC() {
    var rfc = $.cookie('rfc_v');
    if (!rfc) {
        var cookies = document.cookie.split('; ');
        for (var i in cookies) {
            var cookie = cookies[i].split('=');
            if (cookie[0] == 'rfc_v') {
                return cookie[1];
            }
        }
    }
    return rfc;
}





if (window.location.href.includes("userId")){


    insert();
    const { fetch: origFetch } = window;
    window.fetch = async (...args) => {
        console.log("onebazaar called with args:", args);

        const response = await origFetch(...args);

        /* work with the cloned response in a separate promise
     chain -- could use the same chain with `await`. */

        if (response.url && response.url.includes('/bazaar.php?sid=bazaarData&step=getBazaarItems')) {
            let clonedResponse = response.clone();
            let clonedJ = await clonedResponse.json();
            let uid = clonedJ.ID;

            // console.log("userid" + uid);


            for (let item of clonedJ.list) {
                let id = item.bazaarID;

                let itemid = item.ID;
                let amount = item.amount;
                let price = item.price;
                let mv = item.averageprice;
                let isBlockedForBuying = item.isBlockedForBuying;
                let name = item.name;
                // console.log(item);

                toBuy[name] = [uid, id, itemid, amount, price];
                if (!isBlockedForBuying && parseInt(price) <= (100 - percentage) / 100 * mv && !done.includes(id)) {
                    let con = `<button style="color:white; margin:5px; height:40px" class="itembuynao torn-btn" userid=${uid} id=${id} itemid=${itemid} amount=${amount} price=${price}>${name} $${(price * amount).toLocaleString()}</button>`;

                    while ($("#itemList").length == 0) {

                        console.log("itemlist");

                    }
                    done.push(id);
                    itemsdata.push([parseInt(mv - price), con, id]);

                }


            }


        }
        itemsdata = itemsdata.sort(function (a, b) {
            return b[0] - a[0];


        });
        update();

        insertBuyLink();

        console.log(toBuy);
        return response;
    };

}

function insertBuyLink(){
    console.log("insertBuyLink");
    if ($("p[class^='name_']").length == 0){
        setTimeout(insertBuyLink, 300);
        return;
    }
    $("p[class^='name_']:not('processed')").each(function(){
        if ($(this).attr("processed")){
            return;
        }
        let name = $(this).html();
        let tname = name.replace(/ /g, "_");
        console.log(name);
        $(this).html(`<input type="submit" style="width:100%;" id="${tname}-buy" value="${tname}">`);
        $(`#${tname}-buy`).on("click", async function(){
            let userId = toBuy[name][0];
            let id = toBuy[name][1];
            let itemid = toBuy[name][2];
            let amount = toBuy[name][3];
            let price = toBuy[name][4];
            console.log("buying");
            await buy(userId, id, itemid, amount, price);
        });
        $(this).attr("processed", true);
    });
}