Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Listener problems for onlocationchange

5 views
Skip to first unread message

Bryan.paul

unread,
Mar 18, 2008, 3:06:25 PM3/18/08
to
Hello again all. My extension is almost complete, save these two
quirks I can't seem to stamp out. I'm using onLocationChange listener
to trigger a function every time the URL in the address bar changes,
or a tab is switched. It works great except for two things: It won't
unload when I want it to, and it won't detect the very first URL
change.

In the first case, it loads fine and works fine, but when I turn my
extension OFF and call the unload function, the event still triggers
the function I loaded into the listener.

In the second case, If my homepage is google.com for example, and I
load the listener, then switch to another URL, the event will not
fire. Clicking on another link will fire the event, and it works from
here on out. The problem arises, I believe, because the init function
sets the old URL to null, and I check for this and ignore it because
when a new tab is opened, it will try to fire the function twice
because it changes from the old tab URL to NULL, then changes from
NULL to the new tab's URL.

Here is my code:

var EXTEN_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)
{
domWindow = aProgress.DOMWindow;
EXTEN.processNewURL(aURI, domWindow);
},

onStateChange: function() {},
onProgressChange: function() {},
onStatusChange: function() {},
onSecurityChange: function() {},
onLinkIconAvailable: function() {}
};

var EXTEN = {
oldURL: null,

init: function() {

gBrowser.addProgressListener(EXTEN_urlBarListener,
Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
},

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

processNewURL: function(aURI, domWindow) {
if (aURI.spec == this.oldURL || !aURI.spec)
return;
if(domWindow == domWindow.top)
{
this.oldURL = aURI.spec;
EXTEN_Reset(); //MY FUNCTION TO FIRE
}
}
};

I then use the following to load/unload:

gBrowser.addEventListener("load", function() {fixShip.init()},
true); // LOAD
gBrowser.addEventListener("unload", function() {fixShip.uninit()},
true); //UNLOAD

Phew that's a mouthful. I derived the majority of the code from the
moz mdc website ( http://developer.mozilla.org/en/docs/Code_snippets:Progress_Listeners
). Any help at all would be greatly appreciated. Thanks again!

Bryan

0 new messages