MediaWiki:UserAndIPTools.js

/** * @name    UserAndIPTools * @desc    Script used to get data about users and include quick tools related to them * @author  Rail01  * * @external jquery * @external mediawiki * @external wikia.window * @external BannerNotification */ require( [   'jquery',    'mw',    'wikia.window',    'BannerNotification' ], function( $, mw, window, BannerNotification ) {

// Loading restrictions and double-run prevention if ( !$( '#UserProfileMasthead' ).exists || window.UserAndIPToolsLoaded ) return; window.UserAndIPToolsLoaded = true;

// Great way to get user nick var $user = $( '#UserProfileMasthead hgroup h1[itemprop="name"]' ).text, $wrapperHTML;

function init ( i18n ) { // Fetch data from API new mw.Api.get( {           action: 'query',            list: 'users',            ususers: $user,            usprop: 'groups|gender|registration|editcount'        } ).done(function( d ) {            /**             * Create links in better way             *             * @param href             * @param name             */            function buildToolLink( href, name ) {                return {                    href: href,                    text: i18n.msg( name ).plain,                    title:  i18n.msg( name + '_title' ).plain,                    target: ( window.UserAndIPToolsOpenInNewPage === true ? '_blank' : null )               };            }

// Handling request errors if ( d.error ) { var errorData = { code: d.error.code, info: d.error.info };               // us400 means "User does not exist" - therefore display IP tools if ( errorData.code === 'us400' ) { $wrapperHTML = $( ' ', { class: 'stalker-ip-tools' } ).append(                       $( ' ', { class: 'ip-tools-header', text: i18n.msg( 'tools-header_ip' ).plain } ),                       $( '', { class: 'anon-tools' } ).append( $( '' ).append(                               $( '', buildToolLink( '//robtex.com/ip-lookup/' + $user, 'ip-tool-whois' ) )                     // WHOIS                            ), $( '' ).append(                               $( '', buildToolLink( '//dan.me.uk/torcheck?' + $.param( { ip: $user } ), 'ip-tool-torcheck' ) ) // TOR Check                            ), $( '' ).append(                               $( '', buildToolLink( '//ip2location.com/demo/' + $user, 'ip-tool-geolocate' ) )                 // Geolocate                            ) )                   );                } else { // Throw any other errors new BannerNotification(                       i18n.msg( 'error', $user, errorData.code, errorData.info ).plain,                        'error'                    ).show; return; }           } else { // Store fetched data as an object var userData = { id: d.query.users[0].userid, editcount: d.query.users[0].editcount, registration: d.query.users[0].registration, gender: d.query.users[0].gender, groups: d.query.users[0].groups.join };

/**                * Debug log *                * Could be disabled by window.UserAndIPToolsDisableDebugLog = true */               if ( !window.UserAndIPToolsDisableDebugLog ) { console.log(                       i18n.msg( 'debug-log', $user, userData.id ).plain                    ); }

// Display data and tools for registered users var ccLink = '//community.fandom.com/'; $wrapperHTML = $( ' ', { class: 'user-stalk_wrapper' } ).append(                   $( ' ', { class: 'stalker-user-info' } ).append( $( ' ', {                           class: 'user-info-header',                            text: i18n.msg( 'info-header' ).plain                        } ), $( '', { class: 'user-info' } ).append(                           $( '' ).append( // ID                                $( ' ', {                                    text: i18n.msg( 'user-id' ).plain,                                    class: 'data-label'                                } ), $( ' ', {                                   text: userData.id,                                    class: 'data-value'                                } ) ),                           $( '' ).append( // Gender $( ' ', {                                   text: i18n.msg( 'user-gender' ).plain,                                    class: 'data-label'                                } ), $( ' ', {                                   text: userData.gender,                                    class: 'data-value'                                } ) ),                           $( '' ).append( // Registeration $( ' ', {                                   text: i18n.msg( 'user-registration' ).plain,                                    class: 'data-label'                                } ), $( ' ', {                                   text: userData.registration,                                    class: 'data-value'                                } ) ),                           $( '' ).append( // Editcount $( ' ', {                                   text: i18n.msg( 'user-editcount' ).plain,                                    class: 'data-label'                                } ), $( ' ', {                                   text: userData.editcount,                                    class: 'data-value'                                } ) ),                           $( '' ).append( // Groups $( ' ', {                                   text: i18n.msg( 'user-groups' ).plain,                                    class: 'data-label'                                } ), $( ' ', {                                   text: userData.groups,                                    class: 'data-value'                                } ) )                       )                    ),                    $( ' ', { class: 'stalker-user-tools' } ).append( $( ' ', {                           class: 'user-tools-header',                            text: i18n.msg( 'tools-header' ).plain                        } ), $('', { class: 'user-tools' }).append(                           $( '' ).append( $( '<a>', buildToolLink( ccLink + 'User:' + $user + '/global.js', 'user-tool-js' ) )                         // Global JS                            ),                            $( '<li>' ).append( $( '<a>', buildToolLink( ccLink + 'User:' + $user + '/global.css', 'user-tool-css' ) )                       // Global CSS ),                           $( '<li>' ).append( $( '<a>', buildToolLink( '//services.fandom.com/user-attribute/user/' + userData.id, 'user-tool-services' ) ) // Services API ),                           $( '<li>' ).append( $( '<a>', buildToolLink( ccLink + 'Special:Log/rights?' + $.param( { page: $user } ), 'user-tool-cc_log' ) ) // CC userrights log )                       )                    )                );            }            // Display UI            $( '#UserProfileMasthead .details' ).after( $wrapperHTML ); } );   }

// Load i18n messages mw.hook( 'dev.i18n' ).add( function( i18n ) {       i18n.loadMessages( 'UserAndIPTools' ).then( init );    } );

// Import i18n-js and CSS importArticle( {       type: 'style',        article: 'u:dev:MediaWiki:UserAndIPTools.css'    }, {        type: 'script',        article: 'u:dev:MediaWiki:I18n-js/code.js'    } ); } );