-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (38 loc) · 1.5 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
document.addEventListener("DOMContentLoaded", () => {
const themeSwitch = document.getElementById("themeSwitch");
const inputCode = document.getElementById("inputCode");
const outputCode = document.getElementById("outputCode");
const beautifyBtn = document.getElementById("beautifyBtn");
const clearBtn = document.getElementById("clearBtn");
const pasteBtn = document.getElementById("pasteBtn");
const copyBtn = document.getElementById("copyBtn");
themeSwitch.addEventListener("change", () => {
document.body.classList.toggle("dark-mode");
outputCode.parentElement.classList.toggle("dark-mode");
});
beautifyBtn.addEventListener("click", () => {
const code = inputCode.value;
const beautifiedCode = js_beautify(code);
outputCode.textContent = beautifiedCode;
hljs.highlightBlock(outputCode);
});
clearBtn.addEventListener("click", () => {
inputCode.value = "";
outputCode.textContent = "";
});
pasteBtn.addEventListener("click", async () => {
try {
const text = await navigator.clipboard.readText();
inputCode.value = text;
} catch (err) {
console.error("Failed to read clipboard contents: ", err);
}
});
copyBtn.addEventListener("click", async () => {
try {
await navigator.clipboard.writeText(outputCode.textContent);
} catch (err) {
console.error("Failed to copy text: ", err);
}
});
});