WFMario / Fleet Tweaks

// ==UserScript==
// @name         Fleet Tweaks
// @version      2024-09-14
// @author       Mario
// @license MIT
// @include      https://*.atmoburn.com/fleet.php*
// @include      https://*.atmoburn.com/fleet/*
// @include      https://*.atmoburn.com/fleet_cargo_orders.php*
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());
unsafeWindow.eval(copyResources.toString());
unsafeWindow.eval(pasteResources.toString());
unsafeWindow.eval(getCurrentPlanet.toString());
unsafeWindow.eval(toSavedColony.toString());
unsafeWindow.eval(saveColony.toString());
unsafeWindow.eval(unloadAll.toString());
unsafeWindow.eval(loadAll.toString());
unsafeWindow.eval(carrierLoadAll.toString());


// Next and Previous fleet buttons
if (byId("fleetlist").getElementsByClassName("highlight")[0].previousElementSibling && byId("fleetlist").getElementsByClassName("highlight")[0].previousElementSibling.className !== "sidemenu_header") {
    let previousFleet = document.createElement('a');
    previousFleet.innerHTML = "Previous fleet";
    previousFleet.id = "previousFleet";
    previousFleet.className = "darkbutton";
    previousFleet.style.fontSize = "10px";
    previousFleet.href = byId("fleetlist").getElementsByClassName("highlight")[0].previousElementSibling.previousElementSibling.children[0].href;
    let elementIndex = 0;
    byId("midcolumn").getElementsByClassName("subtitle")[0].prepend(previousFleet)
}

if (byId("fleetlist").getElementsByClassName("highlight")[0].nextElementSibling.nextElementSibling) { // first nextElementSibling is always the script for the color
    let nextFleet = document.createElement('a');
    nextFleet.innerHTML = "Next fleet";
    nextFleet.id = "nextFleet";
    nextFleet.className = "darkbutton";
    nextFleet.style.fontSize = "10px";
    nextFleet.href = byId("fleetlist").getElementsByClassName("highlight")[0].nextElementSibling.nextElementSibling.children[0].href;
    let elementIndex = 0;
    byId("midcolumn").getElementsByClassName("subtitle")[0].append(nextFleet)
}

function getCurrentPlanet() {
    let worldRef = byId("navData").children[1].children[0].children[0].children[1].href;
    return worldRef.substr(worldRef.indexOf("Planet(")+7, worldRef.indexOf(",")-(worldRef.indexOf("Planet(")+7));
}

function toSavedColony() {
    verifyMission("tcolony,"+localStorage.getItem("tColony"));
    launchMission();
}

function saveColony() {
    let colonyRef = byId("missionPosition").children[0].children[1].href;
    if (colonyRef.indexOf("colony=") > 0) {
        localStorage.setItem("tColony", colonyRef.substr(colonyRef.indexOf("colony=")+7, colonyRef.indexOf("&fleet")-(colonyRef.indexOf("colony=")+7)));
    } else { alert("target is not a colony") }
}

function massLoadUnload(resources, loadUnload, allOrNot) {
    resources.forEach(resource => {
        var myInterval = setInterval(function() {
            if (!amLoadingCargo) {
                cargoLoadUnload(resource, loadUnload, allOrNot);
                clearInterval(myInterval)
            }
        }, 10);
    });
}

function carrierLoadAll() {
    try {
        Array.from(byId("fleetCarrier").getElementsByClassName("fa fa-angle-double-left")).forEach(carrierScript => {
            let carrierID = carrierScript.parentElement.parentElement.previousElementSibling.id.split("-");
            console.log(carrierID);
            var myInterval = setInterval(function() {
                if (carrierScript.parentElement.parentElement.parentElement.lastElementChild.textContent === '0') {return}
                if (!amModifyingCarrier) {
                    carrierLoadUnload(carrierID[0].substr(carrierID[0].indexOf("Load")+4), carrierID[1], 'load', 0);
                    clearInterval(myInterval)
                }
            }, 100);
        });
    } catch (e) {
        console.log(carrierID)
        console.log(e)
    }
}

function loadAll() {
    let resources = ["steel","aluminum","titanium","tungsten alloy","uranium","copper","gold","lithium","silicium","diamonds","cement","wood","plastic","food"];
    if (document.domain.includes("beta3")) {
        resources = ["iron","copper","silver","titanium","gold","uranium","platinum","diamonds","oil","water","food"];
    }
    massLoadUnload(resources,'load', false);
}

function unloadAll() {
    let resources = ["steel","aluminum","titanium","tungsten alloy","uranium","copper","gold","lithium","silicium","diamonds","cement","wood","plastic","food"];
    if (document.domain.includes("beta3")) {
        resources = ["iron","copper","silver","titanium","gold","uranium","platinum","diamonds","oil","water","food"];
    }
    massLoadUnload(resources,'unload', true);
}


