User:TheJosh/Scripts/NewPagePatrol.js
Appearance
< User:TheJosh | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:TheJosh/Scripts/NewPagePatrol. |
var npp_http;
addOnloadHook( npp_init );
/* initalise */
function npp_init() {
var ret = npp_init_ajax();
if (ret == false) return false;
npp_http.onreadystatechange = function() {
if(npp_http.readyState == 4) npp_ajax_response();
}
npp_ajax_request();
}
/* init ajax */
function npp_init_ajax() {
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;
}
}
}
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)';
}
// 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);
}