MediaWiki:Mobile.js: Difference between revisions

Content deleted Content added
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
Line 156:
}, { passive: true });
})();
 
/* ======================================================= */
/* ADD 'BIZ BOOKS' TO MOBILE SIDEBAR (UNIVERSAL METHOD) */
/* ======================================================= */
 
mw.loader.using( ['mediawiki.util', 'jquery'], function () {
$( function () {
var linkId = 'mobile-nav-biz-books';
var linkHref = '/wiki/Biz/Books';
var linkText = 'Biz Books';
 
// Helper function to find the correct menu list
function findTargetMenu() {
// 1. Look for the standard Minerva menu class
var $lists = $( '.menu ul, .navigation-drawer ul' );
 
// 2. Filter to find the list that actually contains the "Home" or "Random" links
// This prevents us from injecting into the wrong list (like the user settings)
var $target = $lists.filter(function() {
// Look for links to Main_Page or Random inside this UL
return $(this).find('a[href*="Main_Page"], a[href*="Random"]').length > 0;
});
 
// If found, return the first match. If not, fallback to the first UL in the drawer.
return $target.length ? $target.first() : $lists.first();
}
 
function injectLink() {
var $menu = findTargetMenu();
// If no menu is found, or if our link already exists, stop.
if ( $menu.length === 0 || $menu.find( '#' + linkId ).length > 0 ) return;
 
console.log( 'BizSlash: Valid menu found. Injecting...' );
 
var $newItem = $( '<li>' )
.addClass( 'mw-ui-icon-extra' )
.attr( 'id', linkId );
 
var $newLink = $( '<a>' )
.attr( 'href', linkHref )
// We use 'mw-ui-icon-minerva-list' as a fallback icon because it exists in all versions
.addClass( 'mw-ui-icon mw-ui-icon-before mw-ui-icon-minerva-list' )
.text( linkText )
// Force visibility in case classes fail
.css({
'display': 'block',
'font-weight': 'bold'
});
 
$newItem.append( $newLink );
// PREPEND puts it at the TOP of the menu so you can't miss it
$menu.prepend( $newItem );
}
 
// 1. Run on load
injectLink();
 
// 2. Run whenever the drawer is opened (Click listener)
// We listen on the document body for clicks on the hamburger icon
$( 'body' ).on( 'click', '.mw-ui-icon-minerva-mainmenu, #mw-mf-main-menu-button', function() {
// Retry multiple times as the menu slides in
var count = 0;
var interval = setInterval(function() {
injectLink();
count++;
if (count > 20) clearInterval(interval); // Stop after 2 seconds
}, 100);
});
 
// 3. MutationObserver (The Backup)
// Watch for the drawer appearing in the DOM
var observer = new MutationObserver(function(mutations) {
injectLink();
});
// Start observing the body for added nodes
observer.observe(document.body, { childList: true, subtree: true });
} );
} );