-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
38 lines (33 loc) · 1010 Bytes
/
utils.mjs
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
export async function fetchText(url) {
const res = await fetch(url);
const content = await res.text();
return content;
}
export async function fetchJson(url) {
const res = await fetch(url);
const content = await res.json();
return content;
}
export function waitForClick() {
return new Promise((resolve) => {
document.addEventListener('click', resolve, { once: true });
});
}
export function sleepSecs(dt) {
return new Promise((resolve) => {
setTimeout(resolve, dt * 1000);
});
}
function removeIfAbove(maxAmount) {
const codeEls = Array.from(document.querySelectorAll('code'));
if (codeEls.length >= maxAmount) {
document.body.removeChild(codeEls[0]);
}
}
export function printAscii(data, classes = []) {
removeIfAbove(22);
const el = document.createElement('code');
['ascii', ...classes].forEach((cn) => el.classList.add(cn));
el.appendChild(document.createTextNode(data));
document.body.appendChild(el);
}