Jump to content

User:CharlotteWebb/editcount.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.
addOnloadHook(function (){
  a = '<a href="/w/index.php?title=' + wgPageName.split("/")[0] + '&amp;action=editcount">Edit count</a>';
  if((wgNamespaceNumber == 2 || wgNamespaceNumber == 3)) {
    document.getElementById("p-cactions").getElementsByTagName("ul")[0].innerHTML +=
    '\n<li id="ca-editcount">' + a + '</li>';
    tc = document.getElementById("t-contributions");
    if(tc) tc.innerHTML += '</li>\n<li id="ca-editcount">' + a;
    }
  if(wgCanonicalSpecialPageName == "Contributions"){
    cs = document.getElementById("contentSub");
    cs.innerHTML = cs.innerHTML.replace(/(\))\s*$/, " | " + a + ")");
    }
  if(wgAction != "editcount")  return;
  document.getElementsByTagName("h1")[0].innerHTML = document.title =
    "Edit count of User:" + wgTitle;
  (bc = document.getElementById("bodyContent")).innerHTML = "(loading...)";
  var d = new Object, m = new Object, ns = new Object; d.ec = 0; d.o = null;
  d.q = 'action=query&list=usercontribs&ucprop=title&uclimit=500&ucuser=' + wgTitle;
  var scr = function(q, h){
    x = new XMLHttpRequest(); x.onreadystatechange = function() {
      if (x.readyState == 4) h(new DOMParser().parseFromString(x.responseText,"text/xml"));
      }
    x.open("GET", wgServer + "/w/api.php?" + q + "&rawcontinue=&format=xml", true); x.send("");
    }
  var nsh = function(nf){
    n = nf.getElementsByTagName("ns");
    for(i = 0; i < n.length; i++){
      m[id = n[i].getAttribute("id")] = 0;
      ns[id] = n[i].getAttribute("canonical");
      }
    ns["0"] = "[main]";
    var ech = function(ef) {
      r = ef.getElementsByTagName("item");
      for(i = 0; i < r.length; i++) m[r[i].getAttribute("ns")]++;
      d.ob = 'total edits: ' + (d.ec += r.length) + ' <span id="ecstatus">(and counting...)' +
        '</span>\n<table class="wikitable sortable" style="width:300px;">\n' +
        '<tr><th>Namespace</th><th>Edits</th><th>Percent</th></tr>\n';
      for(n in m) if (m[n] > 0) d.ob += '<tr style="text-align:right;">' + 
        '<td style="text-align:left;">' + ns[n] + '</td><td>' + m[n] + '</td><td>' +
        String(Math.round(parseInt(m[n])/(d.ec/1000.0))/10.0).replace(/^(\d+)$/, '$1.0') +
        '</td></tr>\n';
      bc.innerHTML = (d.ob += '</table>');
      np = ef.getElementsByTagName("query-continue");
      if(!np.length) document.getElementById("ecstatus").innerHTML = "<b>(Done!)</b>";
      else {
        d.o = np[0].childNodes[0].getAttribute("ucstart");
        scr(d.q + (d.o?"&ucstart="+d.o:""), ech);
        }
      }
    scr(d.q + (d.o ? "&ucstart=" + d.o : ""), ech);
    }
  scr("action=query&meta=siteinfo&siprop=namespaces", nsh);
  });