-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
326 lines (276 loc) · 9.19 KB
/
script.js
File metadata and controls
326 lines (276 loc) · 9.19 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use strict"
let carat="█"
let prompt;
let currentdirectory=[]
let timer;
let res;
let responset;
let done;
let history=[];
let hi=0;
//list of commands avaliable
let commands={
'ls':lis,
'help':h,
'cd':cd,
'cat':ca,
'clear':clea
}
let expanded=false
function toggleExpand(){
expanded = !expanded;
if (expanded){
console.log(expanded)
let main=document.getElementById("terminal")
main.style.position="absolute"
main.style.top="0px"
main.style.left="0px"
main.style.height="100vh"
main.style.width="100vw"
}else{
}
}
//if autocomplet is wanted
let ac={
'cat':undefined,
'cd':undefined
}
function load(){
document.onkeydown = checkaKey;
clearInterval(timer)
let t=document.getElementById("terminal")
if (innerWidth<500){
t.style.position="absolute"
t.style.margin="0px"
t.style.right="0px"
t.style.top="0px"
t.style.left="0px"
t.style.width="100vw"
t.style.height="50vh"
t.style.borderRadius="0px"
}else{
t.style.verticalAlign="middle"
t.style.margin="auto"
t.style.borderRadius="10px"
t.style.width="50vw"
t.style.height="50vh"
}
prompt=document.getElementById("prompt").innerHTML;
timer=setInterval(() => {
let c=document.getElementById("cursorblink")
if (c.innerHTML==carat){
c.innerHTML=""
}else{
c.innerHTML=carat
}
}, 800);
document.getElementsByClassName('command')[document.getElementsByClassName('command').length-1].focus();
}
function cwd(){
let current=filesystem
console.log(currentdirectory)
for (let i=0;i<currentdirectory.length;i++){
current=current[currentdirectory[i]]
}
return current
}
function checkaKey(a){
let t=document.getElementsByClassName('command')
if (document.activeElement==t[t.length-1])setEndOfContenteditable(t[t.length-1]);
let e = a || window.event;
e=e.keyCode;
if(e==38) {a.preventDefault();gethistory(1);}
if(e==40) {a.preventDefault();gethistory(-1);}
if(e==9){a.preventDefault();autocomplete();}
}
function checkkey(key){
if(key == 13) runcommand();
}
function autocomplete(){
console.log("hello")
let t=document.getElementsByClassName('command')
let current=t[t.length-1]
console.log(current)
let cd=cwd()
let cmd=current.innerHTML.replace(/[<]br[^>]*[>]/gi,"").replace(/ /g,' ').trim().split(" ");
if (cmd.length>0){
if (cmd[0] in ac){
console.log(cmd)
let possible=[]
for (const fn in cd){
if (cmd.length>1 && fn.includes(cmd[cmd.length-1])){
console.log(fn)
possible.push(fn)
}else if(cmd.length==1){possible.push(fn)}
}
if (possible.length==1){
if (cmd.length>1){
cmd.pop()}
console.log(possible)
current.innerHTML=cmd.concat(possible).join(" ");
current.focus();
setEndOfContenteditable(current)
}
}
}
}
function gethistory(a){
if (a>0){
if (hi>0){
let t=document.getElementsByClassName('command')
if (hi==history.length){
history.push(t[t.length-1].innerHTML)
}
hi-=1
t[t.length-1].innerHTML=history[hi]
setEndOfContenteditable(t[t.length-1])
}
}else{
if (hi<history.length-1){
let t=document.getElementsByClassName('command')
hi+=1
t[t.length-1].innerHTML=history[hi]
setEndOfContenteditable(t[t.length-1])
}
}
}
//lol stolen func
function setEndOfContenteditable(contentEditableElement)
{
let range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
function runcommand(){
let t=document.getElementsByClassName('command')
t[t.length-1].setAttribute("contenteditable", false);
let command=t[t.length-1].innerHTML.replace(/[<]br[^>]*[>]/gi,"").replace(/ /g,' ').trim();
let output=(command.length>0)?"<span style='color:red;'>abc: command not found: ".concat(command.split(' ')[0],"</span>"):""
for (let key in commands){
if (command.split(' ')[0]==key){
hi+=1
history=history.slice(0,hi);
history.push(command)
output=commands[key](command.substring(key.length+1))
}
}
document.getElementById("history").innerHTML=document.getElementById("history").innerHTML.concat('<br>',output,(output.length>0)? '<br>' :'' , prompt)
let newprompt=document.getElementsByClassName('command')[document.getElementsByClassName('command').length-1]
newprompt.focus();
let dir=document.getElementsByClassName('dir')[document.getElementsByClassName('dir').length-1]
dir.innerHTML=(currentdirectory.length>0) ? currentdirectory[currentdirectory.length-1]:"root"
function setscroll(){
document.getElementById("texts").scrollTop=10000000}
setTimeout(setscroll,100)
}
//ls
function lis(x,he=false){
if (he){return "Lists all files in directory"}
let current=cwd()
console.log(current)
let final=[]
for (let key in current){
console.log(key)
if (current[key].constructor == Object){
final.unshift('<span style="color:white">'.concat(key,'</span>'))
}else{final.push(key)};
}
return final.join('<br>')
}
//help
function h(x,he=false){
if (he){return "Shows list commands"}
let final='<table><tr><th>Command</th><th>Desc</th></tr>'
for (let keys in commands){
final=final.concat('<tr><td>',keys,'</td><td>',commands[keys]('',true),'</td></tr>')
}
return final.concat('</table>')
}
//cd
function cd(x,he=false){
if (he){return "set current folder to folder given. Usage: cd (foldername)"}
if (x==""){currentdirectory=[]; return ''};
x=x.split('/')
return cd2(x)
}
function cd2(y){
let current=cwd()
let x = y.shift().replace(/[<]br[^>]*[>]/gi,"");
if (x=='.'){}
else if (x=='..'){
if (currentdirectory.length >0){
currentdirectory.pop()
}
}else{
if (x in current){
if (current[x].constructor == Object){
currentdirectory.push(x)
}else{
return "<span style='color:red;'>cd: not a directory: ".concat(x,"</span>")
}
} else{
return "<span style='color:red;'>cd: no such file or directory: ".concat(x,"</span>")
}
}
if (y.length>0){
return cd2(y)
}
return ''
}
//cat
function ca(x,he=false){
if (he){return "Outputs content of file. Usage: cat (filename with extension)"}
let current=cwd()
res=''
if (x in current){
if (current[x].constructor != Object){
let d=currentdirectory.join('/')
res=''
done=true
let loc=(window.location.pathname)
let path=loc.substring(0, loc.lastIndexOf('/'));
path+="".concat("/root/",d,(d!="")?'/':'',current[x])
console.log(path)
let xhr = new XMLHttpRequest();
xhr.open("GET", path, false);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
responset=(xhr.responseText);
} else {
console.log(xhr.statusText);
responset="<span style='color:red;'>Error Please contact person in charge</span>"
}
}
};
xhr.setRequestHeader("Access-Control-Allow-Origin","*")
xhr.send(null);
res=responset;
return res
}else{
return "<span style='color:red;'>cat: not a file: ".concat(x,"</span>")
}
}else{
return "<span style='color:red;'>cat: no such file or directory: ".concat(x,"</span>")
}
}
//clear
function clea(x,h=false){
if (h){return "Clears screen"}
document.getElementById("history").innerHTML=""
return ""
}