-
Notifications
You must be signed in to change notification settings - Fork 0
/
520.js
75 lines (67 loc) · 2.05 KB
/
520.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
const container = document.querySelector(".container");
const userInput = document.getElementById("userInput");
const submitBtn = document.getElementById("submit");
const downloadBtn = document.getElementById("download");
const sizeOptions = document.querySelector(".sizeOptions");
const BGColor = document.getElementById("BGColor");
const FGColor = document.getElementById("FGColor");
let QR_Code;
let sizeChoice, BGColorChoice, FGColorChoice;
//Set size
sizeOptions.addEventListener("change", () => {
sizeChoice = sizeOptions.value;
});
//Set background color
BGColor.addEventListener("input", () => {
BGColorChoice = BGColor.value;
});
//Set foreground color
FGColor.addEventListener("input", () => {
FGColorChoice = FGColor.value;
});
//Format input
const inputFormatter = (value) => {
value = value.replace(/[^a-z0-9A-Z]+/g, "");
return value;
};
submitBtn.addEventListener("click", async () => {
container.innerHTML = "";
//QR code genertion
QR_Code = await new QRCode(container, {
text: userInput.value,
width: sizeChoice,
height: sizeChoice,
colorDark: FGColorChoice,
colorLight: BGColorChoice,
});
//Set url for download
const src = container.firstChild.toDataURL("image/pmg");
downloadBtn.href = src;
let userValue = userInput.value;
try {
userValue = new URL(userValue).hostname;
} catch (_) {
userValue = inputFormatter(userValue);
downloadBtn.download = `${userValue}QR`;
downloadBtn.classList.remove("hide");
}
});
userInput.addEventListener("input", () => {
if (userInput.value.trim().length < 1) {
submitBtn.disabled = true;
downloadBtn.href = "";
downloadBtn.classList.add("hide");
} else {
submitBtn.disabled = false;
}
});
window.onload = () => {
container.innerHTML = "";
sizeChoice = 100;
sizeOptions.value = 100;
userInput.value = "";
BGColor.vavlue = BGColorChoice = "#ffffff";
FGColor.value = FGColorChoice = "#377dff";
downloadBtn.classList.add("hide");
submitBtn.disabled = true;
};