Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js

MediaWiki interface page
Revision as of 18:34, 7 June 2026 by Veza (talk | contribs) (SEO: backfill meta description + image alt attributes)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
/* Site JS — loaded on every page (MediaWiki:Common.js). */

/* SEO fixes (Lighthouse "meta description" + "image alt"). Citizen renders
   og:description but not <meta name="description">, and MediaWiki renders inline
   images with no alt attribute, so we backfill both client-side. */
( function () {
	// 1. Mirror og:description into a real meta description if missing.
	if ( !document.querySelector( 'meta[name="description"]' ) ) {
		var og = document.querySelector( 'meta[property="og:description"]' );
		if ( og && og.content ) {
			var m = document.createElement( 'meta' );
			m.setAttribute( 'name', 'description' );
			m.setAttribute( 'content', og.content );
			document.head.appendChild( m );
		}
	}

	// 2. Give every image an alt attribute if it lacks one, derived from the
	//    wrapping link's title (page/script name) or the file name.
	document.querySelectorAll( 'img:not([alt])' ).forEach( function ( img ) {
		var link = img.closest( 'a' );
		var alt = ( link && link.getAttribute( 'title' ) ) || '';
		if ( !alt ) {
			var src = img.getAttribute( 'src' ) || '';
			alt = decodeURIComponent( src.split( '/' ).pop() || '' )
				.replace( /^\d+px-/, '' )
				.replace( /\.[a-z0-9]+$/i, '' )
				.replace( /[_-]+/g, ' ' )
				.trim();
		}
		img.setAttribute( 'alt', alt );
	} );
}() );