views:

172

answers:

1

Hi, I'm using the nsIWebProgressListener interface to find out if a url has been changed. If it has, I'd like to rewrite the link. Here's a snippet (code taken from the bottom of the page from the link above)


var myExt_urlBarListener = {
  QueryInterface: function(aIID)
  {
   if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
       aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
       aIID.equals(Components.interfaces.nsISupports))
     return this;
   throw Components.results.NS_NOINTERFACE;
  },

  onLocationChange: function(aProgress, aRequest, aURI)
  {
    myExtension.processNewURL(aURI);
  },

  onStateChange: function(a, b, c, d) {},
  onProgressChange: function(a, b, c, d, e, f) {},
  onStatusChange: function(a, b, c, d) {},
  onSecurityChange: function(a, b, c) {}
};

var myExtension = {
  oldURL: null,

  init: function() {
    // Listen for webpage loads
    gBrowser.addProgressListener(myExt_urlBarListener,
        Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
  },

  uninit: function() {
    gBrowser.removeProgressListener(myExt_urlBarListener);
  },

  processNewURL: function(aURI) {
    if (aURI.spec == this.oldURL)
      return;

    // now we know the url is new...
    start_work(aURI.spec);
    this.oldURL = aURI.spec;
  }
};

window.addEventListener("load", function() {myExtension.init()}, false);
window.addEventListener("unload", function() {myExtension.uninit()}, false);

And a function to handle the change:


function start_work(url)
{
    result = check(url);
    if (result) {
            setCookie('bws', 'true', 1, '/');
            window.location = result; // or window.location.replace, doesn't matter
        }
}

Here's what happens! alt text As you can see the entire browser/address bar/chrome disappeared!

Any help on this?

+1  A: 

simple: add window.content.location

daniel
Yes. In this case, his code is in the chrome context, so window points to the chrome window which is the browser UI.
sdwilsh