changeset 13:fe30d1d71ee5

Fixed the race-condition bug for selecting the about:news URL text, and also probably prevented about:news from loading in places that it wasn't supposed to.
author Atul Varma <varmaa@toolness.com>
date Sat, 23 Aug 2008 09:15:27 -0700
parents f7c72b3d2bfb
children 2f58ecd38d7a
files ambnews/content/ambnews.js ambnews/content/browser.xul
diffstat 2 files changed, 23 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ambnews/content/ambnews.js	Fri Aug 22 11:10:27 2008 -0700
+++ b/ambnews/content/ambnews.js	Sat Aug 23 09:15:27 2008 -0700
@@ -131,15 +131,28 @@
                                feed.spec, 0, annSvc.EXPIRE_WEEKS);
     }
   },
-  onLoad: function AmbNews_onLoad(aEvent) {
-    if (aEvent.target.baseURI == "about:blank") {
-      var doc = aEvent.target;
-      doc.defaultView.location = "about:news";
-      // TODO: The delay here only works most of the time;
-      // its effectiveness is a race condition and we should
-      // figure out what even to listen for to actually select
-      // the URL bar text consistently
-      window.setTimeout(function() { gURLBar.select(); }, 50);
+  initBrowser: function AmbNews_initBrowser() {
+    // Listen for the addition of new <link> elements on pages.
+    window.addEventListener("DOMLinkAdded", AmbNews.onLinkAdded, false);
+
+    // Whenever the user opens a new tab, have it point to
+    // about:news by default, and select the URL.
+
+    // The following code was taken from the "New Tab Homepage 0.4"
+    // extension's 'chrome/content/tabhomepage.js' file. Thanks,
+    // Ben Basson, for figuring this out for me.
+    gBrowser.removeEventListener("NewTab", BrowserOpenTab, false);
+    function onOpenTab(aEvent) {
+      var newTab = gBrowser.addTab("about:news");
+      gBrowser.selectedTab = newTab;
+      if (gURLBar)
+        setTimeout(function() { gURLBar.select(); }, 0);
+      if (aEvent)
+        aEvent.stopPropagation();
+
+      return newTab;
     }
+    window.BrowserOpenTab = onOpenTab;
+    gBrowser.addEventListener("NewTab", onOpenTab, false);
   }
 };
--- a/ambnews/content/browser.xul	Fri Aug 22 11:10:27 2008 -0700
+++ b/ambnews/content/browser.xul	Sat Aug 23 09:15:27 2008 -0700
@@ -4,11 +4,6 @@
   <script type="application/javascript;version=1.7"
           src="chrome://ambnews/content/ambnews.js"/>
   <script>
-  window.document.getElementById("content").addEventListener(
-    "DOMContentLoaded",
-    AmbNews.onLoad,
-    false
-    );
-  window.addEventListener("DOMLinkAdded", AmbNews.onLinkAdded, false);
+  window.addEventListener("load", AmbNews.initBrowser, false);
   </script>
 </overlay>