Create src/client.js
Browse files- src/client.js +50 -0
src/client.js
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
const socketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
3 |
+
const socketUrl = `${socketProtocol}//${window.location.host}`;
|
4 |
+
const socket = new WebSocket(socketUrl);
|
5 |
+
|
6 |
+
socket.onmessage = (event) => {
|
7 |
+
term.write(event.data);
|
8 |
+
|
9 |
+
}
|
10 |
+
|
11 |
+
var term = new window.Terminal({
|
12 |
+
cursorBlink: true
|
13 |
+
});
|
14 |
+
term.open(document.getElementById('terminal'));
|
15 |
+
|
16 |
+
function init() {
|
17 |
+
if (term._initialized) {
|
18 |
+
return;
|
19 |
+
}
|
20 |
+
|
21 |
+
term._initialized = true;
|
22 |
+
|
23 |
+
term.prompt = () => {
|
24 |
+
runCommand('\n');
|
25 |
+
};
|
26 |
+
setTimeout(() => {
|
27 |
+
term.prompt();
|
28 |
+
}, 300);
|
29 |
+
|
30 |
+
term.onKey(keyObj => {
|
31 |
+
runCommand(keyObj.key);
|
32 |
+
});
|
33 |
+
|
34 |
+
term.attachCustomKeyEventHandler((e) => {
|
35 |
+
if ((e.ctrlKey || e.metaKey) && e.key === 'v') {
|
36 |
+
navigator.clipboard.readText().then(text => {
|
37 |
+
runCommand(text);
|
38 |
+
});
|
39 |
+
return false;
|
40 |
+
}
|
41 |
+
return true;
|
42 |
+
});
|
43 |
+
}
|
44 |
+
|
45 |
+
function runCommand(command) {
|
46 |
+
socket.send(command);
|
47 |
+
|
48 |
+
}
|
49 |
+
|
50 |
+
init();
|