Jump to content

User:TheJosh/Scripts/NewPagePatrol.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TheJosh (talk | contribs) at 14:38, 12 September 2007 (structure changes). 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.
var npp_http;

addOnloadHook( npp_init );

/* initalise */
function npp_init() {
  npp_ajax_request();
}

/* init ajax */
function npp_create_request() {
  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) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }

  npp_http.onreadystatechange = function() {
    if(npp_http.readyState == 4) npp_ajax_response();
  }

  return true;
}

/* make a request */
function npp_ajax_request() {
  // firstly, inform the user
  var cur_box = document.getElementById('p-newpages');
  if (cur_box != null) {
    cur_box.firstChild.firstChild.data = 'New Pages (updating)';
  }

  if (npp_create_request () == false) {
    if (cur_box != null) {
      cur_box.firstChild.firstChild.data = 'New Pages (update failed)';
    } else {
      alert ("There seems to be a problem using the NewPagePatrol script. I can't make AJAX objects, so I'm just going to complain. God Bless!");
    }
  }

  // Then make the request
  npp_http.open("GET", "/w/index.php?title=Special:Newpages&feed=rss&limit=10", true);
  npp_http.send(null);
}

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

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

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

  // populate the list with 10 links.
  for (var i = 0; i < items.length; i++) {
    var nodes = items[i].getElementsByTagName('title');
    var item_name = nodes[0].firstChild.data;
    nodes = items[i].getElementsByTagName('link');
    var item_url = nodes[0].firstChild.data;

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

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

  // Container div
  var div = document.createElement('div');
  div.setAttribute('id', 'p-newpages');
  div.className = 'portlet';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode('New pages'));
  div.appendChild(heading);
  div.appendChild(link_div);

  // now replace the div
  var old_div = document.getElementById('p-newpages');
  var side_col = document.getElementById('column-one');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById('p-search');
    side_col.insertBefore(div, node);
  }

  // and do it again in 5 secs
  setTimeout("npp_ajax_request()", 5000);
}