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: Difference between revisions

MediaWiki interface page
No edit summary
SEO: backfill meta description + image alt attributes
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Site JS — loaded on every page (MediaWiki:Common.js). */


$(document).ready(function() {
/* SEO fixes (Lighthouse "meta description" + "image alt"). Citizen renders
    // Wait for collapsible elements to be initialized by MediaWiki
  og:description but not <meta name="description">, and MediaWiki renders inline
    mw.hook('wikipage.content').add(function() {
  images with no alt attribute, so we backfill both client-side. */
        var $sections = $('.accordion-section');
( 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 );
}
}


        // Initialize: disable links in collapsed sections
// 2. Give every image an alt attribute if it lacks one, derived from the
        $sections.each(function() {
//    wrapping link's title (page/script name) or the file name.
            if ($(this).hasClass('mw-collapsed')) {
document.querySelectorAll( 'img:not([alt])' ).forEach( function ( img ) {
                $(this).find('.mw-headline a').css('pointer-events', 'none');
var link = img.closest( 'a' );
            }
var alt = ( link && link.getAttribute( 'title' ) ) || '';
        });
if ( !alt ) {
 
var src = img.getAttribute( 'src' ) || '';
        // Handle clicks on headers
alt = decodeURIComponent( src.split( '/' ).pop() || '' )
        $sections.on('click', '.mw-headline', function(e) {
.replace( /^\d+px-/, '' )
            var $section = $(this).closest('.accordion-section');
.replace( /\.[a-z0-9]+$/i, '' )
            var isCollapsed = $section.hasClass('mw-collapsed');
.replace( /[_-]+/g, ' ' )
 
.trim();
            if (isCollapsed) {
}
                // Collapse all other sections
img.setAttribute( 'alt', alt );
                $sections.not($section).addClass('mw-collapsed');
} );
                // Expand this section
}() );
                $section.removeClass('mw-collapsed');
                // Enable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'auto');
                // Disable links in all other sections
                $sections.not($section).find('.mw-headline a').css('pointer-events', 'none');
            } else {
                // Collapse this section
                $section.addClass('mw-collapsed');
                // Disable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'none');
            }
 
            // Prevent link click from propagating if collapsed
            if (e.target.tagName === 'A' && $section.hasClass('mw-collapsed')) {
                e.preventDefault();
            }
        });
    });
});