Jump to content

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

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 06:33, 6 December 2018 (fix). 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.
$( function () {
    if( mw.config.get( "wgPageName" ).indexOf( "Special:Contributions/" ) === 0 ) {

        /** Makes a tab linking to the given page name. redlink is a boolean indicating
         * whether the page exists or not. */
        function makeTab( label, pageName, redlink ) {
            var tab = $( "<li>" ).append( $( "<span>" ).append( $( "<a>" )
                    .text( label )
                    .href( mw.util.getUrl( target ) ) ) );
            if( redlink ) tab.addClass( "new" );
            return tab;
        }

        mw.loader.using( [ "mediawiki.api", "mediawiki.util" ], function () {
            var username = mw.config.get( "wgTitle" ).substring( 14 );
            new mw.Api().get( {
                 action: "query",
                 titles: "User:" + username + "|User talk:" + username
            } ).done( function ( data ) {console.log(data);
                if( data && data.query && data.query.pages ) {
                    var exists = {};
                    Object.values( data.query.pages ).forEach( function ( v ) {
                        exists[ v.title.charAt( 4 ) ] = !v.missing;
                    } );

                    $( "#ca-nstab-special" )
                        .after( makeTab( "User page", "User:" + username, exists[ ":" ] ) )
                        .after( makeTab( "Talk", "User talk:" + username, exists[ " " ] ) )
                }
            } );
        } );
    }
} );