MediaWiki:Common.js

From Wikimedia Incubator

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
 * Please for the sake of the wiki, please do *some* testing of new Javascript code
 * in your own common.js (Special:MyPage/common.js) before adding them here. As far
 * as possible, avoid adding new stuff here and make your new snippets as a gadget
 * (especially if it requires ResourceLoader).
 *
 * We currently don't have any type of voting to decide what should be here, use
 * common sense whenever possible (e.g Is my snippet going to benefit people?).
 * Talk to the other administrators at #wikimedia-incubator about it first and 
 * get their approval before starting to add code here (make sure it works!).
 *
 * Please add documentation about what each snippet does in the form of comments so
 * that other people will know what each snippet does and would be useful if they
 * are copying to another wiki. Its good to share :)
 */

/* ========== Global.js ==========
 * The mobile site does _not_ load Common.js, unlike other skins; therefore,
 * load [[MediaWiki:Globals.js]] to get one common page for Javascript that
 * should be the same for mobile and desktop.
 */
mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:Globals.js&action=raw&ctype=text/javascript' );

/* ========== Ticker from Wikinews ========== */
if((document.getElementById("singleTickerForPage") || document.getElementById('enableTickers'))) {
     mw.loader.load("//en.wikinews.org/w/index.php?title=MediaWiki:Ticker2.js&action=raw&ctype=text/javascript");
}

/* ========== Embedds edit tools drop down menu ========== */
 
if ($.inArray(mw.config.get('wgAction'), ['edit' , 'submit']) !== -1) {
     mw.loader.load("//incubator.wikimedia.org/w/index.php?title=MediaWiki:Onlyifediting.js&action=raw&ctype=text/javascript");
}

/* ========== Test if an element has a certain class - needed for Dynamic Navigation ========== */
 
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();


/* ==== Dynamic navigation ==== */
/* both from [[b:]] */
mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:DynamicNavigation.js&action=raw&ctype=text/javascript' );
/* mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:NavigationTabs.js&action=raw&ctype=text/javascript' ); */

/**
 * Ajax transclusion table adds dynamic content loading to tables with class="attable".
 * @see https://meta.wikimedia.org/wiki/Ajax_transclusion_table
 * @update-token [[File:pathoschild/ajaxtransclusiontable.js]]
 */
mw.loader.load('//incubator.wikimedia.org/w/index.php?title=MediaWiki:Ajaxtransclusiontable.js&action=raw&ctype=text/javascript');

/* Languages awaiting RTLness in MediaWiki (NOTE: also update the list in Common.css) */
var RTLgetContent = document.getElementById('mw-content-text');
if( RTLgetContent.getAttribute('lang') in { 'fuc':'', 'mey': '', 'ota':'', 'phr':'','prs':'', 'tmr':'' } ) {
 RTLgetContent.className = 'mw-content-rtl';
}

/* CSS/JS per test wiki (experimental) */
var prefix = mw.config.get( 'wgWmincTestwikiPrefix' );
if ( prefix ) {
	mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:' + prefix + '/Common.css&action=raw&ctype=text/css', 'text/css' );
	mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:' + prefix + '/Common.js&action=raw&ctype=text/javascript' );
	var skin_cap = mw.config.get('skin').charAt(0).toUpperCase() + mw.config.get('skin').slice(1);
	mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:' + prefix + '/' + skin_cap + '.css&action=raw&ctype=text/css', 'text/css' );
	mw.loader.load( '//incubator.wikimedia.org/w/index.php?title=MediaWiki:' + prefix + '/' + skin_cap + '.js&action=raw&ctype=text/javascript' );
}

/**
 * Remove the test wiki prefix from category links (at the bottom of articles)
 * and from category pages.
 * 
 * @version 1.0.0 (2022-09-26)
 * @author Jon Harald Søby
 */

( function() {
	if (
		mw.config.get( 'wgWmincRealPagename' ) ||
		mw.config.get( 'wgPageName' ) === 'Category:' + mw.config.get( 'wgWmincTestwikiPrefix' )
	) {
	  $( '.mw-category-generated a, #catlinks a' ).each( function() {
	    $( this ).text( $( this ).text().replace( mw.config.get( 'wgWmincTestwikiPrefix' ) + '/', '' ) );
	  });
	}
})();

/**
 * Fix for sign language test wikis being broken in the Vector-2022 skin.
 * 
 * Sign language wikis written in SignWriting are broken in all skins except
 * legacy Vector. This snippet makes the page reload using the Vector skin
 * if you visit a page belonging to such a test wiki. Once that is done, all
 * content links get &useskin=vector appended, so that a reload won't be
 * necessary until you leave the test wiki again.
 * 
 * Relevant Phabricator tasks: T312846, T312844
 * 
 * @version 1.1.0 (2023-11-27)
 * @author Jon Harald Søby
 */

mw.hook( 'incubator.globalsReady' ).add( function() {
	var signLanguages = mw.config.get( 'wgWmincSignLanguages' );
	if (
		mw.config.get( 'skin' ) !== 'vector' &&
		signLanguages.includes( mw.config.get( 'wgPageContentLanguage' ) ) &&
		mw.config.get( 'wgWmincRealPagename' ) &&
		mw.config.get( 'wgAction' ) === 'view'
	) {
		var newUrl = new URL( window.location.href );
		newUrl.searchParams.append( 'useskin', 'vector' );
		window.location.href = newUrl.href;
	} else if (
		signLanguages.includes( mw.config.get( 'wgPageContentLanguage' ) ) &&
		mw.util.getParamValue( 'useskin' )
	) {
		$( '#content a, #p-namespaces a' ).each( function() {
			var href = $( this ).attr( 'href' );
			if ( /^\/wiki\/([\w_]+:)?W[a-z]\/[a-z]{3}\//.test( href ) ) {
				$( this ).attr( 'href', mw.util.getUrl( href.replace( /^\/wiki\//, '' ), { 'useskin': 'vector' } ) );
			}
		});
	}
});