User:Enterprisey/user-tabs-on-contribs.js

if( mw.config.get( "wgPageName" ).indexOf( "Special:Contributions/" ) === 0 ||

mw.config.get( "wgPageName" ).indexOf( "Special:Contributions" ) === 0 && mw.util.getParamValue( "target" ) ) {

/** Makes a tab linking to the given page name. redlink is a boolean indicating

* whether the page exists or not. */

function utocMakeTab( label, pageName, redlink, accesskey, id ) {

var tab = $( "

  • " ).append( $( "" ).append( $( "" )

    .text( label )

    .attr( "accesskey", accesskey )

    .attr( "title", pageName + " (access key " + accesskey + ")" )

    .attr( "href", mw.util.getUrl( pageName ) ) ) )

    .attr( "id", id );

    if( redlink ) tab.addClass( "new" );

    return tab;

    }

    $.when(

    mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ),

    $.ready

    ).then( function () {

    var username = mw.config.get( "wgTitle" ).substring( 14 ) || mw.util.getParamValue( "target" );

    new mw.Api().get( {

    action: "query",

    titles: "User:" + username + "|User talk:" + username,

    formatversion: "2"

    } ).done( function ( data ) {

    if( data && data.query && data.query.pages ) {

    var userPageExists = true, userTalkPageExists = true;

    Object.values( data.query.pages ).forEach( function ( v ) {

    if( v.title.startsWith( "User:" ) ) userPageExists = v.hasOwnProperty( "missing" );

    else userTalkPageExists = v.hasOwnProperty( "missing" );

    } );

    $( "#p-namespaces ul" )

    .append( utocMakeTab( "User page", "User:" + username, userPageExists, "c", "ca-nstab-user" ) )

    .append( utocMakeTab( "Talk", "User talk:" + username, userTalkPageExists, "t", "ca-nstab-talk" ) );

    }

    } );

    } );

    }