Skip to content

Commit

Permalink
Handle non-extra server MOTDs more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley Chalmers committed Aug 16, 2020
1 parent 49b54ea commit 615248c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions mcstatus.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
white-space: pre;
font-family: monospace;
background-color: #000;
color: #bebebe;

width: 53ch;
padding: 10px;
Expand Down
22 changes: 18 additions & 4 deletions mcstatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ function loadStatus(parent, server, callback) {
}

function mcDescriptionToHTML(descriptionRaw) {
if (typeof descriptionRaw == "object") {
return mcExtraDescriptionToHTML(descriptionRaw["extra"]);
} else {
var root = document.createElement("div");
root.classList.add("mc-description");

description = descriptionRaw.replace(/\xa7./g, "");

root.appendChild(document.createTextNode(description));
return root;
}
}

function mcExtraDescriptionToHTML(descriptionRaw) {
const color_codes = {
"dark_red": "#be0000",
"red": "#fe3f3f",
Expand Down Expand Up @@ -85,7 +99,7 @@ function mcPlayersToHTML(playersRaw) {
var list = document.createElement("ul");
list.classList.add("mc-players-list");

if (playersRaw["online"] > 0) {
if (playersRaw["online"] > 0 && playersRaw["sample"].length > 0) {
playersRaw["sample"].forEach(function(player, index) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(player["name"]));
Expand All @@ -106,18 +120,18 @@ function handleStatus(parent, result) {
root.classList.add("mc-status-root");

// MOTD:
var descriptionRaw = [{
var descriptionRaw = {"extra": [{
"bold": true,
"color": "red",
"italic": false,
"obfuscated": false,
"strikethrough": false,
"text": "Server offline",
"underlined": false
}];
}]};

if (code == 200) {
descriptionRaw = status["description"]["extra"];
descriptionRaw = status["description"];
}
var description = mcDescriptionToHTML(descriptionRaw);
root.appendChild(description);
Expand Down

0 comments on commit 615248c

Please sign in to comment.