MediaWiki:Common.js

ShoutWiki — express yourself and be heard!
Jump to navigation Jump to search

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
( function ( mw, $ ) {
 'use strict';

	var
		wgSkin = mw.config.get( 'skin' );

	// short-circuit running this file, usually for debugging
	if ( /(?:\?|&)usesitejs=0(?:&|$)/.test( location.search ) ) {
		return;
	}

	/**
	 * For action=purge to index.php
	 *   change it to POST api.php
	 *   then reload the page
	 * Restores pre-1.28 behavior to MW 1.28+
	 * Based on meta:User:Glaisher/autoPurge.js, 21 Aug 2016
	 */
	if (( mw.config.get( 'wgAction' ) === 'purge' ) &&
		( $.inArray( 'user', mw.config.get( 'wgUserGroups' )) + 1 )) {
		$.post( mw.config.get( 'wgScriptPath' ) + '/api.php', {
			format: 'none',
			action: 'purge',
			titles: mw.config.get( 'wgPageName' ).replace( /_/g, ' ' )
		}, function () {
			// remove action=purge, but keep the rest
			location.replace(
				location.pathname +
				location.search
					.replace( /(?:\?|&)action=purge$/i, '' )
					.replace( /(\?|&)action=purge&/i, '$1' ) +
				location.hash
			);
		} );
	}

	// [[Template:Email]] takes name & domain to create
	//   <span class="sw-email">eman@niamod</span>
	// make it sensible for humans
	( function () {
		var
			a, c, s;

		function reverse( s ) {
			var
				t = [],
				i;

			for ( i = s.length - 1; i >= 0; --i ) {
				t.push( s.charAt( i ) );
			}
			return t.join( '' );
		}

		$( '.sw-email' ).each( function ( i, e ) {
			c = e.childNodes;
			s = reverse( c[0].wholeText ) + '@' + reverse( c[2].wholeText );
			// some risky characters are allowed in email addresses
			// so escape them to prevent injection attacks
			a = '<a class="external" href="mailto:' + encodeURI( s ) + '">' +
				s.replace( /["&'/<>]/g, function ( c ) {
					return '&#x' + c.charCodeAt( 0 ).toString( 16 ).toUpperCase() + ';';
				} ) + '</a>';
			$( e ).empty().append( a );
		} );
	}() );

	// [[Template:Title]] takes wikitext to emulate
	//   DISPLAYTITLE with $wgRestrictDisplayTitle = false
	( function () {
		var
			page, meta;

		meta = $( '.sw-title-meta' ).first();
		if ( meta.length === 0 ) {
			return;
		}
		// special cases first
		switch ( wgSkin ) {
		case 'aurora':
			page = $( '#firstHeading > span' ).first();
			break;
		case 'monaco':
			page = $( '.firstHeading' ).first();
			break;
		case 'nimbus':
			page = $( '.pagetitle' ).first();
			break;
		case 'truglass':
			page = $( '#title' );
			break;
		default: // everything else
			page = $( '#firstHeading' );
		}
		page.empty().append( meta.contents() );
		meta.remove();
	}() );

	// remove edit controls if the User_talk: subpage title matches "archive"
	// edit controls = edit page + add section + move page + edit section
	$( function () {
		var
			title = mw.config.get( 'wgTitle' ).split( '/' )[1],
			s;

		if ( ( mw.config.get( 'wgNamespaceNumber' ) === 3 ) &&
			title && /archive/i.test( title ) ) {
			// special cases first
			switch ( wgSkin ) {
			case 'aurora':
				s = '#edit, #addsection, #ca-move, .mw-editsection';
				break;
			case 'monaco':
				s = '#control_edit, #control_addsection, #control_move, .mw-editsection';
				break;
			case 'nimbus':
				s =
					'#article-tabs > [href$="&action=edit"], ' +
					'#article-tabs > [href$="&action=edit&section=new"], ' +
					'#article-more-container > [href*="/Special:MovePage/"], ' +
					'.mw-editsection';
				break;
			default: // everything else
				s = '#ca-edit, #ca-addsection, #ca-move, .mw-editsection';
			}
			$( s ).remove();
		}
	} );
}( mediaWiki, jQuery ) );