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

58 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,
setCookieChangeTheme;
setCookieChangeTheme = function ( themeName ) {
try {
window.mw.cookie.set( 'skin-citizen-theme', themeName );
} catch ( e ) {
}
window.switchTheme();
};
2021-01-09 22:12:17 +00:00
if ( typeof window.mw === 'undefined' ) {
return;
}
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' ) {
2021-01-09 22:12:17 +00:00
return;
}
2021-01-11 17:13:59 +00:00
try {
if ( window.mw.cookie.get( 'skin-citizen-theme-override' ) === '1' ) {
return;
}
} catch ( e ) {}
2021-01-09 22:12:17 +00:00
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
setCookieChangeTheme( theme );
2021-01-11 17:13:59 +00:00
prefersColorSchemeDarkQuery.addEventListener( 'change', function ( e ) {
setCookieChangeTheme( e.matches ? 'dark' : 'light' );
2021-01-11 17:13:59 +00:00
} );
2021-01-09 22:12:17 +00:00
}() );