User:RandomStringOfCharacters/Scripts/DefconBar.js

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.
addOnloadHook(defcon_init);

function defcon_init(){

  var link_div = document.createElement('div');
  link_div.className = 'body';
  var div = document.createElement('div');
  div.setAttribute('id', 'p-defcon');
  div.className = 'portal';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode('Defcon'));
  div.appendChild(heading);
  div.appendChild(link_div);
 
  // enable link
  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  var a = document.createElement('a');
  a.appendChild(document.createTextNode('Check Defcon'));
  a.onclick = defcon_make_req;
  p.appendChild(a);
  link_div.appendChild(p);
 
  // now replace the div
  var old_div = document.getElementById('p-defcon');
  var side_col = document.getElementById('panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-interaction');
    side_col.insertBefore(div, node);


}}

function defcon_make_req(){

  try {
    npp_http = new XMLHttpRequest();
 
  } catch (e) {
    try {
      npp_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        npp_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        return false;
      }
    }
  }
 
  npp_http.onreadystatechange = function() {
    if(npp_http.readyState == 4) defcon_ajax_response();
  }

  npp_http.open("GET", "http://en.wikipedia.org/wiki/Wikipedia:Defcon", true);
  npp_http.send(null);


}

function defcon_ajax_response() {


  var div = document.createElement('div');
  div.setAttribute('id', 'p-defcon');
  div.className = 'portal expanded';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode('Defcon'));
  div.appendChild(heading);

  var link_div = document.createElement('div');
  link_div.className = 'body';
  link_div.style.display = "block";

var level = 'lol';

//re = /\|info =(.*)\|align/ ;



if (npp_http.responseText.indexOf('Dc_one') == -1){}else{
level = 'Level 1';
}
if (npp_http.responseText.indexOf('Dc_two') == -1){}else{
level = 'Level 2';
}
if (npp_http.responseText.indexOf('Dc_three') == -1){}else{
level = 'Level 3';
}
if (npp_http.responseText.indexOf('Dc_four') == -1){}else{
level = 'Level 4';
}
if (npp_http.responseText.indexOf('Dc_five') == -1){}else{
level = 'Level 5';
}

var mymatch = /\|info =(.*)\|align/.exec(npp_http.responseText);

//level = level + mymatch.length;

  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  p.style.display = 'block';

p.appendChild(document.createTextNode(level));

link_div.appendChild(p);

div.appendChild(link_div);

  var old_div = document.getElementById('p-defcon');
  var side_col = document.getElementById('panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-interaction');
    side_col.insertBefore(div, node);
  }


 }