bug fix: DOMContentLoaded

This commit is contained in:
Junyi Hou 2024-03-13 16:14:18 +08:00
parent e61809d59a
commit 4cba618ce1

View File

@ -26,12 +26,12 @@
document.addEventListener('DOMContentLoaded', function () {
switchButton = document.querySelector('.theme-switcher')
currentTheme = detectCurrentScheme()
if (currentTheme == 'dark') {
document.documentElement.setAttribute('data-theme', 'dark')
}
if (currentTheme == 'auto') {
if (currentTheme === 'auto') {
autoChangeScheme(autoDefinedScheme);
autoDefinedScheme.addListener(autoChangeScheme);
} else {
document.documentElement.setAttribute('data-theme', currentTheme)
}
if (switchButton) {
@ -48,27 +48,13 @@
if (defaultTheme) {
return defaultTheme
}
if (!window.matchMedia) {
return 'light'
}
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
return 'dark'
}
return 'light'
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
function switchTheme(e) {
if (currentTheme == 'dark') {
if (localStorage !== null)
localStorage.setItem(STORAGE_KEY, 'light')
document.documentElement.setAttribute('data-theme', 'light')
currentTheme = 'light'
} else {
if (localStorage !== null)
localStorage.setItem(STORAGE_KEY, 'dark')
document.documentElement.setAttribute('data-theme', 'dark')
currentTheme = 'dark'
}
currentTheme = (currentTheme === 'dark') ? 'light' : 'dark';
if (localStorage) localStorage.setItem(STORAGE_KEY, currentTheme);
document.documentElement.setAttribute('data-theme', currentTheme);
}
function showContent() {