MediaWiki:Common.js: Difference between revisions
From Taylor Wiki
Created page with "→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';..." |
No edit summary |
||
| Line 1: | Line 1: | ||
/* Dynamic session breadcrumb trail with automatic Home anchor */ | |||
/* Dynamic session breadcrumb trail | |||
( function () { | ( function () { | ||
var conf = mw.config.get([ | |||
var conf = mw.config.get([ 'wgAction', 'wgNamespaceNumber', 'wgPageName', 'wgTitle' ]); | '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 ) { | if ( conf.wgAction !== 'view' || conf.wgNamespaceNumber < 0 ) { | ||
return; | return; | ||
| Line 10: | Line 17: | ||
var STORAGE_KEY = 'gewame_wiki_crumbs'; | var STORAGE_KEY = 'gewame_wiki_crumbs'; | ||
var MAX_CRUMBS = 5; | 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 Home, reset history to just Home and don't render a trail | |||
if ( isHome ) { | |||
try { | |||
sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Home', url: homeUrl } ] ) ); | |||
} catch ( e ) {} | |||
return; | |||
} | |||
var currentTitle = conf.wgTitle.replace( /_/g, ' ' ); | var currentTitle = conf.wgTitle.replace( /_/g, ' ' ); | ||
| Line 24: | Line 43: | ||
} | } | ||
// If current page is already in the trail, trim back to it | // Always ensure 'Home' is the very first crumb | ||
if ( !history.length || history[0].title !== 'Home' ) { | |||
history = [ { title: 'Home', url: homeUrl } ]; | |||
} | |||
// If current page is already in the trail, trim back to it (prevents loops) | |||
var existingIndex = -1; | var existingIndex = -1; | ||
for ( var i = 0; i < history.length; i++ ) { | for ( var i = 0; i < history.length; i++ ) { | ||
| Line 37: | Line 61: | ||
} else { | } else { | ||
history.push( { title: currentTitle, url: currentUrl } ); | history.push( { title: currentTitle, url: currentUrl } ); | ||
// Keep within limit, preserving 'Home' at index 0 | |||
if ( history.length > MAX_CRUMBS ) { | if ( history.length > MAX_CRUMBS ) { | ||
history. | history.splice( 1, 1 ); | ||
} | } | ||
} | } | ||
| Line 46: | Line 71: | ||
} catch ( e ) {} | } catch ( e ) {} | ||
// | // Build the breadcrumb bar | ||
var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb Trail"></div>' ); | |||
var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Trail"></div>' ); | |||
$nav.append( '<span class="crumb-label">Trail:</span> ' ); | $nav.append( '<span class="crumb-label">Trail:</span> ' ); | ||
| Line 72: | Line 92: | ||
} | } | ||
// Inject | // Inject above page title in Timeless | ||
$( function () { | $( function () { | ||
var $heading = $( '#firstHeading' ); | var $heading = $( '#firstHeading' ); | ||
Revision as of 22:28, 19 September 2026
/* Dynamic session breadcrumb trail with automatic Home anchor */
( 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 Home, reset history to just Home and don't render a trail
if ( isHome ) {
try {
sessionStorage.setItem( STORAGE_KEY, JSON.stringify( [ { title: 'Home', 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 'Home' is the very first crumb
if ( !history.length || history[0].title !== 'Home' ) {
history = [ { title: 'Home', url: homeUrl } ];
}
// If current page is already in the trail, trim 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 } );
// Keep within limit, preserving 'Home' at index 0
if ( history.length > MAX_CRUMBS ) {
history.splice( 1, 1 );
}
}
try {
sessionStorage.setItem( STORAGE_KEY, JSON.stringify( history ) );
} catch ( e ) {}
// Build the breadcrumb bar
var $nav = $( '<div class="mw-dynamic-breadcrumbs" aria-label="Breadcrumb 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 above page title in Timeless
$( function () {
var $heading = $( '#firstHeading' );
if ( $heading.length ) {
$nav.insertBefore( $heading );
} else {
$( '#mw-content-text' ).prepend( $nav );
}
} );
}() );