MozillaZine

how to get URL of Http Auth dialog?

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

Post Posted September 28th, 2005, 2:03 pm

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

BenBasson
Moderator

User avatar
 
Posts: 13671
Joined: February 13th, 2004, 5:49 am
Location: London, UK

Post Posted September 28th, 2005, 3:06 pm

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 Posted September 28th, 2005, 3:20 pm

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 Posted September 28th, 2005, 3:24 pm

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 Posted September 29th, 2005, 8:47 am

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 Posted October 5th, 2005, 1:12 pm

Anyone?

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

Post Posted October 17th, 2005, 10:07 pm

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

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 0 guests