-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebterm.html
212 lines (201 loc) · 8.42 KB
/
webterm.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>WebTerm</title>
<style>
body {
font-family: monospace;
background-color: #000000;
color: rgb(255, 255, 255);
padding: 20px;
}
.cmd-line {
display: flex;
font: 1em sans-serif;
}
.cmd-line>span {
margin-right: 5px;
}
.cmd-input {
flex: 1;
border: none;
background: none;
color: inherit;
font: inherit;
outline: none;
white-space: pre;
}
.cmd-warn {
color: #ffff00;
}
.cmd-success {
color: #00ff00;
}
.cmd-error {
color: #ff0000;
}
</style>
</head>
<body>
<div id="cmd-container">
<div class="cmd-line">
<span>欢迎使用WebTerm功能,本终端将在线输入的SQL语句于数据库中执行查询,请注意使用时不要写错语句。</span>
</div>
<div class="cmd-line">
<span>WebTerm为简易实现,并未使用任何库,程序报错返回错误色</span>
<span class="cmd-error">[红色]</span>
<span>警告色</span>
<span class="cmd-warn">[黄色]</span>
<span>正常与成功色</span>
<span class="cmd-success">[绿色]</span>
</div>
<div class="cmd-line">
<span>部分危险语句即使正常执行也会标记为</span> <span class="cmd-warn">[警告色]</span>
</div>
<div class="cmd-line">
<span>WebTerm支持多行输入,只需Shift+Enter即可换行,支持按上下键回溯输入内容。</span>
</div>
<div class="cmd-line">
<span id="pretext"></span>
</div>
<div class="cmd-line">
<span>></span>
<div class="cmd-input" contenteditable="true"></div>
</div>
</div>
<script src="./comweb.js"></script>
<script>
let datetime = new Date();
let year = datetime.getFullYear();
let month = datetime.getMonth() + 1;
let day = datetime.getDate();
let hour = datetime.getHours();
let minute = datetime.getMinutes();
let second = datetime.getSeconds();
document.getElementById('pretext').innerText = "本次启动于" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
let inputs = [];
let lastinput = "";
let arrowidx = -1;
let lastnewinput = document.querySelector('.cmd-input');
function wraprawstr() {
lastnewinput.addEventListener('paste', (e) => {
//不带格式的粘贴
e.preventDefault();
const text = e.clipboardData.getData('text/plain');
document.execCommand('insertText', false, text);
});
}
wraprawstr();
document.addEventListener('DOMContentLoaded', () => {
const cmdContainer = document.getElementById('cmd-container');
function setCaretAtEnd(elem) {
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(elem);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
elem.focus();
}
cmdContainer.addEventListener('keydown', (e) => {
if (e.target.classList.contains('cmd-input')) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
const inputLine = e.target.innerText;
executeCommand(inputLine);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
if (inputs.length > 0) {
if (arrowidx == -1) {
lastinput = e.target.innerText || "";
arrowidx = inputs.length;
}
if (arrowidx == 0) { arrowidx = 1; }
arrowidx = arrowidx - 1;
e.target.innerText = inputs[arrowidx];
setCaretAtEnd(e.target);
}
} else if (e.key === 'ArrowDown') {
e.preventDefault();
if (arrowidx != -1 && inputs.length > 0) {
if (arrowidx == inputs.length - 1) {
e.target.innerText = lastinput;
arrowidx = -1;
} else {
arrowidx = arrowidx + 1;
e.target.innerText = inputs[arrowidx];
}
setCaretAtEnd(e.target);
}
}
}
});
lastnewinput.focus();
document.addEventListener('click', (e) => {
lastnewinput.focus();
});
function isDangerousSQL(sql) {
const dangerousKeywords = ['DROP', 'DELETE', 'UPDATE', 'ALTER', 'CREATE', 'TRUNCATE'];
const hasDangerousKeyword = dangerousKeywords.some(keyword => sql.toUpperCase().includes(keyword));
const hasUnsafeInput = sql.includes('--') || sql.includes(';');
return hasDangerousKeyword || hasUnsafeInput;
}
async function sqlRun(query) {
const response = await fetch('/webterm/exec', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: getToken(),
query: query
})
});
const result = await response.json();
if (result.status != 1) {
return ['Warn', result.msg];
} else {
if (result.success) {
if (isDangerousSQL(query)) {
return ['Warn', JSON.stringify(result.msg, null, 4)];
} else {
return ['Success', JSON.stringify(result.msg, null, 4)];
}
} else {
return ['Error', result.msg];
}
}
}
async function executeCommand(inputLine) {
inputs.push(inputLine);
const currentInput = document.querySelector('.cmd-input');
currentInput.contentEditable = 'false';
const outputLine = document.createElement('div');
outputLine.classList.add('cmd-line');
const output = await sqlRun(inputLine);
console.log(output);
if (output[0] === 'Warn') {
outputLine.innerHTML = '<div class="cmd-input cmd-warn">' + output[1] + '</div>';
} else if (output[0] === 'Success') {
outputLine.innerHTML = '<div class="cmd-input cmd-success">' + output[1] + '</div>';
} else if (output[0] === 'Error') {
outputLine.innerHTML = '<div class="cmd-input cmd-error">' + output[1] + '</div>';
} else {
outputLine.innerHTML = '<div class="cmd-input">' + output[1] + '</div>';
}
cmdContainer.appendChild(outputLine);
const newCmdLine = document.createElement('div');
newCmdLine.classList.add('cmd-line');
newCmdLine.innerHTML = '<span>></span><div class="cmd-input" contenteditable="true"></div>';
cmdContainer.appendChild(newCmdLine);
lastnewinput = newCmdLine.querySelector('.cmd-input');
lastnewinput.focus();
lastinput = "";
arrowidx = -1;
wraprawstr();
}
});
</script>
</body>
</html>