Jump to content

User:Versageek/isblocked.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.
/*
 Adds a banner to the top of pages of blocked users. Shamelessly stolen from http://en.wiktionary.org/wiki/User:Conrad.Irwin/isblocked.js
*/

function check_blocked(){
  
  var username;  
  //Is this a user/user_talk page (fails for subpages)
  if( mw.config.get('wgCanonicalNamespace') == 'User' || mw.config.get('wgCanonicalNamespace') == 'User_talk' ){
    username = mw.config.get('wgTitle');

  }else{ //Assume Special:Contributions
     var inputs = document.getElementsByTagName('input');
    for(var i=0;i<inputs.length;i++ ){
      if( inputs[i].name == 'target'){
        username = inputs[i].value;
      }
    }
  }
  
  if(!username) return false;
  var ajaxer = sajax_init_object();
  
  if(! ajaxer) return false;
 
  ajaxer.onreadystatechange = function(){
    if( ajaxer.readyState == 4 ){
      if( ajaxer.status == 200 ){
        var resp = ajaxer.responseText;
        if( resp.indexOf('[expiry]') > 0 ){
          is_blocked(resp);
        }else{
          not_blocked(resp);
        }
      }
    }
  }
 
  ajaxer.open("GET", mw.config.get('wgScriptPath')+ '/api.php?format=txt&action=query&list=blocks&bkprop=expiry&bkusers='+encodeURIComponent(username) );
  ajaxer.send('');

}
function is_blocked(resp){
  var ip = document.getElementById('contentSub');

  var sp = resp.indexOf('[expiry] => ')+12;
  var ep = resp.indexOf("\n",sp);
  var exp = resp.substr(sp,ep-sp);
  var blockNode;
  if(exp == 'infinity')
    blockNode = newNode('b',"indefinitely.");
  else
    blockNode = newNode('span',"until "+exp);

    ip.parentNode.insertBefore( 
    newNode('div',{'style':"border: 1px dashed #884444;background-color: #FFE7DD; text-align: center"},
      newNode('p','This user is blocked from editing ', blockNode ) 
    )
  ,ip.nextSibling);

}
function not_blocked(){ /* Do Nothing */}

if( mw.config.get('wgCanonicalNamespace') == 'User' || mw.config.get('wgCanonicalNamespace') == 'User_talk' || mw.config.get('wgCanonicalSpecialPageName') == 'Contributions' ){
  addOnloadHook(check_blocked);
}

/* DOM abbreviation function */
function newNode(tagname){
 
  var node = document.createElement(tagname);
 
  for( var i=1;i<arguments.length;i++ ){
 
    if(typeof arguments[i] == 'string'){ //Text
      node.appendChild( document.createTextNode(arguments[i]) );
 
    }else if(typeof arguments[i] == 'object'){ 
 
      if(arguments[i].nodeName){ //If it is a DOM Node
        node.appendChild(arguments[i]);
 
      }else{ //Attributes (hopefully)
        for(var j in arguments[i]){
          if(j == 'class'){ //Classname different because...
            node.className = arguments[i][j];
 
          }else if(j == 'style'){ //Style is special
            node.style.cssText = arguments[i][j];
 
          }else if(typeof arguments[i][j] == 'function'){ //Basic event handlers
            try{ node.addEventListener(j,arguments[i][j],false); //W3C
            }catch(e){try{ node.attachEvent('on'+j,arguments[i][j],"Language"); //MSIE
            }catch(e){ node['on'+j]=arguments[i][j]; }}; //Legacy
 
          }else{
            node.setAttribute(j,arguments[i][j]); //Normal attributes
 
          }
        }
      }
    }
  }
 
  return node;
}