-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (99 loc) · 3.04 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
let container1 = document.getElementById("container1");
let form = container1.querySelector("form");
let fileInput = form.querySelector("input");
let infoText = form.querySelector("p");
let QRData = container1.querySelector("textarea");
let QRImg = form.querySelector("img");
let copyBtn = document.getElementById("copy");
let resetBtn = document.getElementById("reset");
form.onclick = () => {
fileInput.click();
};
fileInput.onchange = (e) => {
let file = e.target.files[0];
if (!file) {
return;
}
let formData = new FormData();
formData.append("file", file);
fetchRequest(formData, file);
};
let fetchRequest = (formData, file) => {
infoText.innerText = "QR Code is uploading ....";
fetch("http://api.qrserver.com/v1/read-qr-code/", {
method: "POST",
body: formData,
})
.then((res) => res.json())
.then((result) => {
result = result[0].symbol[0].data;
infoText.innerText = result
? "Upload QR Code To Scan"
: "Couldn't Scan QR Code";
if (!result) {
return;
}
QRData.innerText = result;
QRImg.src = URL.createObjectURL(file);
container1.classList.add("active");
})
.catch(() => {
QRData.innerText = "Couldn't Scan The QR Code";
});
};
copyBtn.onclick = () => {
let text = QRData.textContent;
navigator.clipboard.writeText(text);
copyBtn.style.background = "#2532a5";
copyBtn.style.color = "#fff";
setTimeout(() => {
copyBtn.style.background = "#fff";
copyBtn.style.color = "#3744bd";
}, 100);
};
resetBtn.onclick = () => {
resetBtn.style.background = "#2532a5";
resetBtn.style.color = "#fff";
setTimeout(() => {
resetBtn.style.background = "#fff";
resetBtn.style.color = "#3744bd";
}, 100);
container1.classList.remove("active");
};
let container2 = document.getElementById("container2");
let QRText = container2.querySelector(".form input");
let generateBtn = document.getElementById("generate");
let qrImg = container2.querySelector(".qr-code img");
let downloadBtn = document.getElementById("download");
generateBtn.onclick = () => {
let QRValue = QRText.value;
if (!QRValue) {
return;
}
qrImg.src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${QRValue}`;
qrImg.onload = () => {
container2.classList.add("activate");
};
};
downloadBtn.onclick = () => {
let ImageURL = `https://api.qrserver.com/v1/create-qr-code/?size=500x500&data=${QRText.value}`;
//Fetching the response as a blob
fetch(ImageURL)
.then((res) => res.blob())
.then((file) => {
//URL.createObjectURL() creates a url of passed object
let tempURL = URL.createObjectURL(file);
//Create a anchor element
let aTag = document.createElement("a");
//Set its url as the tempurl
aTag.href = tempURL;
//Set the name of the downloaded file as filename
aTag.download = file.type;
document.body.appendChild(aTag);
// console.log(file);
// console.log(aTag.download);
//Clicking the aTag so that the file download
aTag.click();
aTag.remove();
});
};