how to get URL of Http Auth dialog?

Talk about add-ons and extension development.
Post Reply
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

how to get URL of Http Auth dialog?

Post by ericjung »

Hi,
I'm trying to get the url which initiates FF/Mozilla to display the HTTP Basic Authentication dialog (like this). For example, if the user navigates to http://passwordmaker.org:2082, I'd like to get http://passwordmaker.org:2082.

I have this code which seems close, but always gets about:blank. Anyone know what I'm doing wrong?

Code: Select all

var gWindowManager =  Components.classes['@mozilla.org/appshell/window-mediator;1']
.getService(Components.interfaces.nsIWindowMediator);

var gWindowManagerListener = {
  onOpenWindow: function(aXULWindow) {
    var docShell = aXULWindow.docShell;
    var requestor = docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
    var chromeWindow = requestor.getInterface(Components.interfaces.nsIDOMWindow);
    var url = chromeWindow.document.location;
    dump("url=" + url + "\n");
  },
  onCloseWindow: function(aXULWindow) {},
  onWindowTitleChange: function(aXULWindow, aWindowTitle) {}
};

gWindowManager.addListener(gWindowManagerListener);


Thanks in advance for any help,
grimholtz
User avatar
BenBasson
Moderator
Posts: 13671
Joined: February 13th, 2004, 5:49 am
Location: London, UK
Contact:

Post by BenBasson »

The initial location of chromeWindow will be "about:blank", since that's what Firefox initialises first, replacing it shortly after with the document at the desired location. To be honest, this is a complete pain and I'm not sure where to go from here, but that's what's happening.
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

What if I added an event listener for the "load" event? Would the url be available in the callback, do you think? Should I try it by adding an event listener to the chromeWindow variable I defined above?

Thanks for any ideas.
old Captain Caveman
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by old Captain Caveman »

Lame fix then maybe (got to start somewhere), but if the chromewindow itself is already initialized at that point. You might do a setTimeout on it, and read the location a bit later?
Or... Maybe you can add an onchange event listener to the urlbar, that should also already be on the window?

Just some thoughts...

Or as you where typing yourself, try and add an onload to the window.
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

Hi Cpt. Cavman,

I tried my idea of adding an onload handler to the window. It doesn't get called. How can I add an onchange event listener to the urlbar?

FWIW, here's what I tried re: window onload handler

Code: Select all

init : function() {
  const gWindowManager =
  Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);
   
  const gWindowManagerListener = {
     onOpenWindow: function(aXULWindow) {
       var docShell = aXULWindow.docShell;
       var requestor = docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
       var chromeWindow = requestor.getInterface(Components.interfaces.nsIDOMWindow);
       chromeWindow.addEventListener("load", this.onNewWnd, true);
     },
     onCloseWindow: function(aXULWindow) {},
     onWindowTitleChange: function(aXULWindow, aWindowTitle) {}
  };
   
  gWindowManager.addListener(gWindowManagerListener);
},
onNewWnd : function(aEvent) {
  dump(aEvent + "\n");
  var wnd = aEvent.currentTarget;
  dump(wnd + "\n");
}
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

Anyone?
azzer
Posts: 244
Joined: September 13th, 2005, 9:56 pm
Contact:

Post by azzer »

Have a look at http://forums.mozillazine.org/viewtopic.php?t=319831 . The relevant part is the code that handles notifications of DOM windows opening/closing. In the observe callback, you'll find the current url from subject._content.location.href (subject is the opened window). But note the setTimeout() with a timeout of 0, which invokes the observe() method again, 'immediately'; I think the point of that is reschedule the call, perhaps on another thread. I'm really fuzzy on that, but it works and it's not really a hack since there's no delay.

Hmm. having said all that, I'm having problems with that code under ff/tb 1.5 - the domwindowopened event doesn't seem to fire (perhaps the listener you use would work better). Anyway, it's worth a shot.

Andy
Post Reply