-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (52 loc) · 1.37 KB
/
main.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
import './style.css'
base64Text.oninput = handleBase64
utf8Text.oninput = handleUtf8
base64Copy.onclick = function () {
navigator.clipboard.writeText(base64Text.value)
}
utf8Copy.onclick = function () {
navigator.clipboard.writeText(utf8Text.value)
}
base64Past.onclick = function () {
navigator.clipboard.writeText(base64Text.value)
}
base64Past.onclick = async function () {
const text = await navigator.clipboard.readText()
base64Text.value = text
handleBase64()
}
utf8Past.onclick = async function () {
const text = await navigator.clipboard.readText()
utf8Text.value = text
handleUtf8()
}
function show(element, display = 'block') {
element.classList.add(display)
element.classList.remove('hidden')
}
function hide(element, display = 'block') {
element.classList.remove(display)
element.classList.add('hidden')
}
function handleBase64() {
hide(utf8Error, 'inline-block')
hide(base64Error, 'inline-block')
try {
utf8Text.value = atob(base64Text.value)
} catch ({ name }) {
utf8Text.value = ''
utf8Error.textContent = name
show(utf8Error, 'inline-block')
}
}
function handleUtf8() {
hide(base64Error, 'inline-block')
hide(utf8Error, 'inline-block')
try {
base64Text.value = btoa(utf8Text.value)
} catch ({ name }) {
base64Text.value = ''
base64Error.textContent = name
show(base64Error, 'inline-block')
}
}