WFMario / WF Resource copier

// ==UserScript==
// @name         WF Resource copier
// @version      2024-06-24
// @author       Mario
// @license MIT
// @description  Copies resources to be used by cargo scripts.
// @match        https://*.atmoburn.com/blueprints.php*
// @match        https://*.atmoburn.com/fleet.php*
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());
unsafeWindow.eval(copyResources.toString());
unsafeWindow.eval(loadBPcost.toString());
unsafeWindow.eval(pasteBPCost.toString());

if (document.URL.includes("atmoburn.com/fleet.php")) {
    let pasteCargoButton = document.createElement('button');
    pasteCargoButton.className = "darkbutton";
    pasteCargoButton.type = "button";
    pasteCargoButton.innerHTML = "Paste Resources";
    pasteCargoButton.title = "Pastes stored resources to load fields";
    pasteCargoButton.addEventListener("click", pasteBPCost);
    byId("fleetCargo").children[0].children[0].append(pasteCargoButton);
} else if (document.URL.includes("atmoburn.com/blueprints.php")) {
    let clearResourcesContainer = document.createElement('div');
    clearResourcesContainer.className = "aligncenter";
    let clearResourcesButton = document.createElement('button');
    clearResourcesButton.className = "darkbutton";
    clearResourcesButton.type = "button";
    clearResourcesButton.innerHTML = "Clear Resources";
    clearResourcesButton.title = "Clears copied resources from local storage";
    if (document.URL.includes("beta3")) {
        clearResourcesButton.addEventListener("click", function (e) {
        localStorage.removeItem("Blueprint Costs B3");
        console.log("cleared resources from local storage");
    });
    } else {
        clearResourcesButton.addEventListener("click", function (e) {
            localStorage.removeItem("Blueprint Costs");
            console.log("cleared resources from local storage");
        });
    }
    clearResourcesContainer.append(clearResourcesButton);
    byId("midcolumn").children[1].append(clearResourcesContainer)

    let bplist;
    if (document.URL.includes("beta3")) {
        bplist = document.getElementsByClassName("tbborder flexwrapreverse");
    } else {
        bplist = document.getElementsByClassName("margintb5 flexwrapreverse");
    }
    Array.from(bplist).forEach(blueprint => {
        let copyResourcesButton = document.createElement('button');
        copyResourcesButton.className = "darkbutton";
        copyResourcesButton.type = "button";
        copyResourcesButton.innerHTML = "Copy Resources";
        copyResourcesButton.title = "Copies resources to localstorage to be used by the cargo side of the script";
        copyResourcesButton.addEventListener("click", function (e) {
            copyResources(e.currentTarget.parentElement);
        });
        blueprint.append(copyResourcesButton)
        let copy1000x = document.createElement('button');
        copy1000x.className = "darkbutton";
        copy1000x.type = "button";
        copy1000x.innerHTML = "Copy 1000x";
        copy1000x.title = "Copies resources to localstorage to be used by the cargo side of the script";
        copy1000x.addEventListener("click", function (e) {
            copyResources(e.currentTarget.parentElement, 1000);
        });
        blueprint.append(copy1000x)
    })
}

function copyResources(blueprint, modifier=1) {
    let blueprintCosts = loadBPcost()
    Array.from(blueprint.children[1].getElementsByClassName("width100")[1].firstElementChild.children).forEach(child => {
        if (child.children[0].textContent !== "Price:") {
            blueprintCosts[child.children[0].textContent.replace(":","").toLowerCase()] += modifier * parseInt(child.children[1].textContent)
        }
    })
    if (document.URL.includes("beta3")) {
        localStorage.setItem("Blueprint Costs B3", JSON.stringify(blueprintCosts));
    } else {
        localStorage.setItem("Blueprint Costs", JSON.stringify(blueprintCosts));
    }
}

function loadBPcost() {
    if (document.URL.includes("beta3")) { // redefine if we're on old resource system
         if (localStorage.getItem("Blueprint Costs B3")) {
            return JSON.parse(localStorage.getItem("Blueprint Costs B3"))
        } else {
            return {
                "iron": 0,
                "copper": 0,
                "silver": 0,
                "titanium": 0,
                "gold": 0,
                "uranium": 0,
                "platinum": 0,
                "diamonds": 0,
                "oil": 0,
                "water": 0,
                "food":0,
            }
        }
    } else {
        if (localStorage.getItem("Blueprint Costs")) {
            return JSON.parse(localStorage.getItem("Blueprint Costs"))
        } else {
            return {
                "steel": 0,
                "aluminum": 0,
                "titanium": 0,
                "tungsten alloy": 0,
                "uranium": 0,
                "copper": 0,
                "gold": 0,
                "lithium": 0,
                "silicium": 0,
                "diamonds": 0,
                "cement": 0,
                "wood": 0,
                "plastic": 0,
                "food":0,
            }
        }
    }
}

function pasteBPCost() {
    let blueprintCost = loadBPcost();
    if (document.URL.includes("beta3")) {
        Array.from(byId("fleetCargo").getElementsByClassName("darkinput smalltext alignright")).forEach(field => {
            field.value = blueprintCost[field.parentElement.previousElementSibling.previousElementSibling.textContent.toLowerCase()]
        })
    } else {
        Array.from(byId("fleetCargo").getElementsByClassName("darkinput smalltext alignright margin1")).forEach(field => {
            field.value = blueprintCost[field.parentElement.previousElementSibling.previousElementSibling.textContent.toLowerCase()]
        })
    }
}