mirror of
https://github.com/MeiK2333/github-style.git
synced 2025-10-07 10:04:04 +00:00
26 lines
622 B
JavaScript
26 lines
622 B
JavaScript
function switchTheme() {
|
|
const currentStyle = currentTheme();
|
|
|
|
if (currentStyle == 'light') {
|
|
setTheme('dark');
|
|
}
|
|
else {
|
|
setTheme('light');
|
|
}
|
|
}
|
|
|
|
function setTheme(style) {
|
|
console.log(`set theme ${style}`);
|
|
document.documentElement.setAttribute('data-theme', style);
|
|
localStorage.setItem('data-theme', style);
|
|
}
|
|
|
|
function currentTheme() {
|
|
const localStyle = localStorage.getItem('data-theme');
|
|
const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
return localStyle || systemStyle;
|
|
}
|
|
|
|
(() => {
|
|
setTheme(currentTheme());
|
|
})(); |