Jump to content

User:Dr pda/showlistclass.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.
 //<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>