2021-04-27 16:57:54 +00:00
|
|
|
/*
|
2022-04-26 00:29:29 +00:00
|
|
|
* Citizen
|
2021-04-27 16:57:54 +00:00
|
|
|
*
|
2023-01-21 01:18:58 +00:00
|
|
|
* Inline script used in includes/Hooks/SkinHooks.php
|
2021-04-27 16:57:54 +00:00
|
|
|
*/
|
2024-04-25 00:23:45 +00:00
|
|
|
const LEGACY_PREFIX = 'skin-citizen-';
|
2022-04-26 00:29:29 +00:00
|
|
|
|
2024-04-25 00:23:45 +00:00
|
|
|
window.applyPref = () => {
|
2022-05-20 21:33:10 +00:00
|
|
|
const getStorage = ( key ) => {
|
|
|
|
return window.localStorage.getItem( key );
|
2022-04-26 00:29:29 +00:00
|
|
|
};
|
|
|
|
|
2022-05-20 21:33:10 +00:00
|
|
|
const apply = () => {
|
|
|
|
const cssProps = {
|
|
|
|
fontsize: 'font-size',
|
|
|
|
pagewidth: '--width-layout',
|
|
|
|
lineheight: '--line-height'
|
|
|
|
};
|
2021-04-27 16:57:54 +00:00
|
|
|
|
2022-05-20 21:33:10 +00:00
|
|
|
const injectStyles = ( css ) => {
|
|
|
|
const styleId = 'citizen-style';
|
|
|
|
let style = document.getElementById( styleId );
|
2022-04-26 00:29:29 +00:00
|
|
|
|
2022-05-20 21:33:10 +00:00
|
|
|
if ( style === null ) {
|
|
|
|
style = document.createElement( 'style' );
|
|
|
|
style.setAttribute( 'id', styleId );
|
|
|
|
document.head.appendChild( style );
|
|
|
|
}
|
2024-01-27 02:51:04 +00:00
|
|
|
style.textContent = `:root{${ css }}`;
|
2022-05-20 21:33:10 +00:00
|
|
|
};
|
2022-05-13 04:17:27 +00:00
|
|
|
|
2022-05-20 21:33:10 +00:00
|
|
|
try {
|
|
|
|
let cssDeclaration = '';
|
|
|
|
// Apply pref by adding CSS to root
|
|
|
|
for ( const [ key, property ] of Object.entries( cssProps ) ) {
|
2024-04-25 00:23:45 +00:00
|
|
|
const value = getStorage( LEGACY_PREFIX + key );
|
2022-05-20 21:33:10 +00:00
|
|
|
|
|
|
|
if ( value !== null ) {
|
2024-01-27 02:51:04 +00:00
|
|
|
cssDeclaration += `${ property }:${ value };`;
|
2022-05-20 21:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cssDeclaration ) {
|
|
|
|
injectStyles( cssDeclaration );
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
2021-08-05 15:24:52 +00:00
|
|
|
}
|
2022-05-20 21:33:10 +00:00
|
|
|
};
|
2021-04-27 16:57:54 +00:00
|
|
|
|
2024-04-25 00:23:45 +00:00
|
|
|
apply();
|
|
|
|
};
|
2022-05-20 21:33:10 +00:00
|
|
|
|
2024-04-25 00:23:45 +00:00
|
|
|
/**
|
|
|
|
* Backported from MW 1.42
|
|
|
|
* Modified to use localStorage only
|
|
|
|
*/
|
|
|
|
window.clientPrefs = () => {
|
|
|
|
let className = document.documentElement.className;
|
|
|
|
const storage = localStorage.getItem( 'mwclientpreferences' );
|
|
|
|
if ( storage ) {
|
|
|
|
// TODO: Just use array for localStorage
|
|
|
|
storage.split( '%2C' ).forEach( function ( pref ) {
|
|
|
|
className = className.replace(
|
|
|
|
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
|
|
new RegExp( '(^| )' + pref.replace( /-clientpref-\w+$|[^\w-]+/g, '' ) + '-clientpref-\\w+( |$)' ),
|
|
|
|
'$1' + pref + '$2'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Legacy support
|
|
|
|
if ( pref.startsWith( 'skin-theme-clientpref-' ) ) {
|
|
|
|
const CLIENTPREFS_THEME_MAP = {
|
|
|
|
os: 'auto',
|
|
|
|
day: 'light',
|
|
|
|
night: 'dark'
|
|
|
|
};
|
|
|
|
const matchedKey = CLIENTPREFS_THEME_MAP[ pref.replace( 'skin-theme-clientpref-', '' ) ];
|
|
|
|
if ( matchedKey ) {
|
|
|
|
// eslint-disable-next-line max-len, es-x/no-object-values
|
|
|
|
const classesToRemove = Object.values( CLIENTPREFS_THEME_MAP ).map( ( theme ) => LEGACY_PREFIX + theme );
|
|
|
|
className = className.replace(
|
|
|
|
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
|
|
new RegExp( classesToRemove.join( '|' ), 'g' ),
|
|
|
|
''
|
|
|
|
);
|
|
|
|
className += ` ${ LEGACY_PREFIX }${ matchedKey }`;
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 21:33:10 +00:00
|
|
|
} );
|
2024-04-25 00:23:45 +00:00
|
|
|
document.documentElement.className = className;
|
2022-04-19 22:17:53 +00:00
|
|
|
}
|
2022-05-20 21:33:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
( () => {
|
|
|
|
window.applyPref();
|
2024-04-25 00:23:45 +00:00
|
|
|
window.clientPrefs();
|
2022-04-26 00:29:29 +00:00
|
|
|
} )();
|