-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.html
64 lines (60 loc) · 1.65 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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="node_modules/xterm/css/xterm.css" />
<script src="node_modules/xterm/lib/xterm.js"></script>
<!--
<script src="https://cdnjs.cloudflare.com/ajax/libs/xterm/3.14.5/xterm.min.js"></script>
-->
<script src="Javascript/xterm.js"></script>
</head>
<body>
<div id="terminal"></div>
<script>
var term = new Terminal({
cursorBlink: "block"
});
const ws = new WebSocket("ws://localhost:3000", "echo-protocol");
var curr_line = "";
var entries = [];
term.open(document.getElementById("terminal"));
term.write("web shell $ ");
term.prompt = () => {
if (curr_line) {
let data = { method: "command", command: curr_line };
ws.send(JSON.stringify(data));
}
};
term.prompt();
// Receive data from socket
ws.onmessage = msg => {
term.write("\r\n" + JSON.parse(msg.data).data);
curr_line = "";
};
term.on("key", function(key, ev) {
//Enter
if (ev.keyCode === 13) {
if (curr_line) {
entries.push(curr_line);
term.write("\r\n");
term.prompt();
}
} else if (ev.keyCode === 8) {
// Backspace
if (curr_line) {
curr_line = curr_line.slice(0, curr_line.length - 1);
term.write("\b \b");
}
} else {
curr_line += key;
term.write(key);
}
});
// paste value
term.on("paste", function(data) {
curr_line += data;
term.write(data);
});
</script>
</body>
</html>