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

54 lines
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;
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)' );
if ( prefersColorSchemeDarkQuery.matches ) {
theme = 'dark';
}
2021-01-09 22:12:17 +00:00
2021-01-11 17:13:59 +00:00
prefersColorSchemeDarkQuery.addEventListener( 'change', function ( e ) {
if ( e.matches ) {
theme = 'dark';
2021-01-09 22:12:17 +00:00
} else {
2021-01-11 17:13:59 +00:00
theme = 'light';
2021-01-09 22:12:17 +00:00
}
2021-01-11 17:13:59 +00:00
} );
2021-01-09 22:12:17 +00:00
try {
2021-01-11 17:13:59 +00:00
window.mw.cookie.set( 'skin-citizen-theme', null );
window.mw.cookie.set( 'skin-citizen-theme', theme );
} catch ( e ) {
2021-01-09 22:12:17 +00:00
}
}() );