-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
28 lines (25 loc) · 846 Bytes
/
index.js
File metadata and controls
28 lines (25 loc) · 846 Bytes
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
async function convert() {
console.log('converting');
const file = document.querySelector('#input').files[0];
const base64 = await toBase64(file);
copyToClipboard(base64);
}
function toBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => alert(error);
});
}
function copyToClipboard(text) {
const textarea = document.querySelector('#base64');
const container = document.querySelector('.result-container');
container.style.display = 'block';
textarea.value = text;
textarea.select();
textarea.setSelectionRange(0, text.length);
document.execCommand('copy');
}
const convertBtn = document.querySelector('.btn-convert');
convertBtn.addEventListener('click', convert);