mirror of
https://github.com/StarCitizenTools/mediawiki-skins-Citizen.git
synced 2024-11-17 19:32:00 +00:00
b4488f8ddb
* refactor: Fix switching theme * ci: lint code to MediaWiki standards Check commit and GitHub actions for more details Co-authored-by: github-actions <github-actions@users.noreply.github.com>
58 lines
1.1 KiB
JavaScript
58 lines
1.1 KiB
JavaScript
/*
|
|
* Citizen - Theme Switcher JS
|
|
* https://starcitizen.tools
|
|
*/
|
|
|
|
( function () {
|
|
var prefersColorSchemeDarkQuery,
|
|
userTheme,
|
|
theme,
|
|
setCookieChangeTheme;
|
|
|
|
setCookieChangeTheme = function ( themeName ) {
|
|
try {
|
|
window.mw.cookie.set( 'skin-citizen-theme', themeName );
|
|
} catch ( e ) {
|
|
}
|
|
|
|
window.switchTheme();
|
|
};
|
|
|
|
if ( typeof window.mw === 'undefined' ) {
|
|
return;
|
|
}
|
|
|
|
theme = window.mw.config.get( 'wgCitizenThemeDefault' );
|
|
if ( theme === null ) {
|
|
theme = 'auto';
|
|
}
|
|
|
|
userTheme = window.mw.user.options.get( 'CitizenThemeUser' );
|
|
|
|
if ( userTheme !== null ) {
|
|
theme = userTheme;
|
|
}
|
|
|
|
if ( theme !== 'auto' ) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if ( window.mw.cookie.get( 'skin-citizen-theme-override' ) === '1' ) {
|
|
return;
|
|
}
|
|
} catch ( e ) {}
|
|
|
|
prefersColorSchemeDarkQuery = window.matchMedia( '(prefers-color-scheme: dark)' );
|
|
theme = 'light';
|
|
if ( prefersColorSchemeDarkQuery.matches ) {
|
|
theme = 'dark';
|
|
}
|
|
|
|
setCookieChangeTheme( theme );
|
|
|
|
prefersColorSchemeDarkQuery.addEventListener( 'change', function ( e ) {
|
|
setCookieChangeTheme( e.matches ? 'dark' : 'light' );
|
|
} );
|
|
}() );
|