Jump to content

User:Dr pda/showlistclass.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dr pda (talk | contribs) at 02:21, 20 November 2006 (Added break to stop after finding correct table). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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"
     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(8,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 tables 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
    var captions = tables[i].getElementsByTagName('caption');
    if(captions[0].firstChild.firstChild.nodeValue == 'Heraldry and vexillology articles'){
       //Check that row has not already been added
      if(tables[i].rows[row].cells[0].innerHTML != html){
        var newRow = tables[i].insertRow(row);
        var x = newRow.insertCell(0);
        var y = newRow.insertCell(1);
        x.setAttribute("style",style);
        x.innerHTML = html;
        y.innerHTML = count;
      }
      //If row has been added but count has changed
      else if(tables[i].rows[row].cells[1].innerHTML != count){
        oldcount = tables[i].rows[row].cells[1].innerHTML;
        tables[i].rows[row].cells[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[0];
      var lastTotal = parseInt(lastTotalCell.innerHTML.substring(6)) + count;
      lastTotalCell.innerHTML = 'Total: ' + lastTotal;
      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>