-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.js
106 lines (81 loc) · 2.85 KB
/
API.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const apiAddress = "dev.aunacraft.de:8082"
let currentlyLoadedID = "Nothing is loaded!";
async function load(logID) {
document.title = "Loading..."
if(logID == null || logID === ""){
document.title = "AunaCraft | ChatLog [Error]"
setState(`<h1 style="color: #dd0027">Error</h1>
<h3 style="padding-bottom: 400px">LogID Field is empty</h3>`
)
return
}
const response = await fetch("http://" + apiAddress + "/chatlog/v1/get/" + logID + "/")
currentlyLoadedID = logID
if(response.ok) {
document.title = "AunaCraft | ChatLog#" + logID
const json = await response.json();
const messages = json["messages"]
const table = document.getElementById('table');
for (let i = 0; i < messages.length; i++) {
const messageObject = messages[i]
const uuid = messageObject["uuid"]
const time = messageObject["time"]
const displayName = messageObject["displayName"]
const message = messageObject["message"]
const dateObject = new Date(time)
var row = `<tr>
<td id="head"><img src="https://mc-heads.net/avatar/${uuid}/35.png"></td>
<td id="name">${escapeHtml(displayName) + ":"}</td>
<td id="message">${escapeHtml(message)}</td>
<td id="time">${dateObject.toLocaleString("de-DE")}</td>
</tr>`
table.innerHTML += row
}
}else {
document.title = "AunaCraft | ChatLog [Error]"
var errorMessage = "Response-Code: " + response.status;
if(response.status == 404){
errorMessage = "Requested Log does not exist"
}
setState(`<h1 style="color: #dd0027">Error</h1>
<h3 style="padding-bottom: 400px">${errorMessage}</h3>`
)
}
}
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
function resetTable(){
const table = document.getElementById('table');
table.innerHTML = ""
setState("")
}
document.getElementById("searchButton").addEventListener("click", function () {
const logID = document.querySelector("input").value;
if(logID == currentlyLoadedID){
setState("<h3>This Log is already shown!</h3>")
delay(1000).then(() => setState(""));
return
}
console.log("Showing log with id " + logID)
resetTable()
load(logID)
})
function escapeHtml(raw) {
return raw.replace(/[&<>"']/g, function onReplace(match) {
return '&#' + match.charCodeAt(0) + ';';
});
}
function setState(code) {
document.getElementById('state').innerHTML = code;
}
const url = new URL(document.location)
const logID = url.searchParams.get("logID")
if(logID != null) {
load(logID)
}else {
setState(
`<h1>Invalid Request</h1>
<h3 style="padding-bottom: 400px">URL-Parameter logID is missing</h3>`
)
}