Jump to content

User:Andrybak/Scripts/user-tabs-on-contribs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Andrybak (talk | contribs) at 00:26, 4 March 2024 (implement localization via mw.messages() per Special:Diff/1211703874). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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 = $( "<li>" ).append( $( "<a>" )
                    .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" );
        tab.addClass('vector-tab-noicon mw-list-item');
        return tab;
    }
    
    function getUserPageTabName() {
    	const nsTabUser = mw.messages('Nstab-user');
    	if (!nsTabUser) {
    		return "User page";
    	}
    	return nsTabUser.text();
    }
    
    function getUserTalkPageTabName() {
    	const nsTabUserTalk = mw.messages('Nstab-user talk');
    	if (!nsTabUserTalk) {
    		const talk = mw.messages('Talk');
    		if (!talk) {
    			return "Talk";
    		}
    		return talk.text();
    	}
    	return nsTabUserTalk.text();
    }

    $.when(
        mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] ),
        $.ready
    ).then( function () {
    	/*
    	 * Load localization messages:
    	 * [[MediaWiki:Nstab-user talk]], [[MediaWiki:Nstab-user]], [[MediaWiki:Talk]]
    	 */
    	new mw.Api().loadMessagesIfMissing(['Nstab-user', 'Nstab-user talk', 'Talk']).done(() => {
			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-associated-pages').removeClass('emptyPortlet');
                	const userPageTabName = getUserPageTabName();
                	const userTalkPageTabName = getUserTalkPageTabName();
                	$( "#left-navigation nav ul" )
	                    .append(utocMakeTab(userPageTabName, "User:" + username, userPageExists, "c", "ca-nstab-user"))
                    	.append(utocMakeTab(userTalkPageTabName, "User talk:" + username, userTalkPageExists, "t", "ca-nstab-talk"));
            	}
        	} );
    	});
    } );
}