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

72 lines
1.6 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,
hasCookieConscent,
cookieOptions;
setCookieChangeTheme = function ( themeName ) {
try {
cookieOptions = {};
// The cookie is only used on the site
cookieOptions.sameSite = 'Strict';
// Check if Extension:CookieWarning is enabled
if ( window.mw.loader.getState( 'ext.CookieWarning' ) ) {
// Set the cookie as a session cookie if no conscent is given
hasCookieConscent = window.mw.cookie.get( 'cookiewarning_dismissed' ) === true ||
window.mw.user.options.get( 'cookiewarning_dismissed' ) === 1;
if ( !hasCookieConscent ) {
cookieOptions.expires = null;
}
}
window.mw.cookie.set( 'skin-citizen-theme', themeName, cookieOptions );
} 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
}() );