3.6.05 # Firefox Download Counter

/2005/06/firefox.jpgFirefox is without doubt a remarkable, innovative web browser.

The browsers actual download number is available on Spread Firefox as an RSS-Feed — a small XML-File, which is updated every minute.

I decided to implement a small web application on top of that service. Well in fact I needn't to, since the smart people at Infocraft already did it in an excellent manner . So I implemented a stripped down version, just to learn, how it is working. You can see it in action in the box titled Firefox growth in the left column. Please note, that it's a little boring and not as pleasent as the original. So just sit and look at it for a while in order to see the numbers changing every minute.

The web appliction consists of a javascript fragment and a php file ffcounter.php. The javascript code uses the XmlHttpRequest object to poll the actual download number from my server.

function getHttpRequest(uri, callback) {
  var http = false;

  if (!!window.XMLHttpRequest) { 
    try { http = new XMLHttpRequest(); } 
    catch (e) { http = false; } 
  }
  else if (!!window.ActiveXObject) { 
    try {http = new ActiveXObject("Msxml2.XMLHTTP"); } 
    catch (e) { 
      try { http = new ActiveXObject("Microsoft.XMLHTTP"); } 
      catch (e) { http = false; }
    }
  }

  if (http) {
    http.onreadystatechange = function() {
      if (http.readyState == 4 && 
        http.status == 200 && callback)
        callback(http.responseText);
    };
    http.open("GET", uri, true);
    http.send(null);
  }
}

var ff;
function count() {
  getHttpRequest("/bin/ffcounter.php", 
                 function(res) {
                   document.getElementById("ffc").innerHTML = ff ? ff : res; 
                   document.getElementById("ffi").innerHTML = ff ? res-ff : 0;
                   ff = res; 
                 });
  setTimeout("count()", 60000); // wait one minute after each refresh ..
}

if (window.addEventListener) window.addEventListener("load", count, false);
else if (window.attachEvent) window.attachEvent("onload", count);

The setTimeout method is responsible for polling the updated download number every minute from my server. I cannot obtain the RSS feed directly from its original location because of security restrictions of the XmlHttprequest object.

This is where the php file ffcounter.php comes to the rescue. It is nearly identical with that on Infocraft . Instead of transfering the complete XML file to the web client, I am using a little regular expression to extract the download number. Here is the modified code.

<?php 
$host = "www.spreadfirefox.com"; 
$path = "/download_counter.php?ff=1"; 
$fp = fsockopen($host, 80, $errno, $errstr, 30); 
if ($fp) { 
   $out  = "GET $path HTTP/1.1\r\n"; 
   $out .= "Host: $host\r\n"; 
   $out .= "Connection: Keep-Alive\r\n\r\n"; 
   fwrite($fp, $out); 
   while (!feof($fp)) { 
      $buffer .= fgets($fp, 128); 
   }
   fclose($fp); 
   header('Content-Type: text/plain');
   header('Expires: Fri, 01 Jun 2005 00:00:00 GMT'); 
   if (preg_match("/<description>([0-9,]+)<\/description>/", $buffer, $matches))
      print($matches[1]);
} 
else { 
   print("$errstr ($errno)\n"); 
} 
?>

Feel free to use these scripts for whatever, as it is noted at Infocraft , where — again — the credits should go. I tested it with Firefox and IE6.

Update: Feed url had changed and was corrected!

Update: Finally I removed the script from my sidebar.

0 comments