From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
m (fixed errors from copying old version)
m (substing?)
Line 23: Line 23:
     tableFloat = '';
     tableFloat = '';
   };
   };
   var table = '{|{{prettytable|class='+tableClass+'|style=width:200px'+tableFloat+'}}\n!colspan="2"| Edit count: ';
   var table = '{|{{prettytable|class='+tableClass+'|style=width:200px'+tableFloat+'}}\n!colspan="2"| Edit count: {{formatnum:';
   var skipcaptcha = false;
   var skipcaptcha = false;
   var oldpage = false;
   var oldpage = false;
Line 31: Line 31:
   var edittoken;
   var edittoken;


   queryApi('action=query&meta=userinfo&uiprop=editcount';
   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+'}}';
   });
   });


Line 57: Line 63:
             table += siResponse.query.namespaces[ns].canonical;
             table += siResponse.query.namespaces[ns].canonical;
           };
           };
           table += ' || '+edits+'';
           table += ' || {{formatnum:'+edits+'}}';
         };
         };
       };
       };

Revision as of 01:27, 22 January 2010

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 = '{|{{prettytable|class='+tableClass+'|style=width:200px'+tableFloat+'}}\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 += ' || {{formatnum:'+edits+'}}';
        };
      };
    };
  });
  
  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 (res.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);
  };
}