Jump to content

User:Writ Keeper/Scripts/recentChangesPatrol.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.
/*
 This script was originally written by TheJosh; I've only copied it and debugged it for new changes.  Nearly all credit for this should go to TheJosh.
 Note that I've tweaked this for the vector skin; dunno if it'll still display well (or at all) in others.
 */

var rcp_http;
var rcp_enabled;
var rcp_num_pages;
var rcp_refresh;

$(document).ready( rcp_init );

/* initalise */
function rcp_init() {

  // allow user settings through
  if(rcp_enabled == null) {
    rcp_enabled = false;
  }
  if(rcp_num_pages == null) {
    rcp_num_pages = 10;
  }
  if(rcp_refresh == null) {
    rcp_refresh = 5;
  }

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

  // get our cookie
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf("rcp_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") {
        rcp_enabled = true;
      } else {
        rcp_enabled = false;
      }
    } 
  }

  // Either make a request or show nothing
  if (rcp_enabled == true) {
    rcp_ajax_request();
  } else {
    rcp_draw_disabled_box();
  }
}

/* init ajax */
function rcp_create_request() {
  try {
    rcp_http = new XMLHttpRequest();

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

  rcp_http.onreadystatechange = function() {
    if(rcp_http.readyState == 4) rcp_ajax_response();
  }

  return true;
}

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

  // firstly, inform the user
  var cur_box = document.getElementById('p-recentchanges');
  if (cur_box != null) {
    cur_box.firstChild.firstChild.data = 'Recent Changes (updating)';
  }

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

  // Then make the request
  rcp_http.open("GET", "http://en.wikipedia.org/w/api.php?action=query&list=recentchanges&format=xml&rcnamespace=0&rcshow=!bot&rcshow=!minor&rclimit=" + rcp_num_pages, true);
  rcp_http.send(null);
}

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

  var items = rcp_http && rcp_http.responseXML && rcp_http.responseXML.getElementsByTagName('rc');

  // create the div that holds all the recentchanges links
  var link_div = document.createElement('div');
  link_div.className = 'body';
  var list = document.createElement('ul');
  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');
    var item_url = 'http://en.wikipedia.org/w/index.php?diff=cur&oldid=prev&title=' + escape(item_name);

    a = document.createElement('a');
    a.setAttribute('href', item_url);
    a.setAttribute('style', '"font-size=small"');
    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-recentchanges');
  div.className = 'portal persistent';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode('Recent Changes'));
  div.appendChild(heading);
  div.appendChild(link_div);

  // 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.setAttribute("onclick", "rcp_disable_box(); return;");
  a.setAttribute("href", "#");
  p.appendChild(a);
  link_div.appendChild(p);

  // now replace the div
  var old_div = document.getElementById('p-recentchanges');
  var side_col = document.getElementById('mw-panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById("p-interaction");
    side_col.insertBefore(div, node);
  }

  // and do it again in 5 secs
  setTimeout("rcp_ajax_request()", rcp_refresh * 1000);
}

function rcp_disable_box() {
  rcp_enabled = false;
  rcp_draw_disabled_box();
  document.cookie = "rcp_show_box=no; path=/";
}

function rcp_enable_box() {
  rcp_enabled = true;
  document.cookie = "rcp_show_box=yes; path=/";
  rcp_ajax_request();
}

function rcp_draw_disabled_box() {
  // Container div
  var link_div = document.createElement('div');
  link_div.className = 'pBody';
  var div = document.createElement('div');
  div.setAttribute('id', 'p-recentchanges');
  div.className = 'portal persistent';
  var heading = document.createElement('h5');
  heading.appendChild(document.createTextNode('Recent Changes'));
  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('enable this box'));
  a.setAttribute("onclick", "rcp_enable_box(); return;");
  a.setAttribute("href", "#");
  p.appendChild(a);
  link_div.appendChild(p);

  // now replace the div
  var old_div = document.getElementById('p-recentchanges');
  var side_col = document.getElementById('mw-panel');
  if (old_div != null) {
    side_col.replaceChild(div, old_div);
  } else {
    var node = document.getElementById("p-interaction");
    side_col.insertBefore(div, node);
  }
}