mediawiki-skins-Citizen/resources/skins.citizen.scripts.theme/theme-switcher.js

59 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
* Citizen - Theme Switcher JS
* https://starcitizen.tools
*/
2021-01-09 22:12:17 +00:00
( function () {
2021-01-11 17:13:59 +00:00
var prefersColorSchemeDarkQuery,
userTheme,
theme,
setStorageChangeTheme;
setStorageChangeTheme = function ( themeName ) {
try {
window.localStorage.setItem( 'skin-citizen-theme', themeName );
} catch ( e ) {
}
window.switchTheme();
};
2021-01-09 22:12:17 +00:00
if ( typeof window.mw === 'undefined' ) {
return;
}
try {
if ( window.mw.cookie.get( 'skin-citizen-theme-override' ) === '1' ) {
return;
}
} catch ( e ) {}
2021-01-11 17:13:59 +00:00
theme = window.mw.config.get( 'wgCitizenThemeDefault' );
if ( theme === null ) {
theme = 'auto';
}
2021-01-09 22:12:17 +00:00
2021-01-11 17:13:59 +00:00
userTheme = window.mw.user.options.get( 'CitizenThemeUser' );
2021-01-09 22:12:17 +00:00
2021-01-11 17:13:59 +00:00
if ( userTheme !== null ) {
theme = userTheme;
}
2021-01-09 22:12:17 +00:00
2021-01-11 17:13:59 +00:00
if ( theme !== 'auto' ) {
setStorageChangeTheme( theme );
2021-01-09 22:12:17 +00:00
return;
}
2021-01-11 17:13:59 +00:00
prefersColorSchemeDarkQuery = window.matchMedia( '(prefers-color-scheme: dark)' );
theme = 'light';
2021-01-11 17:13:59 +00:00
if ( prefersColorSchemeDarkQuery.matches ) {
theme = 'dark';
}
2021-01-09 22:12:17 +00:00
setStorageChangeTheme( theme );
2021-01-11 17:13:59 +00:00
prefersColorSchemeDarkQuery.addEventListener( 'change', function ( e ) {
setStorageChangeTheme( e.matches ? 'dark' : 'light' );
2021-01-11 17:13:59 +00:00
} );
2021-01-09 22:12:17 +00:00
}() );