WFMario / Create Same +1

// ==UserScript==
// @name         Create Same +1
// @version      2024-06-06
// @author       Mario
// @license MIT
// @match        https://*.atmoburn.com/fleet_configuration.php?*
// @grant        unsafeWindow
// ==/UserScript==

eval(unsafeWindow.byId.toString());
unsafeWindow.eval(createAndIncrement.toString());


function createAndIncrement() {
    var fleetName = byId("fleetName").value
    var matches = fleetName.match(/(\d+)/g)
    var fleetNumber = Array.from(matches)[matches.length-1] // Last number series in name
    var onlyName = fleetName.substring(0, fleetName.length-fleetNumber.length)
    var newFleetNumber = String(parseInt(fleetNumber) + 1)
    if (fleetNumber.length >= newFleetNumber.length) {
        byId("fleetName").value = onlyName + "0".repeat(fleetNumber.length-newFleetNumber.length) + newFleetNumber
        console.log(onlyName + "0".repeat(fleetNumber.length-newFleetNumber.length) + newFleetNumber)
        byId("createButton").click()
    }
}
if (byId("createButton")) {
    let plusOneButton = document.createElement('button');
    plusOneButton.innerHTML = "Same +1";
    plusOneButton.id = "CreateSame+1";
    plusOneButton.className = "submit darkbutton";
    plusOneButton.addEventListener("click", createAndIncrement);
    plusOneButton.title = "Increments the number at the end of the fleetname by 1"; //  doesn't create fleet yet
    byId("fleetEditForm").parentElement.append(plusOneButton);
}

//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 == 107 ) {//'107' is the keycode for numpad "+"
                e.preventDefault();
                unsafeWindow.createAndIncrement();
            }
        } catch (error) {console.log("slow down faggot")}
    }
};