23 lines
853 B
JavaScript
23 lines
853 B
JavaScript
(function () {
|
|
const KEY = "chessstars-theme";
|
|
function apply(theme) {
|
|
document.documentElement.setAttribute("data-theme", theme);
|
|
}
|
|
const saved = localStorage.getItem(KEY) ||
|
|
(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light");
|
|
apply(saved);
|
|
|
|
window.addEventListener("DOMContentLoaded", function () {
|
|
const btn = document.getElementById("theme-toggle");
|
|
if (!btn) return;
|
|
btn.textContent = saved === "dark" ? "White theme" : "Black theme";
|
|
btn.addEventListener("click", function () {
|
|
const current = document.documentElement.getAttribute("data-theme");
|
|
const next = current === "dark" ? "light" : "dark";
|
|
apply(next);
|
|
localStorage.setItem(KEY, next);
|
|
btn.textContent = next === "dark" ? "White theme" : "Black theme";
|
|
});
|
|
});
|
|
})();
|