More actions
No edit summary |
SEO: backfill meta description + image alt attributes |
||
| (9 intermediate revisions by one other user 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 ); | |||
} ); | |||
}() ); | |||