-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowsersource.html
153 lines (133 loc) · 4.97 KB
/
browsersource.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>2BeerBotRacer</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Abel&display=swap" rel="stylesheet">
<!-- <meta http-equiv="refresh" content="2"> -->
<style type="text/css">
body {
font-size: 1rem;
font-family: 'Abel', sans-serif;
font-weight: 400;
}
table { border-collapse: collapse; }
tr {
border: 1px solid #ccc;
}
th, td {
padding: 2px 5px 2px 5px;
font-size: 1.4rem;
background-color: black;
color: white;
}
td:first-child {
border-right: 1px solid #dfdfdf;
text-align: center;
}
td + td {
padding-left: 15px;
max-width: 187px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
img {
max-width: 100%;
height: auto;
}
.img--logo {
max-width: 300px;
margin-bottom: -7px;
}
#errors {
background: orange;
color: black;
font-weight: 700;
max-width: 312px;
}
#errors p {
padding: 10px;
margin: 0;
}
</style>
<script type="text/javascript">
function getCells(data, type) {
cells="";
for (const property in data) {
cells += `<${type}>${data[property]}</${type}>`;
}
return cells;
}
function createBody(data) {
return data.map(row => `<tr>${getCells(row, 'td')}</tr>`).join('');
}
function createTable(data) {
// Destructure the headings (first row) from
// all the rows
const [headings, ...rows] = data;
// Return some HTML that uses `getCells` to create
// some headings, but also to create the rows
// in the tbody.
return `
<thead><th colspan="2"><img class="img--logo" src="https://2beerminimum.com/2beerbotracer/natmar.png" alt=""></th></thead>
<tbody>${createBody(rows)}</tbody>
`;
}
function fillTable(data) {
if (Array.isArray(data)) {
data.unshift({"number": "number", "name": "name"});
let tablerender = createTable(data);
document.getElementById("entries").innerHTML = tablerender;
// console.log("it works?");
}
}
function setErrorMessage(message) {
dom_message = "";
if (message != "") {
console.log(message);
dom_message = "<p>" + message + "</p>";
}
document.getElementById("errors").innerHTML = dom_message;
}
function generateWS() {
ws = new WebSocket("ws://localhost:64209");
// Send a message to the server to trigger the queue send
ws.onopen = function(event) {
setErrorMessage("");
refresh_queue();
window.setInterval(refresh_queue, 2000);
};
ws.onclose = function(event) {
setErrorMessage('Connection to NATMAR bot is <em>inactive</em>.<br />Pick up the bat phone and call 2beer.');
setTimeout(generateWS, 5000);
};
ws.onerror = function(event) {
setErrorMessage(event.data);
}
// Receive the queue from the server
ws.onmessage = function(event) {
jsondata = JSON.parse(event.data);
// console.log(jsondata);
fillTable(jsondata);
};
return ws;
}
function refresh_queue() {
if (ws != null && ws.readyState == WebSocket.OPEN) {
ws.send("send_queue");
}
}
let ws = null;
generateWS();
</script>
</head>
<body>
<div class="wrap">
<table id="entries"></table>
<div id="errors"><p>Connection to NATMAR bot is <em>inactive</em>.<br />Pick up the bat phone and call 2beer.</p></div>
</div>
</body>
</html>