-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
120 lines (118 loc) · 5.46 KB
/
index.php
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
<html>
<head>
<title>Command Line</title>
<meta charset="utf-8">
<style>
html, body{
padding: 12px;
background: #000;
color: #fff;
font-size: 16px !important;
font-family: "Menlo", "Raster Fonts", "Monospace";
}
#content{
position: absolute;
bottom: 50px;
height: calc(100vh - 50px);
overflow: auto;
width: calc(100% - 32px);
}
textarea{
float: right;
outline: none;
border: none;
background: transparent;
width: calc(100% - 15px);
resize: none;
color: #fff !important;
font-size: 16px !important;
font-family: "Menlo", "Raster Fonts", "Monospace";
}
.input{
width: calc(100% - 48px);
position: absolute;
bottom: 16px;
}
.input span{
line-height: 27px;
}
</style>
<script>
class CommandLine{
static parseCommand(event){
if(event.keyCode==13){
event.preventDefault();
CommandLine.sendCommand(event.srcElement.value)
event.srcElement.value = "";
CommandLine.historyindex = -1;
}
if(event.keyCode==38){
event.preventDefault();
CommandLine.historyindex += 1;
CommandLine.historyindex = Math.min(CommandLine.historyindex, CommandLine.commandhistory.length -1 );
CommandLine.loadHistoryCommand();
}
if(event.keyCode==40){
event.preventDefault();
CommandLine.historyindex -= 1;
CommandLine.historyindex = Math.max(-1, CommandLine.historyindex);
CommandLine.loadHistoryCommand();
}
if(event.keyCode==9){
event.preventDefault();
CommandLine.getSuggestion();
}
}
static sendCommand(command, pushToStack=true){
if(command=="clear"){
CommandLine.reset()
return
}
if(pushToStack) CommandLine.commandhistory.unshift(command);
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let answer = JSON.parse(xhttp.responseText);
console.log(answer);
CommandLine.print(answer["return"]);
}
};
xhttp.open("GET", "commandline/?command="+encodeURIComponent(command), true);
xhttp.send();
}
static print(content){
document.getElementById("content").innerHTML += "\n"+content;
document.getElementById("content").scrollTop = document.getElementById("content").scrollHeight;
}
static reset(content){
document.getElementById("content").innerHTML = "";
}
static loadHistoryCommand(){
if(CommandLine.historyindex==-1) document.getElementsByTagName('textarea')[0].value = "";
else document.getElementsByTagName('textarea')[0].value = CommandLine.commandhistory[CommandLine.historyindex];
document.getElementsByTagName('textarea')[0].focus();
document.getElementsByTagName('textarea')[0].setSelectionRange(document.getElementsByTagName('textarea')[0].value.length,document.getElementsByTagName('textarea')[0].value.length);
}
static getSuggestion(){
let input = document.getElementsByTagName('textarea')[0].value
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
let answer = JSON.parse(xhttp.responseText);
console.log(answer);
if(answer["return"]!=null && answer["return"]!="") document.getElementsByTagName('textarea')[0].value += answer["return"];
}
};
xhttp.open("GET", "commandline/autocomplete.php?input="+encodeURIComponent(input), true);
xhttp.send();}
}
CommandLine.commandhistory = [];
CommandLine.historyindex = -1;
CommandLine.sendCommand("init", false);
</script>
</head>
<body ondblclick="document.getElementsByTagName('textarea')[0].focus();">
<div class="input"><span>$</span><textarea autocomplete="off" spellcheck="false" autofocus rows="1" onkeydown="CommandLine.parseCommand(event);"></textarea></div>
<textarea disabled id="content" class="content"></textarea>
</body>
</html>