Jump to content

User:Sreejithk2000/live-edit-counter.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.
///////////////// Get user details

$(document).ready(function () {
	if(wgCanonicalNamespace == "User" || wgCanonicalNamespace == "User_talk")
      GetUserDetails();
   });

function GetUserDetails()
{
        // Avoid subpages
        if(wgTitle.indexOf("/") > -1 || wgTitle.indexOf("\\") > -1 ) return;
	// apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php",
	var apiURL = "http://en.wikipedia.org/w/api.php";

	var query = {
         action: 'query',
         list: 'users',
         ususers: wgTitle,
         usprop: 'editcount|groups',
         format: 'json'
      };

$.ajax({
         url: apiURL,
         cache: false,
         dataType: 'json',
         data: query,
         type: 'GET',
         success: function (result, status, x) {
            if (!result) alert("Receive empty API response:\n" + x.responseText);

            if (result.error) alert("API request failed (" + result.error.code + "): " + result.error.info);

            var groups = String(result.query.users[0].groups);
            groups = groups.replace("*,","");
            $('#firstHeading').append("<div class='catlinks'>" +
                " (" +  result.query.users[0].editcount + " edits. Rights: [" + groups + "])" +
                "</div>");
         },
         error: function (x, status, error) {
            alert("API request returned status " + x.status + " " + status + ". Error code is " + error);
         }
      });

}

///////////////// End of get user details