-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
160 lines (149 loc) · 6.24 KB
/
app.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
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
input = document.querySelector('.console-input-text')
output = document.querySelector('.console-output')
input_before = document.querySelector('.console-input-before').innerHTML
discordId = document.querySelector('.discord-id')
document.addEventListener('keydown', keyPressEvents);
input.addEventListener('input', resizeInput);
resizeInput.call(input);
defaultOutput()
var commandHistory = [];
var path = 0;
var executingCommand = false;
function printList(data) {
for (let x = 0; x < data.length; x++) {
output.innerHTML += '<pre>' + data[x] + '</pre>'
}
}
function printCommand() {
command = input.value
output.innerHTML += '<pre class="console-input-before">' + input_before + ' <span>' + command + '</span>' + '</pre>'
input.value = ''
input.style.width = '0ch'
}
function defaultOutput() {
fetch('./assets/text/console.json')
.then(response => response.json())
.then(data => {
printList(data.startup);
})
}
function selectCommandLine() {
input.focus()
}
function keyPressEvents(e) {
if (e.keyCode == 9) {
selectCommandLine()
} else if (e.keyCode == 13) {
executeCommand()
}
//else if (e.keyCode == 38) {
// input.value = commandHistory[commandHistory.length - 1]
//}
}
function executeCommand() {
if (executingCommand) { return };
var command = input.value.split(' ')[0];
var argument = input.value.split(' ')[1];
let isCommand = false;
fetch('./assets/text/console.json')
.then(response => response.json())
.then(data => {
for (let x = 0; x < data.commands.length; x++) {
if (data.commands[x] == command) { isCommand = true; }
else { isCommand ? isCommand = true : isCommand = false; }
}
if (isCommand) {
executingCommand = true;
switch (command) {
case 'clear':
input.value = ''
input.style.width = '0ch'
output.innerHTML = '';
executingCommand = false;
break;
case 'exit':
output.innerHTML = ''
let x = 0
input.value = ''
input.style.width = '0ch'
setInterval(type, 2000)
function type() {
if (x < data.secret.length) {
output.innerHTML += '<pre>' + data.secret[x] + '</pre>'
x++
autoScroll()
}
}
executingCommand = false
output.innerHTML += '<pre> </pre>'
break;
case 'cat':
console.log(argument)
fetch('./assets/text/console.json')
.then(response => response.json())
.then(data => {
printCommand()
if (data.path[argument]) {
if (data.path[argument]["type"] != "file") {
if (data.path[argument]["type"] == "directory") {
output.innerHTML += '<pre>You can\'t view a directory</pre>'
}
} else {
printList(data.path[argument]["content"]);
}
} else {
output.innerHTML += '<pre>Can\'t find the file you want to view</pre>'
}
output.innerHTML += '<pre> </pre>'
executingCommand = false
})
break;
case 'ls':
if (!argument) {
printCommand()
printList(data.output[command])
output.innerHTML += '<pre> </pre>'
executingCommand = false;
} else {
console.log(argument)
fetch('./assets/text/console.json')
.then(response => response.json())
.then(data => {
printCommand()
if (data.path[argument]) {
if (data.path[argument]["type"] != "directory") {
if (data.path[argument]["type"] == "file") {
output.innerHTML += '<pre>You can\'t navigate into a file</pre>'
}
} else {
printList(data.path[argument]["content"]);
}
} else {
output.innerHTML += '<pre>Can\'t find the file you want to view</pre>'
}
output.innerHTML += '<pre> </pre>'
executingCommand = false
})
}
break;
default:
printCommand()
printList(data.output[command])
output.innerHTML += '<pre> </pre>'
executingCommand = false
break;
}
} else {
printCommand()
output.innerHTML += '<pre class="console-output-error">command not found</pre>'
output.innerHTML += '<pre> </pre>'
}
autoScroll()
})
}
function resizeInput() {
this.style.width = this.value.length + "ch";
}
function autoScroll() {
window.scrollTo(0,document.body.scrollHeight);
}