mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/reportIfNightModeWasDisabledOnPage.js
Moh'd Khier Abualruz 5d6b866890 Rename the skin night mode classes to more readable classes
The classes:
- skin-night-mode-clientpref-0
- skin-night-mode-clientpref-1
- skin-night-mode-clientpref-2
is being replaced with
- skin-theme-clientpref-day
- skin-theme-clientpref-night
- skin-theme-clientpref-os
- Moved $forceNightMode to be a text parameter (dat|night|os)
- Keep adding the old classes to the html element, to give the ability of gradual deployment

The preference is renamed from minerva-night-mode to minerva-theme (a follow up to consider
migrating existing values will follow).

The query string minervanightmode continues to behave the same but now
accepts other values such as day, night and os.

Bug: T359983
Change-Id: Ia253de68f94236e7fe2219b736dd6084c64ce838
2024-03-19 23:12:59 +00:00

40 lines
1.1 KiB
JavaScript

/**
* @return {boolean}
*/
function reportDisabled() {
mw.notify( mw.msg( 'skin-minerva-night-mode-unavailable' ) );
return true;
}
/**
* @param {Document} doc
* @return {boolean} whether it was reported as disabled.
* @ignore
*/
function reportIfNightModeWasDisabledOnPage( doc ) {
if ( !doc.classList.contains( 'skin-night-mode-page-disabled' ) ) {
return false;
}
// Cast to string.
let userExpectedNightMode = `${ mw.user.options.get( 'minerva-theme' ) }`;
if ( !mw.user.isNamed() ) {
// bit more convoulated here and will break with upstream changes...
// this is protected by an integration test in integration.test.js
const cookieValue = mw.cookie.get( 'mwclientpreferences' ) || '';
const match = cookieValue.match( /skin-theme-clientpref-(\S+)/ );
if ( match ) {
// we found something in the cookie.
userExpectedNightMode = match[ 1 ];
}
}
if ( userExpectedNightMode === 'night' ) {
return reportDisabled();
} else if ( userExpectedNightMode === 'os' && matchMedia( '( prefers-color-scheme: dark )' ).matches ) {
return reportDisabled();
} else {
return false;
}
}
module.exports = reportIfNightModeWasDisabledOnPage;