MediaWiki:Common.js: Difference between revisions

From Good Creations! MC Wiki
Jump to navigation Jump to search
mNo edit summary
Tag: Reverted
mNo edit summary
Tag: Reverted
Line 66: Line 66:
         // Update the "Page" tab (ca-nstab-main) link to go back to the previous page (or the main page)
         // Update the "Page" tab (ca-nstab-main) link to go back to the previous page (or the main page)
         $('#ca-nstab-main').show().find('a').attr('href', previousPage || '/wiki/Main_Page');  // Default to main page if no previous page
         $('#ca-nstab-main').show().find('a').attr('href', previousPage || '/wiki/Main_Page');  // Default to main page if no previous page
       
        // Disable the click on the Discord tab to avoid repeating the action and breaking the referrer history
        $('#ca-my_namespace a').on('click', function(event) {
            event.preventDefault(); // Prevent the default action (i.e., reloading the current page)
        });
     } else {
     } else {
         // If not on the Discord page, reset the tabs to their normal state
         // If not on the Discord page, reset the tabs to their normal state

Revision as of 13:15, 16 March 2025

/*
 * This is the JS for all skins (for both mobile and desktop) on MediaWiki.org.
 * Consider whether you can use a gadget with one of the options set to limit
 * where it is loaded. See [[Extension:Gadgets#Options]].
 */
/* global mw, $ */
/**
 * Hide prefix in category
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
 * @rev 5
 */
var prefix = $.trim( $( '#mw-cat-hideprefix' ).text() ) || ( mw.config.get( 'wgTitle' ) + '/' );
$( '#mw-pages' ).find( 'a' ).text( function ( i, val ) {
    return val.slice( 0, prefix.length ) === prefix ? val.slice( prefix.length ) : val;
} );

/**
 * @source https://www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
 * @revision 2017-05-16
 */
mw.loader.using( ['mediawiki.util'], function () {
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' ),
		extraModule = mw.util.getParamValue( 'withModule' );

	if ( extraCSS ) {
		// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.css$/.test( extraCSS ) ) {
			mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraCSS ) + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		// WARNING: DO NOT REMOVE THIS "IF" - REQUIRED FOR SECURITY (against XSS/CSRF attacks)
		if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
			mw.loader.load( '/w/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	if ( extraModule ) {
		if ( /^ext\.gadget\.[^,\|]+$/.test( extraModule ) ) {
			mw.loader.load( extraModule );
		} else {
			mw.notify( 'Only gadget modules are allowed.', { title: 'Invalid withModule value' } );
		}
	}
});

if ( mw.config.get( 'wgPageName' ) === 'Discord' ) {
	$(document).ready(function() {
    	// Get the current page title
    	var currentPage = mw.config.get('wgTitle');
    	var previousPage = document.referrer; // Get the URL of the previous page

    	// Check if the current page is 'Discord'
    	if (currentPage === 'Discord') {
        	// Directly select the Discord tab
        	$('#ca-nstab-main').removeClass('selected');  // Remove the 'selected' class from the "Page" tab
        	$('#ca-my_namespace').addClass('selected');   // Add the 'selected' class to the "Discord" tab

        	// Update the "Page" tab (ca-nstab-main) link to go back to the previous page (or the main page)
        	$('#ca-nstab-main').show().find('a').attr('href', previousPage || '/wiki/Main_Page');  // Default to main page if no previous page
        	
        	// Disable the click on the Discord tab to avoid repeating the action and breaking the referrer history
        $('#ca-my_namespace a').on('click', function(event) {
            event.preventDefault(); // Prevent the default action (i.e., reloading the current page)
        });
    	} else {
        	// If not on the Discord page, reset the tabs to their normal state
        	$('#ca-my_namespace').removeClass('selected'); // Remove 'selected' from the "Discord" tab
        	$('#ca-nstab-main').addClass('selected');       // Ensure the "Page" tab is selected by default
    	}
	});
}