Jump to content

MediaWiki:Common.js

From Gyaanipedia

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. */

// Function to get a URL parameter's value
function getUrlParameter( name ) {
    name = name.replace( /[\[]/, '\\[' ).replace( /[\]]/, '\\]' );
    var regex = new RegExp( '[\\?&]' + name + '=([^&#]*)' );
    var results = regex.exec( location.search );
    return results === null ? '' : decodeURIComponent( results[1].replace( /\+/g, ' ' ) );
}

// Check if we are on Special:AllPages
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Allpages' ) {
    
    // Get the current 'from' parameter
    var currentFrom = getUrlParameter( 'from' );
    
    // Regular expression to check if the 'from' parameter is numeric (or empty, which defaults to numeric titles)
    var startsWithNumberRegex = /^\d/i; // Starts with a digit

    if ( currentFrom === '' || startsWithNumberRegex.test( currentFrom ) ) {
        // --- STEP 1: FORCE REDIRECT/RELOAD ---
        // If the 'from' parameter is empty or starts with a number,
        // we redirect the user to the same page but starting from 'A'
        
        var newUrl = window.location.href.split( /[?#]/ )[0]; // Base URL
        var query = 'from=A&namespace=' + mw.config.get( 'wgNamespaceNumber' ); // Force start at 'A'
        
        // Preserve any other parameters like the namespace
        var existingParams = window.location.search.substring(1).split('&');
        for ( var i = 0; i < existingParams.length; i++ ) {
            var pair = existingParams[i].split('=');
            var key = pair[0];
            if ( key !== 'from' && key !== 'namespace' && key !== '' ) {
                query += '&' + key + '=' + pair[1];
            }
        }
        
        window.location.replace( newUrl + '?' + query );
        
    } else {
        // --- STEP 2: APPLY FILTER (ONLY ONCE WE'RE PAST NUMBERS) ---
        // The list starts from a letter, now we apply the filter to hide any
        // numeric-only titles that might still appear (less likely, but safe).
        $( function() {
            var numericRegex = /^[0-9]+$/; // Match titles that are ONLY digits

            $( '#mw-content-text li a' ).each( function() {
                var title = $( this ).attr( 'title' );
                
                // Check if the title is strictly composed of digits
                if ( title && numericRegex.test( title ) ) {
                    $( this ).parent( 'li' ).hide();
                }
            } );
        } );
    }
}