KeineAhnung4u / Map Selector for CraftNite

// ==UserScript==
// @name         Map Selector for CraftNite
// @namespace    https://openuserjs.org/user/KeineAhnung4u
// @version      1.0
// @description  Allows users to select a map before starting the game in CraftNite.
// @author       KeineAhnung4u
// @match        https://craftnite.io/*
// @run-at       document-start
// @license      MIT
// @copyright 2025, KeineAhnung4u (https://openuserjs.org/users/KeineAhnung4u)
// ==/UserScript==

function addGlobal(name, value) {
    var script = document.createElement('script');
    script.textContent = `window.${name} = ${value};`;
    (document.head||document.documentElement).appendChild(script);
    script.remove();
}

function addInputAbovePlayButton() {
    const playBtn = document.getElementById('playbtn');

    const customServer = document.createElement('input');
    customServer.id = 'customServer';
    customServer.type = 'text';
    customServer.placeholder = 'Enter map name (e.g., ffa1, ffa2)';
    customServer.value = localStorage.getItem('lastServer') ? localStorage.getItem('lastServer') : '';

    playBtn.parentNode.insertBefore(customServer, playBtn);

    const lineBreak = document.createElement('br');
    customServer.parentNode.insertBefore(lineBreak, customServer);
}

window.addEventListener('load', addInputAbovePlayButton);

function customStartBtn() {
    let nameValue = document.getElementById('name').value;
    addGlobal('playerName', nameValue ? JSON.stringify(nameValue) : JSON.stringify('unnamed'))

    setCookie("name", playerName, 365);
    setCookie("skin", playerSkin, 365);

    var inputValue = (document.getElementById('customServer').value || '').trim();
    var localStorageValue = localStorage.getItem('lastServer');
    if (inputValue === 'Random server 4u' || !inputValue) {
        requestServerName();
    } else if (localStorageValue && inputValue === 'Last server') {
        localStorage.setItem('lastServer', localStorageValue);
        G.gameServerAddress = localStorageValue;
        playGame();
    } else {
        localStorage.setItem('lastServer', inputValue);
        G.gameServerAddress = inputValue;
        playGame();
    }
}

var originalSend = window.XMLHttpRequest.prototype.send;
var originalOpen = window.XMLHttpRequest.prototype.open;

window.XMLHttpRequest.prototype.open = function(method, url) {
    this._url = url;
    return originalOpen.apply(this, arguments);
};

window.XMLHttpRequest.prototype.send = function() {
    if (this._url && this._url.startsWith('https://craftnite.io/gs/requestServer.php')){
        this.addEventListener('readystatechange', function() {
            if (this.readyState === 4) {
                var inputValue = (document.getElementById('customServer')?.value || '').trim();
                var localStorageValue = localStorage.getItem('lastServer');
                if (inputValue === 'Random server' || !inputValue) {
                    localStorage.setItem('lastServer', this.responseText);
                    return this.responseText
                }
            }
        }, false);
    }

    if (this._url.startsWith('https://craftnite.io') || this._url === 'a.zip') {
        return originalSend.apply(this, arguments);
    }
};

window.addEventListener('load', () => {
    const playBtn = document.getElementById('playbtn');
    if (playBtn) {
        playBtn.removeAttribute('onclick');
        playBtn.onclick = customStartBtn;
    }
});