MediaWiki:Common.js
From Taylor Wiki
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.
/* Any JavaScript here will be loaded for all users on every page load. */
/* Dynamic session breadcrumb trail (Recent pages visited) */
( function () {
// Only track standard page views (ignore edits, diffs, history, and login/logout pages)
var conf = mw.config.get([ 'wgAction', 'wgNamespaceNumber', 'wgPageName', 'wgTitle' ]);
if ( conf.wgAction !== 'view' || conf.wgNamespaceNumber < 0 ) {
return;
}
var STORAGE_KEY = 'gewame_wiki_crumbs';
var MAX_CRUMBS = 5;
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 = [];
}
// If current page is already in the trail, trim back to it so we don't create 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.shift();
}
}
try {
sessionStorage.setItem( STORAGE_KEY, JSON.stringify( history ) );
} catch ( e ) {}
// Only render the bar if there is at least one previous page to navigate back to
if ( history.length <= 1 ) {
return;
}
// Build breadcrumb HTML
var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Trail"></div>' );
$nav.append( '<span class="crumb-label">Trail:</span> ' );
for ( var j = 0; j < history.length; j++ ) {
var isCurrent = ( j === history.length - 1 );
if ( j > 0 ) {
$nav.append( '<span class="crumb-sep">›</span>' );
}
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 the article heading in Timeless
$( function () {
var $heading = $( '#firstHeading' );
if ( $heading.length ) {
$nav.insertBefore( $heading );
} else {
$( '#mw-content-text' ).prepend( $nav );
}
} );
}() );