Greasy Fork is available in English.

Brick Hill Outfit Randomizer And Purge

Adds 2 buttons to randomize and purge your avatar

נכון ליום 06-03-2021. ראה הגרסה האחרונה.

// ==UserScript==
// @name         Brick Hill Outfit Randomizer And Purge
// @version      0.1
// @description  Adds 2 buttons to randomize and purge your avatar
// @author       Noah Cool Boy
// @match        https://www.brick-hill.com/customize/
// @namespace https://greasyfork.org/users/725966
// ==/UserScript==

let token = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let outfitCard = document.querySelector(".outfit-card .content")
let buttons = document.createElement("div")
buttons.style.display = "flex"
buttons.style.marginTop = "5px"
buttons.innerHTML = `<button class="blue" style="width: 100%; margin-right: 5px" onclick="rand()">Randomize</button><button class="red" style="width: 100%; margin-left: 5px" onclick="purge()">Purge</button>`
// No judging please ^
outfitCard.appendChild(buttons)

document.purge = function() {
    let http = new XMLHttpRequest()
    http.open("GET", "/api/avatar/wearing", false) // Only gangstas don't use async :sunglasses:
    http.withCredentials = true
    http.send()
    let wearing = JSON.parse(http.responseText)
    wearing.forEach((clothing, i) => {
        setTimeout(()=>{
            avatarUpdate("remove", clothing.id)
        }, i*1000)
    })
}

let types = {}
document.querySelectorAll(".item-types a").forEach(v=>{
    if(v.innerText == "Outfits") return
    types[v.getAttribute("data-url")] = v.innerText
})

document.rand = function() {
    let modal = document.createElement("div")
    modal.className = "modal"
    modal.innerHTML = `<div class="modal-content"><span class="close">x</span>Random Outfit Generator<hr>What would you like to randomize?<br>${Object.values(types).map(v=>`<div class="option"><input type="checkbox" checked>${v}<br></div>`).join("")}<div class="modal-buttons"><button class="green">Randomize!</button></div></div>`
    modal.querySelector(".close").addEventListener("click",()=>{
        modal.remove()
    })
    modal.querySelector("button").addEventListener("click", ()=>{
        randomize(modal)
    })
    outfitCard.appendChild(modal)
}

function wait(ms) {
    return new Promise(a => {
        setTimeout(a, ms)
    })
}

function randomize(modal) {
    modal.querySelector(".close").remove()
    modal.querySelector("button").remove()
    let status = document.createElement("span")
    modal.querySelector(".modal-content").appendChild(status)
    let opts = [...modal.querySelectorAll(".option")].map(v => v.innerText.trim())
    setTimeout(async ()=>{
        let inventory = {}
        if(window.localStorage.randomizer_cache && window.localStorage.randomizer_cache_date && Date.now() - window.localStorage.randomizer_cache_date < 1000 * 60 * 60 * 24) {
            inventory = JSON.parse(window.localStorage.randomizer_cache)
        } else {
            for(let x = 0; x < opts.length; x++) {
                let type = opts[x]
                inventory[type] = []
                let apiType = Object.keys(types).find(key => types[key] == type)
                let page = 1
                while(true) {
                    let req = new XMLHttpRequest()
                    req.open("GET","/api/avatar/crate/"+apiType+"/"+page,false)
                    req.withCredentials = true
                    req.send()
                    let data = JSON.parse(req.responseText)
                    inventory[type] = inventory[type].concat(data.data.map(v=>v.item_id))
                    if(data.pages.current >= data.pages.pageCount) {
                        break
                    }
                    page++
                    //console.log(`Getting ${type} ${data.pages.current}/${data.pages.pageCount}`)
                    await wait(5)
                    status.innerText = `Getting ${type} ${data.pages.current}/${data.pages.pageCount}`
                }
            }
            //console.log(inventory)
            //console.log(JSON.stringify(inventory))

            window.localStorage.randomizer_cache = JSON.stringify(inventory)
            window.localStorage.randomizer_cache_date = Date.now()
        }
        opts = [...modal.querySelectorAll(".option")].map(v => [v.innerText.trim(), v.querySelector("input").checked])
        opts = opts.filter(v=>v[1]).map(v=>v[0])
        let messages = ["Shuffling the stuff", "Doing things", "Randomizing the avatar", "Making your new outfit", "Tiny computer is thinking", "Generating a combo", "The machine is hard at work"]
        status.innerText = messages[Math.floor(Math.random()*messages.length)]
        let outfit = Object.values(inventory).filter(v=>v.length).map(v=>v[Math.floor(Math.random()*v.length)])
        for(let x = 0; x < outfit.length; x++) {
            avatarUpdate("wear", outfit[x])
            await wait(1000)
        }
        modal.remove()
    }, 1)


    console.log("Done!")
}