if ( !document.URL.includes("fleet_cargo_orders") && !byId("pageSubContainer").textContent.includes("Explorer")) {
    let tCurrentPlanet = document.createElement('button');
    tCurrentPlanet.className = "darkbutton";
    tCurrentPlanet.type = "button";
    tCurrentPlanet.innerHTML = "Select Current Planet";
    tCurrentPlanet.title = "Selects Current Planet";
    tCurrentPlanet.addEventListener("click", function(){verifyMission("tworld,"+getCurrentPlanet())});
    byId("missionSelectionLocal").append(tCurrentPlanet);

    let tSavedColony = document.createElement('button');
    tSavedColony.className = "darkbutton";
    tSavedColony.type = "button";
    tSavedColony.innerHTML = "To saved colony";
    tSavedColony.title = "Send to saved colony";
    tSavedColony.addEventListener("click", toSavedColony);
    byId("missionSelectionGlobal").append(tSavedColony);

    let saveColonyButton = document.createElement('button');
    saveColonyButton.className = "darkbutton";
    saveColonyButton.type = "button";
    saveColonyButton.innerHTML = "Save target colony";
    saveColonyButton.title = "Send to saved colony";
    saveColonyButton.addEventListener("click", saveColony);
    byId("mButton").append(saveColonyButton);

    let loadAllButton = document.createElement('button');
    loadAllButton.className = "darkbutton";
    loadAllButton.type = "button";
    loadAllButton.innerHTML = "<<< Load all";
    loadAllButton.title = "Loads everything that is currently filled in";
    loadAllButton.addEventListener("click", loadAll);
    byId("fleetCargo").children[0].children[0].append(loadAllButton);

    let unloadAllButton = document.createElement('button');
    unloadAllButton.className = "darkbutton";
    unloadAllButton.type = "button";
    unloadAllButton.innerHTML = "Unload all >>>";
    unloadAllButton.title = "Unloads all";
    unloadAllButton.addEventListener("click", unloadAll);
    byId("fleetCargo").children[0].children[0].append(unloadAllButton);

    let fillCarriersButton = document.createElement('button');
    fillCarriersButton.className = "darkbutton";
    fillCarriersButton.type = "button";
    fillCarriersButton.innerHTML = "Fill Carriers";
    fillCarriersButton.title = "Fills Carriers";
    fillCarriersButton.addEventListener("click", carrierLoadAll);
    byId("fleetCarrier").children[0].children[0].after(fillCarriersButton);
}

function copyResources(element, resources) {
    Array.from(element.parentElement.parentElement.parentElement.children[2].children).forEach(resource => {
        resources[resource.children[0].textContent.toLowerCase()] = resource.children[1].children[0].value
    })
}

function pasteResources(element, resources) {
    Array.from(element.parentElement.parentElement.parentElement.children[2].children).forEach(resource => {
        resource.children[1].children[0].value = resources[resource.children[0].textContent.toLowerCase()]
    })
}

let resources = {
    "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,
}

if (document.URL.includes("beta3")) { // redefine if we're on old resource system
    resources = {
        "iron": 0,
        "copper": 0,
        "silver": 0,
        "titanium": 0,
        "gold": 0,
        "uranium": 0,
        "platinum": 0,
        "diamonds": 0,
        "oil": 0,
        "water": 0,
        "food":0,
    }
}

if (document.URL.includes("atmoburn.com/fleet_cargo_orders.php")) {
    Array.from(byId("routeList").children).forEach(tradeRoute => {
        let copyTradeRouteButton = document.createElement('button');
        copyTradeRouteButton.className = "darkbutton";
        copyTradeRouteButton.type = "button";
        copyTradeRouteButton.innerHTML = "Copy Trade Route";
        copyTradeRouteButton.title = "Copies stored trade route cargo";
        copyTradeRouteButton.addEventListener("click", function(event){copyResources(event.currentTarget, resources)});
        tradeRoute.children[0].children[1].children[3].children[1].append(copyTradeRouteButton);

        let pasteTradeRouteButton = document.createElement('button');
        pasteTradeRouteButton.className = "darkbutton";
        pasteTradeRouteButton.type = "button";
        pasteTradeRouteButton.innerHTML = "Paste Trade Route";
        pasteTradeRouteButton.title = "Pastes stored trade route cargo";
        pasteTradeRouteButton.addEventListener("click", function(event){pasteResources(event.currentTarget, resources)});
        tradeRoute.children[0].children[1].children[3].children[1].append(pasteTradeRouteButton);
    })
}

// Maintenance button to other tab
let maintenanceButton = byId("rosterTable").getElementsByClassName("darkbutton")[0];
let maintenanceAlternativeButton = document.createElement('a');
maintenanceAlternativeButton.className = "darkbutton";
maintenanceAlternativeButton.innerHTML = "Open Maintenance in other tab";
maintenanceAlternativeButton.target = "_blank";
maintenanceAlternativeButton.href = maintenanceButton.onclick.toString().split("/")[1].split("\"")[0];
maintenanceButton.after(maintenanceAlternativeButton);


Array.from(byId("fleetlist").getElementsByClassName("highlight")).forEach(highlight => {highlight.style.backgroundColor = "darkgreen"})


 //Keyboard shortcuts section
document.onkeydown = function(e) {
    e = e || window.event;
    var keycode = e.which || e.keyCode;
    var ctrlPressed = e.ctrlKey || e.metaKey; //record if Ctrl key is pressed

    if( !ctrlPressed && document.activeElement.tagName == "BODY") { //skip if Ctrl key is pressed, activeElement used to skip if user has a text input active, don't do anything before page is loaded
        try {
            if( keycode == 37 && byId("previousFleet") && byId("previousFleet").style.visibility != "hidden") {// '37' is the keycode for ArrowLeft, skip if previous colony button doesn't exist or is hidden
                e.preventDefault();
                byId("previousFleet").click();
            } else if( keycode == 39 && byId("nextFleet") && byId("nextFleet").style.visibility != "hidden") {//'39' is the keycode for ArrowRight, skip if next colony button doesn't exist or is hidden
                e.preventDefault();
                byId("nextFleet").click();
            }
        } catch (error) {console.log("slow down faggot")}
    }
};