-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompt.js
54 lines (46 loc) · 1.54 KB
/
prompt.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
(function() {
// Get the information
var lines = [].slice.call(document.querySelectorAll('.prompt > *'));
var cmds = lines.map(function(line) { return line.getAttribute('data-cmd'); });
var htmls = lines.map(function(line) { return line.innerHTML; });
// Clear every element
lines.forEach(function(line) { line.innerHTML = ''; });
// Perform the typing of every line
var i = 0;
function typeLine() {
if (i < lines.length) {
// Get the information about the line
var line = lines[i], cmd = cmds[i], html = htmls[i];
// Initial line content
line.appendChild(document.createTextNode('root@qhack:~# '));
line.classList.add('active');
// Print every character
var c = 0;
function typeChar() {
if (c < cmd.length) {
// Print a char
var character = document.createTextNode(cmd.charAt(c));
line.appendChild(character);
c++;
setTimeout(typeChar, 70);
} else {
// Print the output
var result = document.createElement('div');
result.setAttribute('class', 'output');
result.innerHTML = html;
line.appendChild(result);
line.classList.remove('active');
i++;
typeLine();
}
}
setTimeout(typeChar, 300);
} else {
var last = document.createElement('p');
last.appendChild(document.createTextNode('root@qhack:~# '));
last.classList.add('active');
document.querySelector('.prompt').appendChild(last);
}
}
typeLine();
})();