Jump to content

User:JackSchmidt/JS UserWatch.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.
JS_UserWatch = {};

JS_UserWatch.MakeButton = function(user,nick) {
  if(!nick) nick=user;
  addOnloadHook( function() {
    mw.util.addPortletLink('p-cactions',"javascript:JS_UserWatch.UpdateEditCount('" + user + "', '" + nick + "');",nick,"ca-" + user + "watch");
} );
};

JS_UserWatch.EditCountFromContribCounterText = function(txt) {
  try { return eval('(' + txt + ')').query.allusers[0].editcount; } catch(e) { return "?"; };
};
JS_UserWatch.UpdateEditCount = function(user,nick) {
  if(!nick) nick=user;
  document.getElementById("ca-" + user + "watch").getElementsByTagName("a")[0].innerHTML 
      = nick + "...";
  JS_PHP(
    "api.php",
    { action:"query", list:"allusers", aulimit:1, auprop:"editcount", aufrom:user, format:"json" },
    user + "Watch",
    function(id,txt) { try {
      var li,a1,a2;
      li = document.getElementById("ca-" + user + "watch");
      a1 = document.createElement('a');
      a2 = document.createElement('a');
      a1.href="javascript:JS_UserWatch.UpdateEditCount('" + user + "','" + nick + "');";
      a1.title='Update editcount';
      a1.appendChild(document.createTextNode(nick));
      a2.href= wgArticlePath.replace(/\$1/, 'Special:Contributions/'+user);
      a2.title='Special:Contributions/'+user;
      a2.appendChild(document.createTextNode(JS_UserWatch.EditCountFromContribCounterText(txt)));
      li.innerHTML = ' ';
      li.appendChild(a1);
      li.appendChild(document.createTextNode(': '));
      li.appendChild(a2);
      if(addTooltip) addTooltip(a2);
      } catch(e) { }
    }
  )
};