Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (91 loc) · 3.31 KB
/
Copy pathscript.js
File metadata and controls
99 lines (91 loc) · 3.31 KB
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
const container = document.getElementsByClassName("container");
const textBox = document.getElementById("text");
const generateBtn = document.getElementById("Generate");
const QRSection = document.getElementById("qrCodeSection");
let QRImage = document.getElementsByClassName("QRImg");
const downloadBtn = document.getElementById("Download");
const link = document.getElementById("link");
const pngformat = document.getElementById("png");
const jpegformat = document.getElementById("jpeg");
const svgformat = document.getElementById("svg");
const select = document.getElementById("Select");
const fileFormat = document.getElementById("fileTypes");
const Types = document.getElementsByClassName("Types");
let QRText;
let QRType;
QRShow();
fileChange();
async function QRGeneration(QRText) {
let response = await fetch(`https://api.qrserver.com/v1/create-qr-code/?&data=${encodeURIComponent(QRText)}&format=svg&ecc=H&charset-source=UTF-8`);
let QRCode = response.url;
QRAdd(QRCode);
}
async function QRFileTypeGeneration(QRText , QRType) {
let response = await fetch(`https://api.qrserver.com/v1/create-qr-code/?&data=${encodeURIComponent(QRText)}&format=${QRType}&ecc=H&charset-source=UTF-8`);
let QRdownload = await response.blob();
let QRImage = window.URL.createObjectURL(QRdownload);
Download(QRImage);
}
function QRShow() {
textBox.addEventListener("input", () => {
QRText = textBox.value;
if (QRText != "") {
["click","keypress"].forEach((event)=>{
generateBtn.removeEventListener(event , QRGenerationEvent);
generateBtn.addEventListener(event , QRGenerationEvent);
})
}
else {
container[0].classList.remove("displayOpen");
QRSection.classList.add("hide");
QRSection.classList.remove("show");
imageRemove();
}
})
}
function QRGenerationEvent() {
if(downloadBtn.childElementCount == 1){
downloadBtn.firstElementChild.remove();
}
generateBtn.innerText = "Generating QR Code...";
let start = performance.now();
QRGeneration(QRText);
let end = performance.now();
let responseTime = end - start;
setTimeout(() => {
generateBtn.innerText = "Generate QR Code";
container[0].classList.add("displayOpen");
QRSection.classList.remove("hide");
QRSection.classList.add("show");
}, (responseTime + 500))
}
function QRAdd(QRCode) {
imageRemove();
let Image = `<img class="QRImg" src="${QRCode}" alt="QR-Code">`;
QRSection.insertAdjacentHTML("afterbegin", Image);
QRImage = document.getElementsByClassName("QRImg");
}
function imageRemove() {
Array.from(QRImage).forEach((img) => {
img.remove();
})
}
function Download(QRImage) {
let link = document.createElement('a');
downloadBtn.appendChild(link);
link.href = QRImage;
link.download = "QR-Code";
}
function fileChange(){
select.addEventListener("click" , ()=>{
fileFormat.classList.toggle("hide");
fileFormat.classList.toggle("show");
Array.from(Types).forEach((e)=>{
e.addEventListener("click" , ()=>{
select.innerText = e.innerText;
QRType = e.innerText.toLowerCase();
QRFileTypeGeneration(QRText , QRType);
})
})
})
}