More actions
No edit summary Tag: Reverted |
SEO: backfill meta description + image alt attributes |
||
| (26 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
/* | /* 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 ); | |||
} ); | |||
}() ); | |||
Latest revision as of 18:34, 7 June 2026
/* 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 );
} );
}() );