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 (tableFloat == 'left' || tableFloat == 'right') { tableFloat = ';float:'+tableFloat; } else { tableFloat = ''; }; var table = '{|\n!colspan="2"| Edit count: {{formatnum:'; var skipcaptcha = false; var oldpage = false; var edits; var nsNum; var next; var edittoken;

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) {      nsNum = siResponse.query.namespaces[ns].id;      if (nsNum > -1) {        next = '';        edits = 0;        while (next != 'stop') {          queryApi('action=query&list=usercontribs&ucuser='+wgUserName+'&uclimit=max&ucdir=newer&ucnamespace='+nsNum+next, function(ucResponse) { edits += ucResponse.query.usercontribs.length; if (ucResponse["query-continue"]) { next = '&ucstart='+ucResponse["query-continue"].usercontribs.ucstart; } else { next = 'stop'; };         });        };        if (edits != 0) {          table += '\n|-\n| ';          if (nsNum == 0) {            table += 'Main';          } else {            table += siResponse.query.namespaces[ns].canonical;          };          table += ' || NaN';        };      };    };  }); table += '\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&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); }; }