-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (47 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// This function will fetch the data from the API and update the element
// It takes one parameter: the IP address of the server to check
function checkServerOnline(serverIP) {
// Get the element to be updated by its ID
let element = document.getElementById("server-online");
// Construct the API URL with the server IP as a query parameter
// Use All Origins proxy to bypass CORS issues
let apiURL = "https://api.allorigins.win/get?url=" + encodeURIComponent("https://api.mcsrvstat.us/simple/" + serverIP);
// Use the fetch API to make a GET request to the API
fetch(apiURL)
// Parse the response as JSON
.then((response) => response.json())
// Handle the data received from the API
.then((data) => {
// Get the actual data from the contents property
let actualData = data.contents;
// If the data is "online", the server is online
if (actualData === "Online") {
// Set the element text color to green and show the server status
element.style.color = "green";
element.innerHTML += "The Java SMP Server is currently online!";
}
// If the data is "offline", the server is offline
else if (actualData === "Offline") {
// Set the element text color to red and show the server status
element.style.color = "red";
element.innerHTML += "The Java SMP Server is currently offline.";
}
// If the data is neither "online" nor "offline", there is an error
else {
alert(actualData)
// Set the element text color to gray and show an error message
element.style.color = "gray";
element.innerHTML += "Error: Could not ping the Minecraft Server.";
}
})
// Handle any errors that occur during the fetch
.catch((error) => {
// Log the error to the console
console.error(error);
});
}
// This function will run once the page is loaded
window.onload = function () {
// Call the checkServerOnline function with the desired server IP
checkServerOnline("mc.liath.org");
};