2006/ack ack.pl,1.8,1.9 check.js,1.3,1.4

Update of /sources/public/2006/ack
In directory hutz:/tmp/cvs-serv2561

Modified Files:
	ack.pl check.js 
Log Message:
Clean up and robustify JavaScript, mainly fixes problems with keeping the
number of links checked up to date and in bounds, as well as avoids
Firefox pause/resume exceptions.  The state of each link is now maintained in
each link object instead of the request, and request objects are cleaned up
more aggressively.


Index: check.js
===================================================================
RCS file: /sources/public/2006/ack/check.js,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- check.js	26 Apr 2006 22:20:14 -0000	1.3
+++ check.js	25 May 2006 21:10:25 -0000	1.4
@@ -11,161 +11,183 @@
 //
 // $Id$
 
-var reqs = new Array();
-var total = 0;
-var done = 0;
-
-var hostid_re = /\/\/([^\/]+)\//;
+var REQS  = new Array();
+var TOTAL = 0;
+var DONE  = 0;
+var BASE  = "grab.pl?";
 
-var base = "grab.pl?";
+function el(id)
+{
+  return id == null ? null : document.getElementById(id);
+}
 
-function setTextContent(el, text)
+function clear(el)
 {
   while (el.firstChild) {
     el.removeChild(el.firstChild);
   }
+}
+
+function setTextContent(el, text)
+{
+  clear(el);
   el.appendChild(document.createTextNode(text));
 }
 
-function hostid(url)
+function getLink(key)
 {
-  var hits = url.match(hostid_re);
-  return (hits == null) ? "__unknown__" + new Date().getTime() : hits[1];
+  return el("L" + key);
+}
+
+function getStatusElement(key)
+{
+  return el("S" + key);
 }
 
 function checkAll()
 {
+  pauseResume(el("pr"));
   var links = document.getElementsByTagName("a");
-  total = links.length;
-  updateCount();
+  TOTAL = links.length;
+  updateCount(0);
   for (var i = 0; i < links.length; i++) {
-    var n = links.item(i);
-    if (n.id == null) continue; if (n.id == "") continue;
-    check(n.href, n.id.substring(1));
+    check(links.item(i));
   }
 }
 
 function pauseResume(el)
 {
+  el.disabled = false;
+  if (el.paused == null) el.paused = true;
   el.paused ? resumeCheck() : pauseCheck();
   el.paused = !el.paused;
   el.value = el.paused ? "Resume" : "Pause";
 }
 
