diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-02-09 01:24:23 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-02-09 01:24:23 +0300 |
| commit | 3955fbabc9ed05116be07c9f659cd550734f8593 (patch) | |
| tree | 109ebc5e0946d44b4766219a02b74dac36795300 /static/boot.js | |
| parent | f2bb77e460acfd5929ecc6de053b68932f2c8121 (diff) | |
go FAST
Diffstat (limited to 'static/boot.js')
| -rw-r--r-- | static/boot.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/static/boot.js b/static/boot.js new file mode 100644 index 0000000..0bec294 --- /dev/null +++ b/static/boot.js @@ -0,0 +1,84 @@ +(() => { + if (window.__wsapBootInitialized) return; + window.__wsapBootInitialized = true; + + const getCurrentTheme = () => + document.documentElement.getAttribute("data-theme") === "dark" + ? "dark" + : "light"; + + const updateThemeToggleLabels = (theme) => { + const label = + theme === "dark" ? "Switch to light mode" : "Switch to dark mode"; + document.querySelectorAll(".theme-toggle").forEach((button) => { + if (button instanceof HTMLElement) { + button.setAttribute("aria-label", label); + button.setAttribute("title", label); + } + }); + }; + + const applyTheme = (theme, persist = true) => { + document.documentElement.setAttribute("data-theme", theme); + updateThemeToggleLabels(theme); + + if (!persist) return; + + try { + localStorage.setItem("theme", theme); + } catch { + // Ignore read/write failures in strict privacy modes. + } + }; + + const initThemeToggle = () => { + applyTheme(getCurrentTheme(), false); + + document.addEventListener("click", (event) => { + const eventTarget = event.target; + if (!(eventTarget instanceof Element)) return; + + const toggleButton = eventTarget.closest(".theme-toggle"); + if (!(toggleButton instanceof HTMLElement)) return; + + const currentTheme = getCurrentTheme(); + const nextTheme = currentTheme === "dark" ? "light" : "dark"; + applyTheme(nextTheme); + }); + }; + + const loadAnalytics = () => { + if (document.querySelector('script[data-goatcounter-script="true"]')) + return; + + const script = document.createElement("script"); + script.src = "https://stats.wsap.dev/count.js"; + script.async = true; + script.defer = true; + script.setAttribute("data-goatcounter", "https://stats.wsap.dev/count"); + script.setAttribute("data-goatcounter-script", "true"); + document.head.appendChild(script); + }; + + const scheduleAnalytics = () => { + if ("requestIdleCallback" in window) { + window.requestIdleCallback(loadAnalytics, { timeout: 3000 }); + return; + } + window.setTimeout(loadAnalytics, 1500); + }; + + const init = () => { + initThemeToggle(); + + if (document.readyState === "complete") scheduleAnalytics(); + else window.addEventListener("load", scheduleAnalytics, { once: true }); + }; + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", init, { once: true }); + return; + } + + init(); +})(); |
