MediaWiki:Common.js: Difference between revisions

From eMushpedia
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 41: Line 41:
}
}
return 'Home';
return 'Home';
}
function rewriteLink( a, url, title, text ) {
if ( !a ) {
return;
}
a.href = url;
a.title = title;
if ( text ) {
const span = a.querySelector( 'span' );
if ( span ) {
span.textContent = text;
} else {
a.textContent = text;
}
}
}
}


Line 48: Line 66:
const url = mw.util.getUrl( target, { uselang: lang } );
const url = mw.util.getUrl( target, { uselang: lang } );


// Logo
// Logo links
document.querySelectorAll( 'a.mw-wiki-logo, a.mw-logo' ).forEach( function ( a ) {
document.querySelectorAll( 'a.mw-wiki-logo, a.mw-logo' ).forEach( function ( a ) {
a.href = url;
rewriteLink( a, url, target, null );
a.title = target;
} );
 
// Sidebar custom main page item
document.querySelectorAll( '#n-lang-mainpage a' ).forEach( function ( a ) {
rewriteLink( a, url, target, target );
} );
} );


// Sidebar main page links
// Fallbacks for default main page links if they still exist somewhere
document.querySelectorAll( '#n-n-lang-mainpage a, a[accesskey="z"]' ).forEach( function ( a ) {
document.querySelectorAll( '#n-mainpage-description a, a[accesskey="z"]' ).forEach( function ( a ) {
a.href = url;
rewriteLink( a, url, target, target );
a.title = target;
} );
} );
}
}


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

Latest revision as of 03:45, 9 April 2026

// 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 rewriteLink( a, url, title, text ) {
		if ( !a ) {
			return;
		}

		a.href = url;
		a.title = title;

		if ( text ) {
			const span = a.querySelector( 'span' );
			if ( span ) {
				span.textContent = text;
			} else {
				a.textContent = text;
			}
		}
	}

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

		// Logo links
		document.querySelectorAll( 'a.mw-wiki-logo, a.mw-logo' ).forEach( function ( a ) {
			rewriteLink( a, url, target, null );
		} );

		// Sidebar custom main page item
		document.querySelectorAll( '#n-lang-mainpage a' ).forEach( function ( a ) {
			rewriteLink( a, url, target, target );
		} );

		// Fallbacks for default main page links if they still exist somewhere
		document.querySelectorAll( '#n-mainpage-description a, a[accesskey="z"]' ).forEach( function ( a ) {
			rewriteLink( a, url, target, target );
		} );
	}

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