-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebconsole.js
More file actions
100 lines (96 loc) · 2.75 KB
/
webconsole.js
File metadata and controls
100 lines (96 loc) · 2.75 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
// from https://stackoverflow.com/questions/2794137/sanitizing-user-input-before-adding-it-to-the-dom-in-javascript
// https://www.sitepoint.com/delay-sleep-pause-wait/
var Webconsole = (function() {
return {
sanitize: function(string) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
"/": '/',
"`": '`'
};
const reg = /[&<>"'/]/ig;
return string.replace(reg, (match)=>(map[match]));
},
sleep: function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
addTo: function(element) {
var webconsoleHTML = `
<div class="main">
Output:<br>
<div contenteditable="false" id="webconsole-out"></div>
<br><hr>
Input:<br>
<input type="text" contenteditable="true" id="webconsole-in" autofocus></input>
</div>
<footer>
<em><p>Powered by <a href="https://github.com/reeceyang/webconsole">Webconsole</a>. Made with <3 by <a href="https://github.com/reeceyang">Reece</a>.</p></em>
</footer>`;
element.innerHTML += webconsoleHTML;
},
addDarkModeTo: function(element) {
element.innerHTML = "<a id=\"toggle-theme\">Dark/Light Mode</a>" + element.innerHTML;
document.getElementById("toggle-theme").addEventListener("click", () => {
document.body.classList.toggle("dark");
});
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
document.getElementById("toggle-theme").click();
}
},
currentlyInputting: false,
};
})();
function println() {
var s = "";
for (i in arguments) {
s += arguments[i];
}
print(s, "<br>");
}
function print() {
setTimeout(() => {
var out = document.getElementById("webconsole-out");
var s = "";
for (i in arguments) {
s += arguments[i];
}
out.innerHTML += s;
//console.log("printing " + s);
//console.log("out is: " + out.innerHTML);
out.scrollTop += 100;
}, 0);
}
function clear() {
setTimeout(() => {
var out = document.getElementById("webconsole-out");
out.innerHTML = "";
}, 0);
}
async function input() {
var inp = document.getElementById("webconsole-in");
for (i in arguments) {
print(arguments[i]);
}
var enterPressed = new Promise(function(resolve, reject) {
inp.addEventListener("keyup", function(event) {
if(event.key === "Enter" & Webconsole.currentlyInputting) {
//console.log("enter pressed");
resolve("Yes");
}
});
});
Webconsole.currentlyInputting = true;
//console.log("waiting for enterPressed");
await enterPressed;
//console.log("enterPressed");
var stuff = Webconsole.sanitize(inp.value);
inp.value = "";
print(stuff);
println();
Webconsole.currentlyInputting = false;
return stuff;
}