Last active
October 16, 2024 05:42
-
-
Save FuchsCrafter/27d693811eb8e67eaefe7d66c0ca5c07 to your computer and use it in GitHub Desktop.
Get the Online Status and Metadata of a minecraft server using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
const SERVER_URL = "mc.hypixel.net"; | |
// set any of below values to "off" if not needed | |
const HTML_STATUS_ELEMENT_ID = "status"; | |
const HTML_PLAYERCOUNT_ELEMENT_ID = "players"; | |
const HTML_MOTD_ELEMENT_ID = "motd"; | |
const HTML_ICON_ELEMENT_ID = "icon"; | |
// end | |
const ONLINE_TEXT = "Online!"; | |
const OFFLINE_TEXT = "Offline!"; | |
const MOTD_COLORS = true; // set to true if you want ingame colors, to false if you want default colors | |
const API_BASE_URL = "https://api.mcsrvstat.us/3/"; | |
var url = API_BASE_URL + SERVER_URL; | |
var xmlhttp = new XMLHttpRequest(); | |
xmlhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
let arr = JSON.parse(this.responseText); | |
setServerStatusDisplay(arr); | |
} | |
}; | |
xmlhttp.open("GET", url, true); | |
xmlhttp.send(); | |
function setServerStatusDisplay(arr) { | |
let statuselem, playerelem, motdelem, iconelem | |
if (HTML_STATUS_ELEMENT_ID !== "off") { statuselem = document.getElementById(HTML_STATUS_ELEMENT_ID);} | |
if (HTML_PLAYERCOUNT_ELEMENT_ID !== "off") { playerelem = document.getElementById(HTML_PLAYERCOUNT_ELEMENT_ID);} | |
if (HTML_MOTD_ELEMENT_ID !== "off") { motdelem = document.getElementById(HTML_MOTD_ELEMENT_ID);} | |
if (HTML_ICON_ELEMENT_ID !== "off") { iconelem = document.getElementById(HTML_ICON_ELEMENT_ID);} | |
if (arr["online"]) { | |
if (HTML_STATUS_ELEMENT_ID !== "off") {statuselem.innerHTML = ONLINE_TEXT; console.log("t")} | |
if (HTML_PLAYERCOUNT_ELEMENT_ID !== "off") {playerelem.innerHTML = arr["players"]["online"] + "/" + arr["players"]["max"];} | |
if (HTML_ICON_ELEMENT_ID !== "off") { | |
icon = document.createElement("img"); | |
icon.src = arr["icon"]; | |
iconelem.appendChild(icon); | |
} | |
if (HTML_MOTD_ELEMENT_ID !== "off") { | |
if (MOTD_COLORS) { | |
motdelem.innerHTML = arr["motd"]["html"]; | |
} else { | |
motdelem.innerHTML = arr["motd"]["clean"]; | |
} | |
} | |
} else { | |
statuselem.innerHTML = OFFLINE_TEXT; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment