Jump to content

User:Splarka/sysopdectector.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Splarka (talk | contribs) at 04:22, 6 January 2008 (refactor to not need a global variable, fix apostrophe bug). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/* 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');
  }
}