-function updateCount()
+function updateCount(n)
 {
-  var el = document.getElementById("stat");
-  setTextContent(el, "(" + done + " of " + total + " checked)");
+  DONE += n;
+  setTextContent(el("stat"), "(" + DONE + " of " + TOTAL + " checked)");
 }
 
 function pauseCheck()
 {
-  for (var i in reqs) {
-    if (i == null) continue;
-    if (reqs[i] == null) continue;
-    if (reqs[i].readyState < 4) {
-      reqs[i].abort();
-      setTextContent(document.getElementById("S" + i), "paused");
+  for (var key in REQS) {
+    if (REQS[key] == null) continue;
+    if (REQS[key].readyState < 4) REQS[key].abort();
+    REQS[key] = null;
+    var link = getLink(key);
+    if (!link.done) {
+      link.checking = false;
+      setTextContent(getStatusElement(key), "paused");
     }
   }
 }
 
-function resumeCheck()
+function resumeCheck(pr)
 {
   var links = document.getElementsByTagName("a");
   for (var i = 0; i < links.length; i++) {
-    var n = links.item(i);
-    if (n.id == null) continue; if (n.id == "") continue;
-    var key = n.id.substring(1);
-    var req = reqs[key];
-    if (req == null) continue;
-    if (req.readyState < 4) {
-      check(n.href, key);
-    }
+    var link = links.item(i);
+    if (!link.done) check(link);
   }
 }
 
-function check(url, key)
+function check(link)
 {
-  document.getElementById("S" + key).className = "na";
-  var req = reqs[key];
-  if (req == null) {
-    try {
-      req = new XMLHttpRequest();
-    } catch (e) { /* ignore */ }
+  if (link.id == null || link.id == "" || link.checking) return;
+  link.checking = true;
+  var key = link.id.substring(1);
+  getStatusElement(key).className = "na";
+  var req = null;
+  try {
+    req = new XMLHttpRequest();
+  } catch (e) { /* ignore */ }
+  if (req == null)
     try {
       req = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) { /* ignore */ }
+  if (req == null)
     try {
       req = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (e) { /* ignore */ }
-    reqs[key] = req;
-  }
-  //alert(req);
+  REQS[key] = req;
   if (req != null) {
-    var el = document.getElementById("pr");
-    el.value = "Pause"; el.disabled = false;
+    if (link.done) updateCount(-1);
     req.onreadystatechange = function() { check_cb(key); }
     // TODO: s/escape/encodeURIComponent/ ?
-    req.open("HEAD", base + "url=" + escape(url) + ";ref=" + escape(document.getElementById("url").defaultValue), true);
+    req.open("HEAD", BASE + "url=" + escape(link.href) + ";ref=" + escape(el("url").defaultValue), true);
     req.send("");
   }
 }
 
 function check_cb(key)
 {
-  var req = reqs[key];
-  var el = document.getElementById("S" + key);
+  var req = REQS[key];
+  var statusText = getStatusElement(key);
   if (req.readyState == 0) {
-    setTextContent(el, "uninitialized");
+    setTextContent(statusText, "uninitialized");
   }
   else if (req.readyState == 1) {
-    setTextContent(el, "initialized");
+    setTextContent(statusText, "initialized");
   }
   else if (req.readyState == 2) {
-    setTextContent(el, "connected");
+    setTextContent(statusText, "connected");
   }
   else if (req.readyState == 3) {
-    setTextContent(el, "receiving data");
+    setTextContent(statusText, "receiving data");
   }
   else if (req.readyState == 4) {
-    var st = req.getResponseHeader("X-LinkCheck-Status");
+    var reqStatus = null;
+    try {
+      reqStatus = req.status;
+    }
+    catch (e) { /* Ignore, check value of reqStatus instead */ }
+    if (reqStatus == null || reqStatus == 0) { // 0 ~ MSIE sometimes?
+      // Paused/aborted?  Ignore.
+      req = REQS[key] = null;
+      return;
+    }
+
+    var linkStatus = req.getResponseHeader("X-LinkCheck-Status");
     var msg = "complete: " + req.getResponseHeader("X-LinkCheck-Message");
     var redirect = "";
     try {
       redirect = req.getResponseHeader("X-LinkCheck-Redirect");
     } catch (e) { /* duh */ }
+    req = REQS[key] = null;
+
     if (redirect == null || redirect == "") {
-      setTextContent(el, msg);
+      setTextContent(statusText, msg);
     }
     else {
-      while (el.firstChild) {
-        el.removeChild(el.firstChild);
-      }
-      el.appendChild(document.createTextNode(msg));
-      el.appendChild(document.createElement("br"));
-      el.appendChild(document.createTextNode("\u21d2\u00a0")); // "-> "
+      clear(statusText);
+      statusText.appendChild(document.createTextNode(msg));
+      statusText.appendChild(document.createElement("br"));
+      statusText.appendChild(document.createTextNode("\u21d2\u00a0")); // "-> "
       var a = document.createElement("a");
       a.setAttribute("href", redirect);
       a.appendChild(document.createTextNode(redirect));
-      el.appendChild(a);
+      statusText.appendChild(a);
     }
 
-    if (req.status != 200 || st > 399) {
-      el.className = "error";
+    if (reqStatus != 200 || linkStatus > 399) {
+      statusText.className = "error";
     }
-    else if (st > 299) {
-      el.className = "redirect";
+    else if (linkStatus > 299) {
+      statusText.className = "redirect";
     }
     else {
-      el.className = "ok";
+      statusText.className = "ok";
     }
-    req = reqs[key] = null;
-    done++;
-    updateCount();
-    if (done >= total) {
-      var pr = document.getElementById("pr");
+    var link = getLink(key);
+    link.done = true;
+    link.checking = false;
+    updateCount(1);
+    if (DONE >= TOTAL) {
+      var pr = el("pr");
       pr.value = "Completed";
+      pr.paused = false;
       pr.disabled = true;
     }
   }

Index: ack.pl
===================================================================
RCS file: /sources/public/2006/ack/ack.pl,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- ack.pl	25 May 2006 15:21:46 -0000	1.8
+++ ack.pl	25 May 2006 21:10:25 -0000	1.9
@@ -119,7 +119,7 @@
     my $l = length($_);
     my $foo = encode_entities($l > ML ?
       substr($_, 0, ML/2) . "[...]" . substr($_, $l - ML/2) : $_);
-    print "<tr><td><a id=\"L$md5\" href=\"$url\" onclick=\"check(this.href,'$md5');return false\">$foo</a>";
+    print "<tr><td><a id=\"L$md5\" href=\"$url\" onclick=\"check(this);return false\">$foo</a>";
     print "&nbsp;($links{$_})" if ($links{$_} > 1);
     print "</td><td class=\"na\" id=\"S$md5\">uninitialized</td></tr>\n";
   }

Received on Thursday, 25 May 2006 21:10:31 UTC