User:Majr/docTabs.js

$( function { 'use strict';

var namespace = mw.config.get( 'wgCanonicalNamespace' ).replace( '_talk', '' ), title = mw.config.get( 'wgTitle' ), mainPage = namespace + ':' + title.replace( /\/doc$/, '' ), mainPageTalk = namespace + ' talk:' + title.replace( /\/doc$/, '' );

$( '#p-namespaces li:last' ).after(	$( '' ).html( $( ' ' ).html(			$( '' ).prop( { title: 'View the documentation page', href: '/' + mainPage.replace( / /g, '_' ) + '/doc' } ).text( 'Doc' )		) ),	$( '' ).html( $( ' ' ).html(			$( '' ).prop( { title: 'Talk about the documentation page', href: '/' + mainPageTalk.replace( / /g, '_' ) + '/doc' } ).text( 'Doc talk' )		) ) );

if ( title.indexOf( '/doc' ) > -1 ) { $( '#ca-talk,#ca-nstab-' + namespace.toLowerCase ).removeClass( 'selected' ).find( 'a' ).prop( 'href', function {		return this.href.replace( '/doc', '' );	} ); if ( mw.config.get( 'wgCanonicalNamespace' ).indexOf( '_talk' ) > -1 ) { $( '#ca-doc-talk' ).addClass( 'selected' ); } else { $( '#ca-doc' ).addClass( 'selected' ); } }

mw.loader.using( 'mediawiki.api', function {	new mw.Api.get( { action: 'query', prop: 'info', titles: [ mainPage, mainPageTalk, mainPage + '/doc', mainPageTalk + '/doc' ].join( '|' ) } ).done( function( data ) { var nameToId = {}; nameToId[mainPage] = '#ca-nstab-' + namespace.toLowerCase; nameToId[mainPageTalk] = '#ca-talk'; nameToId[mainPage + '/doc'] = '#ca-doc'; nameToId[mainPageTalk + '/doc'] = '#ca-doc-talk';

$.each( data.query.pages, function {			var id = nameToId[this.title];			if ( $( id ).hasClass( 'new' ) ) {				if ( !this.hasOwnProperty( 'missing' ) ) {					$( id ).removeClass( 'new' ).find( 'a' ).prop( 'href', function { return this.href.replace( /index\.php\?title=([^&]+)&action=edit&redlink=1/, '$1' ); } );				}			} else if ( this.hasOwnProperty( 'missing' ) ) {				$( id ).addClass( 'new' ).find( 'a' ).prop( 'href', function { return this.href.replace( /gamepedia\.com\/(.+)/, 'gamepedia.com/index.php?title=$1&action=edit&redlink=1' ); } );			}		} );	} ).fail( function( error ) { console.error( error ); } ); } );

} );