-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (37 loc) · 1.44 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
let imgBox = document.getElementById("imgBox");
let qrImage = document.getElementById("qrImage");
let qrText = document.getElementById("qrText");
function generateQr() {
if (qrText.value.length > 0) {
let bgColor = document.getElementById("bgColor").value.replace("#", "");
let fgColor = document.getElementById("fgColor").value.replace("#", "");
qrImage.src = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${qrText.value}&bgcolor=${bgColor}&color=${fgColor}`;
imgBox.classList.add("show-img");
document.querySelector(".download").style.display = "block";
} else {
qrText.classList.add("error");
setTimeout(() => {
qrText.classList.remove("error");
}, 1000);
}
}
function downloadQr(event) {
event.preventDefault();
if (qrImage.src !== "") {
fetch(qrImage.src)
.then(response => response.blob())
.then(blob => {
const blobUrl = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = blobUrl;
link.download = "qrcode.png";
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(link);
})
.catch(error => {
console.error("Error fetching image data:", error);
});
}
}