2022-02-05 08:19:47 +00:00
|
|
|
const sitePreference = document.documentElement.getAttribute("data-default-appearance");
|
2022-01-27 04:26:18 +00:00
|
|
|
const userPreference = localStorage.getItem("appearance");
|
|
|
|
|
2022-11-21 22:55:20 +00:00
|
|
|
function getCSSValue(varName) {
|
|
|
|
var cssValue = window.getComputedStyle(document.documentElement).getPropertyValue(varName);
|
|
|
|
return "rgb(" + cssValue.replace(/\s+/g, "") + ")";
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:35:17 +00:00
|
|
|
function setThemeColor() {
|
2022-11-21 22:55:20 +00:00
|
|
|
var metaThemeColor = document.querySelector("meta[name=theme-color]");
|
2024-01-19 19:02:44 +00:00
|
|
|
var metaAppleMobileWebAppStatusBarStyle = document.querySelector("meta[name=apple-mobile-web-app-status-bar-style]");
|
2022-11-21 22:35:17 +00:00
|
|
|
document.documentElement.classList.contains("dark")
|
2022-11-21 22:55:20 +00:00
|
|
|
? metaThemeColor.setAttribute("content", getCSSValue("--color-neutral-800"))
|
|
|
|
: metaThemeColor.setAttribute("content", getCSSValue("--color-neutral"));
|
2024-01-19 19:02:44 +00:00
|
|
|
document.documentElement.classList.contains("dark")
|
|
|
|
? metaAppleMobileWebAppStatusBarStyle.setAttribute("content", getCSSValue("--color-neutral-800"))
|
|
|
|
: metaAppleMobileWebAppStatusBarStyle.setAttribute("content", getCSSValue("--color-neutral"));
|
2022-11-21 22:55:20 +00:00
|
|
|
return true;
|
2022-11-21 22:35:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 23:40:34 +00:00
|
|
|
if ((sitePreference === "dark" && userPreference === null) || userPreference === "dark") {
|
2022-01-27 04:26:18 +00:00
|
|
|
document.documentElement.classList.add("dark");
|
2022-11-21 22:35:17 +00:00
|
|
|
setThemeColor();
|
2022-01-27 04:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
|
2022-03-13 23:40:34 +00:00
|
|
|
if (
|
|
|
|
window.matchMedia &&
|
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches &&
|
|
|
|
userPreference !== "light"
|
|
|
|
) {
|
|
|
|
document.documentElement.classList.add("dark");
|
2022-11-21 22:35:17 +00:00
|
|
|
setThemeColor();
|
2022-03-13 23:40:34 +00:00
|
|
|
}
|
2022-01-27 04:26:18 +00:00
|
|
|
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (event) => {
|
|
|
|
if (event.matches) {
|
|
|
|
document.documentElement.classList.add("dark");
|
|
|
|
} else {
|
|
|
|
document.documentElement.classList.remove("dark");
|
|
|
|
}
|
2022-11-21 22:35:17 +00:00
|
|
|
setThemeColor();
|
2022-01-27 04:26:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", (event) => {
|
2023-01-17 01:58:04 +00:00
|
|
|
var switchers = document.querySelectorAll("[id^='appearance-switcher']");
|
|
|
|
switchers.forEach((switcher) => {
|
2022-02-05 08:19:47 +00:00
|
|
|
switcher.addEventListener("click", () => {
|
|
|
|
document.documentElement.classList.toggle("dark");
|
2022-11-21 22:35:17 +00:00
|
|
|
setThemeColor();
|
2022-02-05 08:19:47 +00:00
|
|
|
localStorage.setItem(
|
|
|
|
"appearance",
|
|
|
|
document.documentElement.classList.contains("dark") ? "dark" : "light"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
switcher.addEventListener("contextmenu", (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
localStorage.removeItem("appearance");
|
|
|
|
});
|
2023-01-17 01:58:04 +00:00
|
|
|
});
|
2022-01-27 04:26:18 +00:00
|
|
|
});
|