User:Dr pda/showlistclass.js
Appearance
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. |
![]() | Documentation for this user script can be added at User:Dr pda/showlistclass. |
//<pre><code>
//This function displays the count of List-Class Heraldry and Vexillology items
//It only works on the page /wiki/Wikipedia:WikiProject_Heraldry_and_vexillology/Assessment
//It also assumes that there are less than 200 pages in the category
//To use this, copy and paste {{subst:js|User:Dr_pda/showlistclass.js}} into your monobook.js
function loadXMLDoc(url,handler)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = function () {handler(req)};
req.open("GET", url, true);
req.send("");
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = function () {handler(req)};
req.open("GET", url, true);
req.send("");
}
}
}
function addListClass(req) {
// only if req shows "loaded"
//alert(req.readyState);
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
response = req.responseText;
targetPara = response.match(/There are \d+ pages in this section of this category/) + '';
countList = parseInt(targetPara.substring(9));
addClassCell(9,countList,"background: lightblue; text-align: center",'<b><a href="/wiki/Category:List-Class_heraldry_and_vexillology_articles">List</a></b>');
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
function addClassCell(row,count,style, html){
//Get list of <table>s in the HTML document
var tables = document.getElementsByTagName('table');
//Loop over all tables
for(var i=0; i<tables.length; i++){
//Check that it is the right table
if(tables[i].rows[0].cells[0].innerHTML.match('Heraldry and vexillology')){
var nCells = tables[i].rows[row-1].cells.length;
//Check that row has not already been added
if(tables[i].rows[row].cells[0].innerHTML != html){
tables[i].rows[2].cells[0].rowSpan = "10";
var newRow = tables[i].insertRow(row);
var x = newRow.insertCell(0);
for(var j=1;j<nCells-2; j++){
newRow.insertCell(j);
}
var y = newRow.insertCell(nCells-2);
var z = newRow.insertCell(nCells-1);
x.setAttribute("style",style);
x.innerHTML = html;
y.innerHTML = count;
z.innerHTML = '<b>' + count + '</b>';
}
else if(tables[i].rows[row].cells[nCells-2].innerHTML != count){
oldcount = tables[i].rows[row].cells[nCells-2].innerHTML;
tables[i].rows[row].cells[nCells-2].innerHTML = count;
tables[i].rows[row].cells[nCells-1].innerHTML = count;
//convert new count to difference, so the code for calculating the
//total still works
count -= oldcount;
}
else{
return;
}
var lastRow = tables[i].rows.length - 1;
var lastTotalCell = tables[i].rows[lastRow].cells[nCells-2];
var lastTotal = parseInt(lastTotalCell.innerHTML.substring(3)) + count;
tables[i].rows[lastRow].cells[nCells-2].innerHTML = '<b>' + lastTotal + '</b>';
tables[i].rows[lastRow].cells[nCells-1].innerHTML = '<b>' + lastTotal + '</b>';
break;
}
}
}
addOnloadHook(function () {
if(document.location.pathname.match('/wiki/Wikipedia:WikiProject_Heraldry_and_vexillology/Assessment')){
loadXMLDoc("/wiki/Category:List-Class_heraldry_and_vexillology_articles",addListClass);
}
});
//</code></pre>