-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
173 lines (146 loc) · 4.05 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>WebSocket LogWatcher</title>
<style>
html,
body {
height: 100%;
background-color:rgb(187, 187, 187);
}
div.code {
white-space: pre;
font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
font-size: 12px;
color: #c7dfff;
background-color: rgb(49, 49, 49);
padding:5px;
height: 93%;
overflow-y: scroll;
}
.error {
color: white;
background-color:red;
padding: 2px;
}
.success {
color: white;
background-color:green;
padding: 2px;
}
.referer_tag {
display:inline-block;
color: white;
background-color: hsl(22, 100%, 43%);
padding: 3px;
margin-bottom: 5px;
}
.content_tag {
display:inline-block;
color: white;
background-color: hsl(157, 100%, 29%);
padding: 3px;
margin-bottom: 5px;
}
.toolbar {
display: block;
width: 100%;
padding:3px;
}
.controls {
display: inline-block;
width: 70%;
}
.status_display {
display: inline-block;
position: absolute;
text-align: right;
right:7px;
width: 20%;
}
</style>
<script type="text/javascript">
var ws;
function ws_connect() {
ws = new WebSocket("wss://websocket.your-domain.com"); // <- had to replace this address with yours
ws.onopen = function() {
document.getElementById("ws_server_status").innerHTML = "<span class='success'>Connection established</span>";
};
ws.onmessage = function(event) {
let data = JSON.parse(event.data);
if ( data.type == "log" ) {
let logformat = extract_log_details(data.logdata);
let formated_data = n2br(logformat);
let logplaceholder = document.getElementById("server_messages");
if( document.getElementById("clear_for_new_data").checked ) {
document.getElementById("server_messages").innerHTML="";
}
logplaceholder.insertAdjacentHTML('beforeend', "<br/>"+formated_data);
let placeholder_height = logplaceholder.scrollHeight;
logplaceholder.scrollTo(0, placeholder_height);
}
}
ws.onerror = function(event) {
console.log("Server err:", event.data);
ws.close();
}
ws.onclose = function(e) {
document.getElementById("ws_server_status").innerHTML = "<span class='error'>disconnected to server, reconnecting...</span>";
setTimeout(function() {
ws_connect();
}, 1000);
}
}
// this will remove some log details like timestamp, server ip etc. and just get the log content only
function extract_log_details(data) {
let arr = data.match(/\[client(.*)\](.*)/);
if (!arr) return data;
let arr1 = arr[0].split(']');
let log_details = arr1[1];
let string_index = data.indexOf(log_details);
let string_data = data.substring(string_index);
let log_date = data.match(/(\[(.*?)\])/);
let tag_string = data.match(/(?=#).*?(?=\s)/);
let ar_referer = string_data.match(/referer:(.*)/g);
let referer_string = (ar_referer == null) ? "" : ar_referer[0];
let tag_display = "";
if (tag_string !== null) {
tag_display = '<div class="content_tag">' + tag_string[0] + '</div>';
string_data = string_data.replace(tag_string[0],'');
}
//extract the referer: url
let referer_url = tag_display + '<div class="referer_tag">' + log_date[2] + ' | ' + referer_string + '</div><br/>';
let log_data = string_data.replace(/, referer:(.*)/g,'');
return referer_url + log_data;
}
// convert all newline character to <br>
function n2br(data){
const regex = /\\n|\\r\\n|\\n\\r|\\r/g;
return data.replace(regex, '<br>')
}
// clear status display
function clear_data() {
document.getElementById("server_messages").innerHTML="";
}
// reload the recent log data
function reload_data() {
if (ws) {
ws.send("reload");
}
}
// initiate connection
ws_connect();
</script>
</head>
<body>
<div class="toolbar">
<div class="controls">
<button onClick="reload_data()">Reload</button>
<button onClick="clear_data()">Clear</button>
<input type="checkbox" id="clear_for_new_data"> Auto-clear
</div>
<div id="ws_server_status" class="status_display"></div>
</div>
<div id="server_messages" class="language-java code"></div>
</body>
</html>