-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
67 lines (60 loc) · 1.57 KB
/
background.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
document.addEventListener("DOMContentLoaded", function(){
var terminal = getElem("prompt");
var result = getElem("writer");
var commands = [];
var upCounter = 0;
var ws = new WebSocket("ws://localhost:50667");
function getElem(id){
return document.getElementById(id);
}
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function nl2br(txt){
return txt.replace(/\n/g, "<br />");
}
function sendCmd(msg){
ws.send(msg);
}
ws.onmessage = function(msg){
var res = nl2br(JSON.parse(msg.data));
result.innerHTML += res;
result.scrollTop = result.scrollHeight;
terminal.value = null;
}
function rlwrap(){
terminal.value = commands.reverse()[upCounter-1];
commands = commands.reverse();
if(terminal.value == "undefined"){
terminal.value = null;
upCounter = 0;
}
}
terminal.addEventListener("keydown", function(e){
switch(e.keyCode){
case 13: //enter key
var cmd = terminal.value;
commands.push(cmd);
commands = commands.filter(onlyUnique);
console.log(commands);
upCounter = 0;
sendCmd(cmd);
cmd = "q)"+cmd+"<br />";
result.innerHTML += cmd;
break;
case 38: //up key
upCounter++;
console.log(upCounter);
rlwrap();
break;
case 40: //down key
upCounter--;
console.log(upCounter);
rlwrap();
break;
default:
upCounter = 0;
}
},false);
terminal.focus();
},false);