User:UBX/LiveEditCounter.js

// Based on User:Henrik/js/live-edit-counter

function liveEditCounter(username) {

if (!document.getElementById('edit-count-id') || !document.getElementById('edit-count-info'))

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)

$('#edit-count-id').text(count); // Update "icon"

$('#edit-count-info').text(count); // Update text

}

$(function() {

if ($.inArray(mw.config.get('wgCanonicalNamespace'), ["User" , "User_talk"]) !== -1) {

var username = encodeURIComponent( mw.config.get('wgTitle').split("/")[0] );

liveEditCounter(username);

}

});