User:Aude/nearby.js

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.
( function ( mw, $ ) {

function setMessages() {
	switch ( mw.config.get( 'wgUserLanguage' ) ) {
		default:
		case 'en':
			mw.messages.set( {
				'gadget-nearby': 'Nearby'
			} );
			break;
	}
}

function init() {
	var action = mw.config.get( 'wgAction' ),
	    $indicators,
	    $nearbyEl;
	
	if ( action === 'view' ) {
		if ( mw.config.get( 'wgNamespaceNumber' ) != 0 ) {
			return;
		}
		
		if ( document.getElementById( 'P625' ) == null ) {
			return;
		}
		
		if ( mw.config.get( 'skin') == 'minerva' ) {
			$indicators = $( '#page-actions' );
			$nearbyEl = $( '<li>' );
		} else {
			$indicators = $( '.mw-indicators' ).first();
			$nearbyEl = $( '<div>' );
		}

		if ( $indicators.find( '#mw-indicator-nearby' ).length > 0 ) {
			return;
		}

		setMessages();

		var nearbyText = mw.msg( 'gadget-nearby' );

		$nearbyEl
			.addClass( 'mw-indicator mw-indicator-nearby' )
			.attr( {
				id: 'mw-indicator-nearby',
				title: nearbyText,
				alt: nearbyText
			} )
			.appendTo( $indicators )
			.click( function( e ) {
				var url =  window.location.origin + '/wiki/Special:Nearby#/page/' + mw.config.get( 'wgPageName' );
				window.location.replace( url );
			} );
	}
}

$( init );

}( mediaWiki, jQuery ) );