Jump to content

User:Xyzzy529/LiveEditCounter.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// Based on [[User:Henrik/js/live-edit-counter]]
// updated by [[User:UBX/LiveEditCounter.js]]
function liveEditCounter_xy(username)
{
//  if (!document.getElementById('edit-count-id') || !document.getElementById('edit-count-info')) 
    if (!document.getElementById('edit-count-award') || !document.getElementById('edit-count-next'))
        return;
    var count="";
    if (mw.config.get('wgUserName') == mw.config.get('wgTitle')) // If a user is viewing their own page, the data has already been loaded, no need to make a XHR
    {
        count = mw.config.get('wgUserEditCount');
    }
    else
    {
        var xhr;
        try { xhr = new XMLHttpRequest(); }
        catch(e)
        {
            xhr = new ActiveXObject(Microsoft.XMLHTTP);
        }
 
        xhr.onreadystatechange  = function()
        {
            if(xhr.readyState  == 4)
            {
                if(xhr.status  == 200) {
                    var doc = xhr.responseXML;
                    count = doc.getElementsByTagName('user')[0].getAttribute('editcount')
                }
            }
        };
 
        xhr.open('GET', "http://en.wikipedia.org/w/api.php?action=query&list=users&usprop=editcount&format=xml&ususers="+username, true);
        xhr.send(null);
    }
    if(!count) count="0"; // Cater to zero edit counts
    count = (count+'').replace(/(?=(?:\d{3})+$)(?!^)/g, ','); // Add commas as thousand separators (hat tip to http://jsperf.com/number-format)
    document.getElementById('edit-count-award').innerHTML=count; // Update "icon"
    document.getElementById('edit-count-next').innerHTML=count; // Update text
}
 
$(function() {
  if ($.inArray(mw.config.get('wgCanonicalNamespace'), ["User" , "User_talk"]) !== -1) {
     var username = encodeURIComponent( mw.config.get('wgTitle').split("/")[0] );
     liveEditCounter_xy(username);
  }
});