generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
63 lines (55 loc) · 1.62 KB
/
script.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
const img = document.getElementById("img");
const input = document.getElementById("input");
const output = document.getElementById("output");
const radioPng = document.getElementById("radio-png");
const radioSvg = document.getElementById("radio-svg");
const copyFeedback = document.getElementById("copy-feedback");
let lastInput = "";
const callAPI = async () => {
if (lastInput === input.value) return;
lastInput = input.value;
if (!input.value) {
output.value = "";
img.src = "";
return;
}
const res = await fetch("./api?emoji=" + encodeURI(input.value));
if (res.ok) {
const src = await res.text();
img.src = src;
output.value = radioPng.checked ? src : src.replace(/72x72|png/g, "svg");
} else {
output.value = "There is no such twemoji...";
img.src = "";
}
};
const selectPng = () => {
if (img.src) {
output.value = output.value.replace(/svg/, "72x72").replace(/svg/, "png");
}
};
const selectSvg = () => {
if (img.src) {
output.value = output.value.replace(/72x72|png/g, "svg");
}
};
output.addEventListener("click", () => {
if (!output.value.startsWith("https")) return;
output.select();
// deno-lint-ignore no-window
const clipboard = navigator.clipboard || window.clipboard;
clipboard.writeText(output.value)
.then(() => {
copyFeedback.classList.add("show");
})
.then(() =>
setTimeout(() => {
copyFeedback.classList.remove("show");
}, 1000)
);
});
globalThis.addEventListener("load", (_event) => {
setInterval(callAPI, 1000);
});
radioPng.addEventListener("change", selectPng);
radioSvg.addEventListener("change", selectSvg);