/** Main JS file for WebConsole. https://github.com/mesacarlos 2019 Carlos Mesa under MIT License. */ /** * Global variables */ var connectionManager = new WebConsoleManager(); var statusCommandsInterval = -1; var commandHistoryIndex = -1; //Saves current command history index. -1 when not browsing history. /** * Prepare and show server to user */ function openServer(serverName){ //Create or retrieve connection connectionManager.loadConnection(serverName); //Load saved messages var i; var messages = connectionManager.activeConnection.messages; for(i = 0; i < messages.length; i++){ if(messages[i].status != 401){ onWebSocketsMessage(messages[i]); } } //Subscribe a function connectionManager.activeConnection.subscribe(onWebSocketsMessage); } function onWebSocketsMessage(message){ switch (message.status) { case 10: //Console Output writeToWebConsole(message.message); break; case 200: //Processed writeToWebConsole(message.message); break; case 400: //Unknown Command writeToWebConsole(message.message); break; case 401: //Send password connectionManager.activeConnection.sendToServer("LOGIN 1234"); break; case 1000: //Players writePlayerInfo(message.connectedPlayers, message.maxPlayers); connectionManager.activeConnection.players = JSON.parse(message.players); break; case 1001: //Cpu Usage writeCpuInfo(message.usage); break; case 1002: //RAM Usage writeRamInfo(message.free, message.used, message.max); break; default: console.log('Unknown server response:'); } console.log(message); //Add interval for Players, CPU and RAM info, if not set if(statusCommandsInterval == -1 && message.status !== 401){ statusCommandsInterval = setInterval(function(){ connectionManager.askForInfo(); }, 2500); } } /** * Write to console */ function writeToWebConsole(msg){ var isScrolledDown = document.getElementById("consoleTextArea").scrollHeight - document.getElementById("consoleTextArea").scrollTop - 40 == $("#consoleTextArea").height(); //Write to div, replacing < to < (to avoid XSS) and replacing new line to br. msg = msg.replace(/"); //Color filter for Windows (thanks to SuperPykkon) msg = msg.replace(/\[0;30;22m/g, ""); //&0 msg = msg.replace(/\[0;34;22m/g, ""); //&1 msg = msg.replace(/\[0;32;22m/g, ""); //&2 msg = msg.replace(/\[0;36;22m/g, ""); //&3 msg = msg.replace(/\[0;31;22m/g, ""); //&4 msg = msg.replace(/\[0;35;22m/g, ""); //&5 msg = msg.replace(/\[0;33;22m/g, ""); //&6 msg = msg.replace(/\[0;37;22m/g, ""); //&7 msg = msg.replace(/\[0;30;1m/g, ""); //&8 msg = msg.replace(/\[0;34;1m/g, ""); //&9 msg = msg.replace(/\[0;32;1m/g, ""); //&a msg = msg.replace(/\[0;36;1m/g, ""); //&b msg = msg.replace(/\[0;31;1m/g, ""); //&c msg = msg.replace(/\[0;35;1m/g, ""); //&d msg = msg.replace(/\[0;33;1m/g, ""); //&e msg = msg.replace(/\[0;37;1m/g, ""); //&f msg = msg.replace(/\[m/g, ""); //&f //Color filter for UNIX (This is easier!) //span may not be closed every time but browsers will do for ourselves msg = msg.replace(/§0/g, ""); //&0 msg = msg.replace(/§1/g, ""); //&1 msg = msg.replace(/§2/g, ""); //&2 msg = msg.replace(/§3/g, ""); //&3 msg = msg.replace(/§4/g, ""); //&4 msg = msg.replace(/§5/g, ""); //&5 msg = msg.replace(/§6/g, ""); //&6 msg = msg.replace(/§7/g, ""); //&7 msg = msg.replace(/§8/g, ""); //&8 msg = msg.replace(/§9/g, ""); //&9 msg = msg.replace(/§a/g, ""); //&a msg = msg.replace(/§b/g, ""); //&b msg = msg.replace(/§c/g, ""); //&c msg = msg.replace(/§d/g, ""); //&d msg = msg.replace(/§e/g, ""); //&e msg = msg.replace(/§f/g, ""); //&f msg = msg.replace(/§l/g, ""); //&l msg = msg.replace(/§m/g, ""); //&m msg = msg.replace(/§n/g, ""); //&n msg = msg.replace(/§o/g, ""); //&o msg = msg.replace(/§r/g, ""); //&r $("#consoleTextArea").append(msg + "
"); if(isScrolledDown){ var textarea = document.getElementById('consoleTextArea'); textarea.scrollTop = textarea.scrollHeight; } } /** * Fill connected players card */ function writePlayerInfo(connected, maximum){ $("#connectedPlayers").text(connected); $("#maxPlayers").text(maximum); var percent = (connected/maximum)*100; $("#playerProgressBar").width(percent + "%"); } /** * Fill CPU info card */ function writeCpuInfo(usage){ $("#cpuInfo").text(usage + "%"); $("#CpuProgressBar").width(usage + "%"); } /** * Fill RAM info card */ function writeRamInfo(free, used, total){ $("#usedRam").text(used); $("#totalRam").text(total); var percent = (used/total)*100; $("#RamProgressBar").width(percent + "%"); } /** * Called from WebConsoleConnector only. */ function closedConnection(){ $("#offlineContainer").show(); $("#serverContainer").hide(); }