-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (100 loc) · 3.61 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
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
108
109
110
111
112
113
114
// General
const scanBtn = document.getElementById("scanbtn");
const generateBtn = document.getElementById("generatebtn");
const scanDiv = document.getElementById("scandiv");
const generateDiv = document.getElementById("generatediv");
scanBtn.addEventListener("click", () => {
scanBtnBas();
});
generateBtn.addEventListener("click", () => {
generateBtnBas();
});
function scanBtnBas() {
if (scanDiv.style.display === "none") {
generateDiv.style.display = "none";
scanDiv.style.display = "block";
}
scanBtn.style.backgroundColor = "#434242";
scanBtn.style.color = "#F3EFE0";
generateBtn.style.backgroundColor = "";
generateBtn.style.color = "";
};
function generateBtnBas() {
if (generateDiv.style.display === "none") {
generateDiv.style.display = "block";
scanDiv.style.display = "none";
}
generateBtn.style.backgroundColor = "#434242";
generateBtn.style.color = "#F3EFE0";
scanBtn.style.backgroundColor = "";
scanBtn.style.color = "";
};
// Scan Section
const form = document.querySelector("form");
const fileInp = form.querySelector("input");
const infoText = form.querySelector("p");
const closeBtn = document.getElementById("close");
const copyBtn = document.getElementById("copy");
fileInp.addEventListener("change", async e => {
let file = e.target.files[0];
if(!file) return;
let formData = new FormData();
formData.append('file', file);
fetchRequest(file, formData);
});
function fetchRequest(file, formData) {
infoText.innerText = "Scanning QR Code...";
fetch("https://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;
document.querySelector("textarea").innerText = result;
form.querySelector("img").src = URL.createObjectURL(file);
document.getElementById("firstscan").style.display = "none";
form.querySelector("img").style.display = "block";
document.getElementById("results").style.display = "block";
}).catch(() => {
infoText.innerText = "Couldn't scan QR Code";
});
}
form.addEventListener("click", () => fileInp.click());
closeBtn.addEventListener("click", () => {
form.querySelector("img").style.display = "none";
document.getElementById("results").style.display = "none";
document.getElementById("firstscan").style.display = "block";
});
copyBtn.addEventListener("click", () => {
let text = document.querySelector("textarea").textContent;
navigator.clipboard.writeText(text);
});
// Generate Section
const generatedQR = document.getElementById("generatedqrcode");
const generateBtn2 = document.getElementById("generatebtn2");
qrInput = document.getElementById("urlinput");
qrImg = document.querySelector(".qrcode");
let preValue;
qrInput.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
let qrValue = qrInput.value.trim();
if(!qrValue || preValue === qrValue) return;
preValue = qrValue;
// generateBtn2.innerText = "Generating QR Code...";
qrImg.src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${qrValue}`;
qrImg.addEventListener("load", () => {
// generateBtn2.innerText = "Proceed";
generatedQR.style.display = "block";
});
}
});
/*
generateBtn2.addEventListener("click", () => {
});
*/
qrInput.addEventListener("keyup", () => {
if(!qrInput.value.trim()) {
preValue = "";
generatedQR.style.display = "none";
}
});