|
1 | 1 | document.addEventListener("DOMContentLoaded", () => { |
2 | | - console.log("Seasonal script initialized"); |
| 2 | + const isChristmas = new Date().getMonth() === 11; |
| 3 | + const checkbox = document.getElementById("christmas-theme-toggle"); |
3 | 4 |
|
4 | | - const isWithinChristmasPeriod = () => { |
5 | | - const today = new Date(); |
6 | | - const currentMonth = today.getMonth(); |
7 | | - return currentMonth === 11; |
| 5 | + const setIcon = (christmas) => { |
| 6 | + const icon = document.querySelector('.sidebar img[title="icon"]'); |
| 7 | + if (icon) icon.src = `/img/icon${christmas ? "-christmas" : ""}.png`; |
8 | 8 | }; |
9 | 9 |
|
10 | | - const updateIcon = () => { |
11 | | - const iconElement = document.querySelector('.sidebar img[title="icon"]'); |
12 | | - if (!iconElement) return; |
13 | | - const basePath = "/img/"; |
14 | | - const enabled = localStorage.getItem("christmasThemePreference") === "enabled"; |
15 | | - if (enabled && isWithinChristmasPeriod()) { |
16 | | - iconElement.src = `${basePath}icon-christmas.png`; |
17 | | - } else { |
18 | | - iconElement.src = `${basePath}icon.png`; |
19 | | - } |
20 | | - }; |
21 | | - |
22 | | - const loadThemeScript = (scriptPath) => { |
23 | | - const existingScript = document.getElementById("dynamic-theme-script"); |
24 | | - if (existingScript) existingScript.remove(); |
| 10 | + const loadScript = (src) => { |
| 11 | + const id = "dynamic-theme-script"; |
| 12 | + document.getElementById(id)?.remove(); |
25 | 13 | const script = document.createElement("script"); |
26 | | - script.id = "dynamic-theme-script"; |
27 | | - script.src = scriptPath; |
28 | | - script.async = true; |
| 14 | + Object.assign(script, { id, src, async: true }); |
29 | 15 | document.head.appendChild(script); |
30 | 16 | }; |
31 | 17 |
|
32 | | - const applyChristmasTheme = () => { |
33 | | - console.log("Applying Christmas theme"); |
34 | | - document.body.classList.add("christmas-theme"); |
35 | | - updateIcon(); |
36 | | - loadThemeScript("/js/christmas.js"); |
37 | | - }; |
| 18 | + const updateTheme = () => { |
| 19 | + const enabled = localStorage.getItem("christmasThemePreference") === "enabled"; |
| 20 | + const active = enabled && isChristmas; |
38 | 21 |
|
39 | | - const removeChristmasTheme = () => { |
40 | | - console.log("Removing Christmas theme"); |
41 | | - document.body.classList.remove("christmas-theme"); |
42 | | - updateIcon(); |
43 | | - loadThemeScript("/js/usual.js"); |
| 22 | + document.body.classList.toggle("christmas-theme", active); |
| 23 | + setIcon(active); |
| 24 | + loadScript(active ? "/js/christmas.js" : "/js/usual.js"); |
44 | 25 | }; |
45 | 26 |
|
46 | | - const toggleCheckbox = document.getElementById("christmas-theme-toggle"); |
47 | | - |
48 | | - let preference = localStorage.getItem("christmasThemePreference"); |
49 | | - if (!preference) { |
50 | | - preference = "enabled"; |
| 27 | + if (!localStorage.getItem("christmasThemePreference")) { |
51 | 28 | localStorage.setItem("christmasThemePreference", "enabled"); |
52 | 29 | } |
53 | 30 |
|
54 | | - if (preference === "enabled" && isWithinChristmasPeriod()) { |
55 | | - applyChristmasTheme(); |
56 | | - } else { |
57 | | - removeChristmasTheme(); |
58 | | - } |
59 | | - |
60 | | - if (toggleCheckbox) { |
61 | | - toggleCheckbox.checked = preference === "enabled"; |
62 | | - |
63 | | - toggleCheckbox.addEventListener("change", () => { |
64 | | - const newPref = toggleCheckbox.checked ? "enabled" : "disabled"; |
65 | | - localStorage.setItem("christmasThemePreference", newPref); |
66 | | - |
67 | | - if (newPref === "enabled" && isWithinChristmasPeriod()) { |
68 | | - applyChristmasTheme(); |
69 | | - } else { |
70 | | - removeChristmasTheme(); |
71 | | - } |
| 31 | + if (checkbox) { |
| 32 | + checkbox.checked = localStorage.getItem("christmasThemePreference") === "enabled"; |
| 33 | + checkbox.addEventListener("change", () => { |
| 34 | + localStorage.setItem("christmasThemePreference", checkbox.checked ? "enabled" : "disabled"); |
| 35 | + updateTheme(); |
72 | 36 | }); |
73 | 37 | } |
74 | 38 |
|
75 | | - updateIcon(); |
| 39 | + updateTheme(); |
76 | 40 | }); |
0 commit comments