Jump to content

User:Barras/prosesize.js

From Simple English Wikipedia, the free encyclopedia
Revision as of 20:45, 26 December 2006 by Dr pda (talk | changes) (commenting out images (won't work because on different domain), added debug output)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//<pre>
//This function adds a link to the toolbox which, when clicked, displays the size of the page
//and the size of the prose. See the talk page for more details.
//
//This function requires the addLink function. If you do not already have it in your monobook.js,
//add {{subst:js|Wikipedia:WikiProject User scripts/Scripts/addLink}}
//
//To use this function add {{subst:js|User:Dr pda/prosesize.js}} to your monobook.js
//
function loadXMLDoc(url,handler)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    // 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 processReqChange(req) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
	 response = req.responseText;
         var startindex = response.indexOf('&#x00BB;');
	 startindex = response.indexOf('href="'+location.pathname,startindex);
	 var endindex = response.indexOf('</li>',startindex);
	 if(startindex > -1){
	   searchresult = response.substr(startindex,endindex-startindex);
	   result = searchresult.match(/\d+\.\d kB \(\d+ words\)/);
	    wiki_value = document.createElement("li");
	    wiki_value.id = "wiki-size";
	    wiki_value.innerHTML = '<b>Wiki text :</b>'+result;
	    var output = document.getElementById("document-size-stats");
	    ref_html_value = document.getElementById("ref-size-html");
	    output.insertBefore(wiki_value,ref_html_value);

	 }
	 else{
	  alert("Could not find article on first page of Wikipedia search. No values for wiki text.");
	 }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function getFileSize(url,id){
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    // branch for IE/Windows ActiveX version
     else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if(req){
        req.open("HEAD", url, false);
        req.send(null);
	var fileSize = parseInt(req.getResponseHeader("Content-Length"))/1024;
	return (fileSize > 0) ? fileSize : 0*1;
    }
}

function getLength(id){
 var textLength = 0;
 for(var i=0;i<id.childNodes.length; i++){
  if(id.childNodes[i].nodeName == '#text'){
   textLength += id.childNodes[i].nodeValue.length;
  }
  else{
    textLength += getLength(id.childNodes[i]);
  }
 }
 return textLength;
}

function getWordCount(id){
 var numWords = 0;
 for(var i=0;i<id.childNodes.length; i++){
  if(id.childNodes[i].nodeName == '#text'){
  numWords += id.childNodes[i].nodeValue.split(' ').length;
  }
  else{
    numWords += getWordCount(id.childNodes[i]);
  }
 }
 return numWords;
}

function getDocumentSize(){
 var output = document.createElement("ul");
 output.id = "document-size-stats";

 var total_value = document.createElement("li");
 total_value.id = "total-size";
 output.appendChild(total_value);

 var prose_html_value = document.createElement("li");
 prose_html_value.id = "prose-size-html";
 output.appendChild(prose_html_value);

 var prose_value = document.createElement("li");
 prose_value.id = "prose-size";
 output.appendChild(prose_value);

 var ref_html_value = document.createElement("li");
 ref_html_value.id = "ref-size-html";
 output.appendChild(ref_html_value);

 var ref_value = document.createElement("li");
 ref_value.id = "ref-size";
 output.appendChild(ref_value);

 //var image_value = document.createElement("li");
 //image_value.id = "image-size";
 //output.appendChild(image_value);

 var dummy = document.getElementById("siteSub");
 dummy.parentNode.insertBefore(output, dummy.nextSibling);

 var header = document.createElement("span");
 header.innerHTML = '<br/>Document statistics: <small><i>(See <a href="http://en.wikipedia.org/wiki/User_talk:Dr_pda/prosesize.js">here</a> for details.)<i></small>';
 dummy.parentNode.insertBefore(header,output);

 var total = getFileSize(location.pathname,"total-size");
 total_value.innerHTML='<b>File size: </b>'+total.toFixed(0)+'&nbsp;kB';
/*
// COMMENTING OUT IMAGES
// Since they are hosted at wikimedia.org, XMLHttpRequest won't work

 //Get size of images
 var bodyContent = document.getElementById("bodyContent");
 var iList = bodyContent.getElementsByTagName("img");

 //Get size of images included in document
 var image_size = 0;
 var first_magnify = true;

 for (var i=0;i<iList.length; i++){
  var im = iList[i];
  var url = im.getAttribute("src");
  if(url.indexOf("magnify-clip.png") != -1){
    if(first_magnify){
      image_size += getFileSize(url,"image-size");
      first_magnify = false;
    }
  }
  else{
    image_size += getFileSize(url,"image-size");
  }
 }
 image_value.innerHTML='<b>Images: </b>'+image_size.toFixed(0)+'&nbsp;kB';
*/
 //Calculate prose size
 var pList = bodyContent.getElementsByTagName("p");

 prose_size = 0;
 prose_size_html = 0;
 word_count = 0;
 for(var i=0;i<pList.length; i++){
   var para = pList[i];
   prose_size += getLength(para);
   prose_size_html += para.innerHTML.length;
   word_count += getWordCount(para);
   para.style.cssText = "background-color:yellow";
 }

 var rmList = bodyContent.getElementsByTagName("sup");

 refmark_size = 0;
 refmark_size_html = 0;
 for(var i=0;i<rmList.length; i++){
   var refmark = rmList[i];
   if(refmark.className == "reference"){
     refmark_size += getLength(refmark);
     refmark_size_html += refmark.innerHTML.length;
   }
 }

 prose_value.innerHTML='<b>Prose size (text only): </b>'+((prose_size-refmark_size)/1024).toFixed(0)+'&nbsp;kB ('+word_count+' words)';
 prose_html_value.innerHTML='<b>Prose size (HTML): </b>'+((prose_size_html-refmark_size_html)/1024).toFixed(0)+'&nbsp;kB';

 //Calculate size of references
 var rList = bodyContent.getElementsByTagName("ol");
 var ref_size = 0;
 var ref_size_html = 0;
 for (var i=0; i<rList.length; i++){
   if(rList[i].className == "references"){
     ref_size = getLength(rList[i]);
     ref_size_html = rList[i].innerHTML.length;
   }
 }
 ref_value.innerHTML='<b>References (text only): </b>'+((ref_size+refmark_size)/1024).toFixed(0)+'&nbsp;kB';
 ref_html_value.innerHTML='<b>References (HTML): </b>'+((ref_size_html+refmark_size_html)/1024).toFixed(0)+'&nbsp;kB';

 //Get size as returned by Wikipedia search
 var encodedURL = escape(location.pathname.substr(6).replace(/\s/g,'+'));
 var searchURL = '/wiki/Special:Search?search=' + encodedURL + '&fulltext=Search';
 alert(searchURL);
 loadXMLDoc('testsearch.html',processReqChange)
}

addOnloadHook(function () {
  addLink('p-tb', 'javascript:getDocumentSize()', 'Page size', 't-page-size', 'Calculate page and prose size', '', '');
});

//</pre>