Jump to content

MediaWiki:Get-my-ip.js

From Wikipedia, the free encyclopedia
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.
mw.hook( "wikipage.content" ).add( function( element ) {
    element.find( ".get-my-ip-hide" ).hide();
    mw.loader.using( "mediawiki.util", function() {
        $.get( mw.util.wikiScript( "api"), {
            "action": "query",
            "meta": "userinfo",
            "callback": "",
            "format": "json",
            "formatversion": "2"
        } ).then( function ( data ) {
            if( data.substring( 0, 5 ) === "/**/(" ) {
                var json = JSON.parse( data.substring( 5, data.length - 1 ) );
                if( json && json.query && json.query.userinfo && json.query.userinfo.name && json.query.userinfo.anon ) {
                    element.find( ".get-my-ip-target" ).text( json.query.userinfo.name );
                } else {
                    element.find( ".get-my-ip-target" ).text( "ERROR" );
                    console.error( "Get-my-ip.js: ERROR - malformed API response detected. Response: ", json );
                }
            } else {
                element.find( ".get-my-ip-target" ).text( "ERROR" );
                console.error( "Get-my-ip.js: ERROR - API response had no opening comment. Response: ", data );
            }
        } );
    } );
} );