MediaWiki:Common.js

From eMushpedia
Revision as of 03:39, 9 April 2026 by Admin (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Override the page the user is redirected to when clicking the logo
// based only on ?uselang=fr|en|es in the URL, then remember it.
(function () {
	const ALLOWED_LANGS = [ 'fr', 'en', 'es' ];
	const STORAGE_KEY = 'emushpedia_lang';

	function getLangFromUrl() {
		const params = new URLSearchParams( window.location.search );
		const uselang = params.get( 'uselang' );
		return ALLOWED_LANGS.includes( uselang ) ? uselang : null;
	}

	function getStoredLang() {
		const stored = localStorage.getItem( STORAGE_KEY );
		return ALLOWED_LANGS.includes( stored ) ? stored : null;
	}

	function saveLang( lang ) {
		if ( ALLOWED_LANGS.includes( lang ) ) {
			localStorage.setItem( STORAGE_KEY, lang );
		}
	}

	function getLang() {
		const urlLang = getLangFromUrl();

		if ( urlLang ) {
			saveLang( urlLang );
			return urlLang;
		}

		return getStoredLang() || 'en';
	}

	function getHomeTitle( lang ) {
		if ( lang === 'fr' ) {
			return 'Accueil';
		}
		if ( lang === 'es' ) {
			return 'Inicio';
		}
		return 'Home';
	}

	function rewriteMainLinks() {
		const lang = getLang();
		const target = getHomeTitle( lang );
		const url = mw.util.getUrl( target, { uselang: lang } );

		// Logo
		document.querySelectorAll( 'a.mw-wiki-logo, a.mw-logo' ).forEach( function ( a ) {
			a.href = url;
			a.title = target;
		} );

		// Sidebar main page links
		document.querySelectorAll( '#n-mainpage-description a, a[accesskey="z"]' ).forEach( function ( a ) {
			a.href = url;
			a.title = target;
		} );
	}

	mw.loader.using( [ 'mediawiki.util' ] ).then( rewriteMainLinks );
}());