const themeButton = document.getElementById('darkmodetoggle'); const theme = localStorage.getItem("theme"); var isDark = false; if (theme) { setTheme(theme === "dark"); } else if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) { setTheme(true); } function setTheme(dark) { if (dark) { document.documentElement.classList.add("dark-mode"); themeButton.className = "fa fa-sun-o"; localStorage.setItem("theme", "dark"); } else { document.documentElement.classList.remove("dark-mode"); themeButton.className = "fa fa-moon-o"; localStorage.setItem("theme", "light"); } isDark = dark; } function switchTheme() { setTheme(!isDark); }