User:The Voidwalker/newUserPatrol.js

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-newusers'); if (cur_box !== null) { cur_box.firstChild.firstChild.data = 'New Users (updating)'; }

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

// 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 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 item_name = items[i].getAttribute('title').substring(5); var item_url = 'http://en.wikipedia.org/wiki/Special:Contributions/' + 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 div var div = document.createElement('div'); div.setAttribute('id', 'p-newusers'); div.className = 'portal'; var heading = document.createElement('h3'); heading.appendChild(document.createTextNode('New users')); 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.onclick = nup_disable_box; p.appendChild(a); link_div.appendChild(p);

// now replace the div var old_div = document.getElementById('p-newusers'); var side_col = document.getElementById('mw-panel'); if (old_div !== null) { side_col.replaceChild(div, old_div); } else { var node = document.getElementById('p-logo'); side_col.insertBefore(div, node.nextSibling); //Effectively, "insert after" }

// 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 div var link_div = document.createElement('div'); link_div.className = 'pBody'; var div = document.createElement('div'); div.setAttribute('id', 'p-newusers'); div.className = 'portal'; var heading = document.createElement('h3'); heading.appendChild(document.createTextNode('New users')); 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.onclick = nup_enable_box; p.appendChild(a); link_div.appendChild(p);

// now replace the div var old_div = document.getElementById('p-newusers'); var side_col = document.getElementById('mw-panel'); if (old_div !== null) { side_col.replaceChild(div, old_div); } else { var node = document.getElementById('p-logo'); side_col.insertBefore(div, node.nextSibling); //Effectively, "insert after"); } }

$( nup_init );