Passed
Push — main ( cd158d...b5c9c5 )
by LCS
02:32
created

tools/qr.js   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 53
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7
mnd 3
bc 3
fnc 4
bpm 0.75
cpm 1.75
noi 4

1 Function

Rating   Name   Duplication   Size   Complexity  
B qr.js ➔ downloadImage 0 8 7
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!");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
12
    return;
13
  }
14
15
  // Clear previous QR
16
  qrCodeContainer.innerHTML = "";
17
18
  // Generate new QR
19
  qr = new QRCode(qrCodeContainer, {
0 ignored issues
show
Unused Code introduced by
The variable qr seems to be never used. Consider removing it.
Loading history...
Bug introduced by
The variable QRCode seems to be never declared. If this is a global, consider adding a /** global: QRCode */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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!");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
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
}