-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.html
107 lines (88 loc) · 2.18 KB
/
console.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
<style>
body {
margin: 0;
}
.console * {
font-family: monospace;
}
.console {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
background: #111111;
}
.console-history {
flex-grow: 1;
box-sizing: border-box;
padding: 24px;
overflow-y: auto;
}
.console-input-log,
.console-output-log {
box-sizing: border-box;
padding: 6px 12px;
}
.console-input-log:hover,
.console-output-log:hover {
background: rgba(255, 255, 255, 0.03);
}
.console-input-log {
color: #ffffff;
font-weight: bold;
}
.console-output-log {
color: #00ffcc;
}
.console-input {
flex-shrink: 0;
box-sizing: border-box;
padding: 12px;
outline: none;
border: none;
border-top: 1px solid #333333;
color: #ffffff;
background: rgba(255, 255, 255, 0.05);
transition: background 0.2s;
}
.console-input:hover,
.console-input:focus {
background: rgba(255, 255, 255, 0.07);
}
</style>
<html>
<div class="console">
<div class="console-history"></div>
<input class="console-input" type="text" autofocus spellcheck="false">
</div>
</html>
<script>
const consoleInput = document.querySelector(".console-input");
const historyContainer = document.querySelector(".console-history");
function addResult(inputAsString, output) {
const outputAsString =
output instanceof Array ? `[${output.join(", ")}]` : output.toString();
const inputLogElement = document.createElement("div");
const outputLogElement = document.createElement("div");
inputLogElement.classList.add("console-input-log");
outputLogElement.classList.add("console-output-log");
inputLogElement.textContent = `> ${inputAsString}`;
outputLogElement.textContent = outputAsString;
historyContainer.append(inputLogElement, outputLogElement);
}
consoleInput.addEventListener("keyup", (e) => {
const code = consoleInput.value.trim();
if (code.length === 0) {
return;
}
if (e.key === "Enter") {
try {
addResult(code, eval(code));
} catch (err) {
addResult(code, err);
}
consoleInput.value = "";
historyContainer.scrollTop = historyContainer.scrollHeight;
}
});
</script>