Total Complexity | 7 |
Complexity/F | 1.75 |
Lines of Code | 53 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | const qrText = document.getElementById("qrText"); |
||
2 | const generateBtn = document.getElementById("generateBtn"); |
||
3 | const downloadBtn = document.getElementById("downloadBtn"); |
||
4 | const qrCodeContainer = document.getElementById("qrcode"); |
||
5 | |||
6 | let qr; |
||
7 | |||
8 | generateBtn.addEventListener("click", () => { |
||
9 | const text = qrText.value.trim(); |
||
10 | if (!text) { |
||
11 | alert("Please enter text or a URL!"); |
||
|
|||
12 | return; |
||
13 | } |
||
14 | |||
15 | // Clear previous QR |
||
16 | qrCodeContainer.innerHTML = ""; |
||
17 | |||
18 | // Generate new QR |
||
19 | qr = new QRCode(qrCodeContainer, { |
||
20 | text: text, |
||
21 | width: 200, |
||
22 | height: 200, |
||
23 | colorDark: "#000000", |
||
24 | colorLight: "#ffffff", |
||
25 | }); |
||
26 | |||
27 | // Enable download after generating |
||
28 | setTimeout(() => { |
||
29 | downloadBtn.disabled = false; |
||
30 | }, 300); |
||
31 | }); |
||
32 | |||
33 | downloadBtn.addEventListener("click", () => { |
||
34 | const img = qrCodeContainer.querySelector("img"); |
||
35 | const canvas = qrCodeContainer.querySelector("canvas"); |
||
36 | |||
37 | if (img && img.src) { |
||
38 | downloadImage(img.src); |
||
39 | } else if (canvas) { |
||
40 | downloadImage(canvas.toDataURL("image/png")); |
||
41 | } else { |
||
42 | alert("Please generate a QR code first!"); |
||
43 | } |
||
44 | }); |
||
45 | |||
46 | function downloadImage(dataUrl) { |
||
47 | const link = document.createElement("a"); |
||
48 | link.href = dataUrl; |
||
49 | link.download = "qrcode.png"; |
||
50 | document.body.appendChild(link); |
||
51 | link.click(); |
||
52 | document.body.removeChild(link); |
||
53 | } |