MediaWiki:Common.js

From Taylor Wiki
Revision as of 22:30, 19 September 2026 by Admin (talk | contribs)

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.
/* Dynamic session breadcrumb trail with clean spacing */
( function () {
    var conf = mw.config.get([
        'wgAction',
        'wgNamespaceNumber',
        'wgPageName',
        'wgTitle',
        'wgIsMainPage',
        'wgMainPageTitle'
    ]);

    // Only run on standard page views (ignore edit forms, history, special pages)
    if ( conf.wgAction !== 'view' || conf.wgNamespaceNumber < 0 ) {
        return;
    }

    var STORAGE_KEY = 'gewame_wiki_crumbs';
    var MAX_CRUMBS = 5;

    var mainPageTitle = conf.wgMainPageTitle || 'Main Page';
    var homeUrl = mw.util.getUrl( mainPageTitle );
    var isHome = conf.wgIsMainPage || ( conf.wgPageName === mainPageTitle.replace( / /g, '_' ) );

    // If currently on Main Page, reset history and don't render a bar
    if ( isHome ) {
        try {
            sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Main Page', url: homeUrl } ] ) );
        } catch ( e ) {}
        return;
    }

    var currentTitle = conf.wgTitle.replace( /_/g, ' ' );
    var currentUrl = mw.util.getUrl( conf.wgPageName );

    var history = [];
    try {
        var raw = sessionStorage.getItem( STORAGE_KEY );
        if ( raw ) {
            history = JSON.parse( raw );
        }
    } catch ( e ) {
        history = [];
    }

    // Always ensure 'Main Page' is the first crumb
    if ( !history.length || history[0].title !== 'Main Page' ) {
        history = [ { title: 'Main Page', url: homeUrl } ];
    }

    // If current page is already in trail, rewind back to it (prevents loops)
    var existingIndex = -1;
    for ( var i = 0; i < history.length; i++ ) {
        if ( history[i].url === currentUrl ) {
            existingIndex = i;
            break;
        }
    }

    if ( existingIndex !== -1 ) {
        history = history.slice( 0, existingIndex + 1 );
    } else {
        history.push( { title: currentTitle, url: currentUrl } );
        if ( history.length > MAX_CRUMBS ) {
            history.splice( 1, 1 );
        }
    }

    try {
        sessionStorage.setItem( STORAGE_KEY, JSON.stringify( history ) );
    } catch ( e ) {}

    // Build breadcrumb bar (no "Trail:" label)
    var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb navigation"></div>' );

    for ( var j = 0; j < history.length; j++ ) {
        var isCurrent = ( j === history.length - 1 );
        if ( j > 0 ) {
            $nav.append( $( '<span>' ).addClass( 'crumb-sep' ).text( '›' ) );
        }

        if ( isCurrent ) {
            $nav.append( $( '<span>' ).addClass( 'crumb-current' ).text( history[j].title ) );
        } else {
            var $link = $( '<a>' )
                .addClass( 'crumb-link' )
                .attr( 'href', history[j].url )
                .text( history[j].title );
            $nav.append( $link );
        }
    }

    // Inject directly above page title in Timeless
    $( function () {
        var $heading = $( '#firstHeading' );
        if ( $heading.length ) {
            $nav.insertBefore( $heading );
        } else {
            $( '#mw-content-text' ).prepend( $nav );
        }
    } );
}() );