Skip to content Skip to sidebar Skip to footer

Detecting Navigation In Ff Add Ons

I am porting a Chrome extension to FF using 'FF Addon SDK'. In the background script (main.js) file, I need to use the FF equivalent of... chrome.webNavigation.onBeforeNavigate.add

Solution 1:

unfortunately there isn't any suitable replacement for chrome.webNavigation.onBeforeNavigate.addListener(). Found myself in the same situation a week ago. Tried using nsIWebProgressListener.onStateChange for STATE_START. Didnot work as expected. your best bet would be: 1.) use onLocationChange event, that'd give you the URI of the location that is being loaded. 2.) intercept Httprequest. Filter out one for top level and call it onbeginnavigate.

Let me know if you find other means.

Solution 2:

OK, I managed to figure it out. We need to use the onStateChange listener. To mimic onBeforeNavigation, we need to not only check for STATE_START, but also STATE_IS_DOCUMENT.

var progressListener = {
    QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]),

    onLocationChange: function(aWebProgress, aRequest, aURI) {
        if (aRequest && aURI) {
            console.log('onLocationChange: ' + aURI.spec);
        }
    },

    onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
        var status = Ci.nsIWebProgressListener;

        if(aStateFlags&status.STATE_START && aStateFlags&status.STATE_IS_DOCUMENT) {
            console.log('onStateChange: ' + aRequest.QueryInterface(Ci.nsIChannel).originalURI.spec);
        }
    }
};

Solution 3:

You could attach a onBeforeUnload/onUnload event from your framescript.

Post a Comment for "Detecting Navigation In Ff Add Ons"