User:Splarka/sysopdectector.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/* This is a proof-of-concept 'sysop detector' script using ajax.
  + The current implementation only triggers on user pages (and not user subpages) in namespace 2.
   . This could be extended to namespace 3, but suggested not to trigger on any subpages.
  + The page does not need to exist. Works on edit/history and anything with tabs. Should work in all monobook-dervied skins.
  + The current display is in ca-nstab-user (the leftmost tab). the text 'sysop' is added.
   . Alternatives include adding a class to the body tag, for css manipulation.
  + Sysops versed in JS (zocky/lupin/ruud/others?) may edit this page freely (this is simply a proof-of-concept test) or fork it.
   . If you do, please update these notes as appropriate.
   . Please be aware my ajax is amateurish.
  + Stab MZMcBride if anything goes wrong.
*/
 
if(wgNamespaceNumber == 2) addOnloadHook(getGroupSysop)
function getGroupSysop() {
  if(wgTitle.indexOf('/')==-1) {
    var url = wgServer + wgScriptPath + '/api.php?action=query&list=allusers&aulimit=1&augroup=sysop&format=xml&aufrom=' + encodeURIComponent(wgTitle);
    getXMLsysop(url);
  }
}
 
 
function getXMLsysop(url) {
  var getReq;
  if (window.XMLHttpRequest) { // Non-IE browsers
    getReq = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    getReq = new ActiveXObject('Microsoft.XMLHTTP');
  }
  if (getReq) {
    getReq.onreadystatechange = function () {
      switch (getReq.readyState) {
        case 4:
          if (getReq.status == 200) { // OK response
            var txt = getReq.responseText;
            var sysgrp = '<u name="' + wgTitle.replace(/\'/g,'&#039;') + '" />';
            var isSysop = (txt.indexOf(sysgrp) > -1) ? true : false
            if(isSysop) {
              document.getElementById('ca-nstab-user').getElementsByTagName('a')[0].appendChild(document.createTextNode(' (sysop)'));
            }
          } else {
            alert('Error:' + getReq.statusText)
          }
          break;
      }
    }
    try {
      getReq.open('GET', url, true);
      getReq.send('');
    } catch (e) {
      alert(e);
    }
  } else {
    alert('XMLHTTPRequest not supported');
  }
}