|
|
|
const applyAutoTheme = () => {
|
|
|
|
const prefersLight = window.matchMedia("(prefers-color-scheme: light)").matches;
|
|
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
|
|
|
|
if (prefersLight) {
|
|
document.body.setAttribute("data-md-color-scheme", "default");
|
|
document.body.setAttribute("data-md-color-primary", "indigo");
|
|
} else if (prefersDark) {
|
|
document.body.setAttribute("data-md-color-scheme", "slate");
|
|
document.body.setAttribute("data-md-color-primary", "black");
|
|
}
|
|
};
|
|
|
|
|
|
function checkAutoTheme() {
|
|
|
|
const supportedLangCodes = ["en", "zh", "ko", "ja", "ru", "de", "fr", "es", "pt", "it", "tr", "vi", "nl"];
|
|
|
|
const path = window.location.pathname;
|
|
|
|
const langCode = path.split("/")[1];
|
|
|
|
const isValidLangCode = supportedLangCodes.includes(langCode);
|
|
|
|
const localStorageKey = isValidLangCode ? `/${langCode}/.__palette` : "/.__palette";
|
|
|
|
const palette = localStorage.getItem(localStorageKey);
|
|
if (palette) {
|
|
|
|
const paletteObj = JSON.parse(palette);
|
|
if (paletteObj && paletteObj.index === 0) {
|
|
applyAutoTheme();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
checkAutoTheme();
|
|
|
|
|
|
window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", checkAutoTheme);
|
|
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", checkAutoTheme);
|
|
|
|
|
|
|
|
|
|
|
|
var autoThemeInput = document.getElementById("__palette_1");
|
|
if (autoThemeInput) {
|
|
|
|
autoThemeInput.addEventListener("click", function () {
|
|
|
|
if (autoThemeInput.checked) {
|
|
|
|
setTimeout(applyAutoTheme);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
window.onhashchange = function() {
|
|
window.parent.postMessage({
|
|
type: 'navigation',
|
|
hash: window.location.pathname + window.location.search + window.location.hash
|
|
}, '*');
|
|
};
|
|
|