User:Najzere/edit counter.js

if (wgTitle == wgUserName && wgNamespaceNumber == 2) { addOnloadHook(makeEditCountButton); }

function makeEditCountButton { var newlist = document.createElement('li'); newlist.id = "t-editcounter"; var newlink = document.createElement('a'); newlink.href = 'javascript:countEdits'; var linkname = document.createTextNode('Edit counts'); newlink.appendChild(linkname); newlist.appendChild(newlink); document.getElementById('nav_right').getElementsByTagName('ul')[0].insertBefore(newlist, document.getElementById('t-contributions').nextSibling); }

function countEdits { document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML = 'Calculating...';

var editSummary = 'Edited with User:Najzere/edit counter.js'; if (typeof editPage == "undefined") {var editPage = 'Edit count'); if (typeof tableClass == "undefined") {var tableClass = };  if (typeof tableFloat == "undefined") {var tableFloat = };  if (typeof tableStyle == "undefined") {var tableStyle = };  if (tableClass != ) {    tableClass = '|class='+tableClass;  };  if (tableFloat == 'left' || tableFloat == 'right') {    tableFloat = '|float='+tableFloat;  } else {    tableFloat = ;  };  if (tableStyle != ) {    tableStyle = '|style='+tableStyle;  };  var skipcaptcha = false;  var oldpage = false;  var edits;  var namespaces = new Array;  var movelog = new Array;  var protectlog = new Array;  var next;  var edittoken;  var brace = '{';  var table = '{|'+brace+brace+'prettytable'+tableClass+tableFloat+tableStyle'}}\n!colspan="2"| Edit count: '+brace+brace+'subst:formatnum:';

queryApi('action=query&meta=userinfo&uiprop=editcount|rights', function(uiResponse) {   for (var r in uiResponse.query.userinfo.rights) {      if (uiResponse.query.userinfo.rights[r] == "autoconfirmed") {        skipcaptcha = true;      };    };    table += uiResponse.query.userinfo.editcount+'}}';  });

queryApi('action=query&meta=siteinfo&siprop=namespaces', function(siResponse) {   for (var ns in siResponse.query.namespaces) {      if (siResponse.query.namespaces[ns].id > -1) {        if (siResponse.query.namespaces[ns].id == 0) {          namespaces[siResponse.query.namespaces[ns].id] = 'Main';        } else {          namespaces[siResponse.query.namespaces[ns].id] = siResponse.query.namespaces[ns].canonical;        };        movelog[siResponse.query.namespaces[ns].id] = 0;        protectlog[siResponse.query.namespaces[ns].id] = 0;      };    };  });

next = ''; while (next != 'stop') { queryApi('action=query&list=logevents&letype=move&leuser='+wgUserName+'&lelimit=max&leprop=title'+next, function(moveResponse) {     for (var event in moveResponse.query.logevents) {        movelog[moveResponse.query.logevents[event].ns] += 1;      }      if (moveResponse["query-continue"]) {        next = '&lestart='+moveResponse["query-continue"].logevents.lestart;      } else {        next = 'stop';      };    }); }

next = ''; while (next != 'stop') { queryApi('action=query&list=logevents&letype=protect&leuser='+wgUserName+'&lelimit=max&leprop=title'+next, function(protectResponse) {     for (var event in protectResponse.query.logevents) {        protectlog[protectResponse.query.logevents[event].ns] += 1;      }      if (protectResponse["query-continue"]) {        next = '&lestart='+protectResponse["query-continue"].logevents.lestart;      } else {        next = 'stop';      };    }); }

for (var i = 0; i < namespaces.length; i++) { next = ''; edits = 0; while (next != 'stop') { queryApi('action=query&list=usercontribs&ucuser='+wgUserName+'&uclimit=max&ucdir=newer&ucnamespace='+i+next, function(ucResponse) {       edits += ucResponse.query.usercontribs.length;        if (ucResponse["query-continue"]) {          next = '&ucstart='+ucResponse["query-continue"].usercontribs.ucstart;        } else {          next = 'stop';        };      }); };   edits = edits - movelog[i] - protectlog[i]; if (edits > 0) { table += '\n|-\n| '+namespaces[i]+' || '+brace+brace+'subst:formatnum:'+edits+'}}'; }; };  var rightnow = new Date.toLocaleDateString.replace(/^.*?, /g,''); table += '\n|-\n|colspan="2" align="center"| Last run: '+brace+brace+'subst:#dateformat:'+rightnow+'|mdy}} \n|}';

queryApi('action=query&prop=info|revisions&intoken=edit&titles=User:'+wgUserName+'/'+editPage, function(propResponse) {   for (var page in propResponse.query.pages) {      edittoken = propResponse.query.pages[page].edittoken.replace(/\+\\$/g,'%2B%5C');      if (propResponse.query.pages[page]["revisions"]) {        oldpage = true;      };    };  });

if (!skipcaptcha && !oldpage) { alert("Sorry, you're not autoconfirmed yet, so captcha is required to make new pages. Go create the page at: User:"+wgUserName+"/"+editPage+", then try again."); return false; };

queryApi('action=edit&title=User:'+wgUserName+'/'+editPage+'&summary='+editSummary+'&minor=1&recreate&text='+table+'&token='+edittoken, function(actionResponse) {   if (actionResponse.edit.result == "Success") {      document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML = 'Done!';    } else {      document.getElementById('t-editcounter').getElementsByTagName('a')[0].innerHTML = 'Error!';    };  }); }

function queryApi(query, callback) { try { var xmlRequest = new XMLHttpRequest; } catch(e) { try { var xmlRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { throw "Could not create an XmlHttpRequest"; }     }    }

xmlRequest.open('POST', wgScriptPath+'/api.php', false); xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlRequest.send(query+'&format=json'); var xmlResponse = new Function("return "+xmlRequest.responseText); if (!xmlResponse) { alert('Problem retrieving information'); } else { callback(xmlResponse); }; }