Jump to content

User:Oshwah/NewUserPatrol.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
var nup_http; 
var nup_enabled;
var nup_num_pages;
var nup_refresh;


/* initalise */
function nup_init() {

  // allow user settings through
  if(nup_enabled === undefined) {
    nup_enabled = false;
  }
  if(nup_num_pages === undefined) {
    nup_num_pages = 10;
  }
  if(nup_refresh === undefined) {
    nup_refresh = 5;
  }

  // A few limits to be nice to the servers
  if (nup_num_pages > 50) {
    nup_num_pages = 50;
  }
  if (nup_num_pages < 1) {
    nup_num_pages = 1;
  }
  if (nup_refresh < 2) {
    nup_refresh = 2;
  }

  // get our cookie
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf("nup_show_box=");
    if (c_start != -1) {
      c_start = c_start + 13;
      var c_end = document.cookie.indexOf(";", c_start);
      if (c_end == -1) {
        c_end = document.cookie.length;
      }

      if (document.cookie.substring(c_start, c_end) == "yes") {
        nup_enabled = true;
      } else {
        nup_enabled = false;
      }
    }
  }

  // Either make a request or show nothing
  if (nup_enabled === true) {
    nup_ajax_request();
  } else {
    nup_draw_disabled_box();
  }
}

/* init ajax */
function nup_create_request() {
  try {
    nup_http = new XMLHttpRequest();

  } catch (e) {
    try {
      nup_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        nup_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  nup_http.onreadystatechange = function() {
    if(nup_http.readyState == 4) nup_ajax_response();
  };

  return true;
}

/* make a request */
function nup_ajax_request() {
  // check we are enabled
  if (nup_enabled === false) return;

  // firstly, inform the user
  var cur_box = document.getElementById('p-newuserpatrolscript');
  if (cur_box !== null) {
    cur_box.firstChild.firstChild.data = 'New users (...)';
  }

  if (nup_create_request () === false) {
    if (cur_box !== null) {
      cur_box.firstChild.firstChild.data = 'New users (Update FAILED!)';
    } else {
      alert ("Error with AJAX object creation.");
    }
  }

  // Then make the request
  nup_http.open("GET", "/w/api.php?action=query&list=logevents&leaction=newusers/create&format=xml&lelimit=" + nup_num_pages, true);
  nup_http.send(null);
}

/* we have received a response */
function nup_ajax_response() {

  var items = nup_http.responseXML.getElementsByTagName('item');

  // create the nup_div that holds all the newpage links
  var nup_link_div = document.createElement('div');
  nup_link_div.className = 'pBody';
  var list = document.createElement('ul');
  nup_link_div.appendChild(list);

  // populate the list with 10 links.
  for (var i = 0; i < items.length; i++) {
    var item_name = items[i].getAttribute('title').substring(5);
    var item_url = 'https://en.wikipedia.org/wiki/Special:Contributions/' + encodeURIComponent(item_name);

    a = document.createElement('a');
    a.style.fontSize = 'small';
    a.setAttribute('href', item_url);
    a.appendChild(document.createTextNode(item_name));

    var li = document.createElement('li');
    li.appendChild(a);
    list.appendChild(li);
  }

  // Container nup_div
  var nup_div = document.createElement('div');
  nup_div.setAttribute('id', 'p-newuserpatrolscript');
  nup_div.className = 'portal';
  var nup_heading = document.createElement('h6');
  nup_heading.appendChild(document.createTextNode('New users'));
  nup_div.appendChild(nup_heading);
  nup_div.appendChild(nup_link_div);
  window.console.log("[NEW-USER-PATROL]: Object created!");
  
  // disable link
  var p = document.createElement('p');
  p.style.fontSize = 'x-small';
  p.style.margin = '0px';
  p.style.textAlign = 'right';
  a = document.createElement('a');
  a.appendChild(document.createTextNode('disable this box'));
  a.onclick = nup_disable_box;
  p.appendChild(a);
  nup_link_div.appendChild(p);

  // now replace the nup_div
  var nup_old_div = document.getElementById('p-newuserpatrolscript');
  var nup_side_col = document.getElementById('mw-panel');
  if (nup_old_div !== null) {
    nup_side_col.replaceChild(nup_div, nup_old_div);
	window.console.log("[NEW-USER-PATROL]: Object updated!");
  } else {
    var node = document.getElementById('p-tb');
    nup_side_col.insertBefore(nup_div, node.nextSibling); //Effectively, "insert after"
	window.console.log("[NEW-USER-PATROL]: Object inserted for the first time!");
  }

  // and do it again in 5 secs
  setTimeout(nup_ajax_request, nup_refresh * 1000);
}

function nup_disable_box() {
  nup_enabled = false;
  nup_draw_disabled_box();
  document.cookie = "nup_show_box=no; path=/";
}

function nup_enable_box() {
  nup_enabled = true;
  document.cookie = "nup_show_box=yes; path=/";
  nup_ajax_request();
}

function nup_draw_disabled_box() {
  // Container nup_div
  var nup_link_div = document.createElement('div');
  nup_link_div.className = 'pBody';
  var nup_div = document.createElement('div');
  nup_div.setAttribute('id', 'p-newuserpatrolscript');
  nup_div.className = 'portal';
  var nup_heading = document.createElement('h6');
  nup_heading.appendChild(document.createTextNode('New users (Disabled)'));
  nup_div.appendChild(nup_heading);
  nup_div.appendChild(nup_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('enable this box'));
  a.onclick = nup_enable_box;
  p.appendChild(a);
  nup_link_div.appendChild(p);

  // now replace the nup_div
  var nup_old_div = document.getElementById('p-newuserpatrolscript');
  var nup_side_col = document.getElementById('mw-panel');
  if (nup_old_div !== null) {
    nup_side_col.replaceChild(nup_div, nup_old_div);
  } else {
    var node = document.getElementById('p-tb');
    nup_side_col.insertBefore(nup_div, node.nextSibling); //Effectively, "insert after"
  }
}

addOnloadHook( nup_init );