Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (65 loc) · 2.34 KB
/
Copy pathscript.js
File metadata and controls
79 lines (65 loc) · 2.34 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
/* =====================================================
QR Code Generator + Dark Mode (Inverted QR Support)
===================================================== */
/* ---------- DOM ELEMENTS ---------- */
const qrInput = document.getElementById("qr-code-url");
const generateBtn = document.getElementById("generate-btn");
const qrContainer = document.getElementById("qrcode");
const themeToggle = document.getElementById("theme-toggle");
/* ---------- STATE ---------- */
// Stores the last generated QR text so we can regenerate on theme change
let currentQRText = "";
/* ---------- QR CODE GENERATION ---------- */
/**
* Generates a QR code based on the provided text.
* Automatically inverts QR colors based on theme.
*/
function generateQRCode(text) {
// Clear any existing QR code
qrContainer.innerHTML = "";
// Check if dark mode is active
const isDarkMode = document.body.classList.contains("dark");
// Create QR code with theme-aware colors
new QRCode(qrContainer, {
text: text,
width: 200,
height: 200,
colorDark: isDarkMode ? "#ffffff" : "#000000",
colorLight: isDarkMode ? "#121212" : "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
}
/* ---------- GENERATE BUTTON ---------- */
generateBtn.addEventListener("click", () => {
const value = qrInput.value.trim();
// Prevent empty QR generation
if (!value) {
alert("Please enter text or a URL");
return;
}
currentQRText = value;
generateQRCode(currentQRText);
});
/* ---------- THEME TOGGLE ---------- */
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark");
const isDarkMode = document.body.classList.contains("dark");
// Update toggle button text
themeToggle.textContent = isDarkMode
? "☀️ Light Mode"
: "🌙 Dark Mode";
// Regenerate QR to invert colors if QR already exists
if (currentQRText) {
generateQRCode(currentQRText);
}
// Persist theme preference
localStorage.setItem("theme", isDarkMode ? "dark" : "light");
});
/* ---------- LOAD SAVED THEME ---------- */
(function loadSavedTheme() {
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
document.body.classList.add("dark");
themeToggle.textContent = "☀️ Light Mode";
}
})();