Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox addon-sdk - listen for page navigation

I am trying to port a Chrome plugin to Firefox using the addon-sdk and I cannot find an equivalent method to listen to tab navigation events.

What I need to do is keep data per page (detected from the DOM), and remove this as soon as the user navigates to a new page in the tab (but, maintain the data on refresh)

I Chrome, to do something when a tab changes URL, I can use:

chrome.tabs.onUpdated.addListener(function(tab_id, changeInfo, tab) {
    if(changeInfo.status == 'loading' && changeInfo.url) {
        //DO STUFF AS THE URL CHANGED
    }
});

In Firefox using the addon-sdk I have tried using:

tabs.on('open', function(tab){
  tab.on('ready', function(tab){
    if(tab.cachedURL != tab.url) {
      //DO STUFF AND SET CACHE
    }
  });
});

The problem is that I cannot hook into the initial navigation event, so in-between the user starting navigatiion and the DOM of the new page being ready, the old data is available.

Basically I need a way to hook into the initial navigation of a tab and ideally see where it's going (just as I can in Chrome).

Any thoughts?

like image 427
Adam Heath Avatar asked Feb 27 '12 07:02

Adam Heath


1 Answers

At the moment there is no way to do detect page loading with tabs. However you can do it with the start event in page-mods. I'm also interested in doing this the right manner, so please ping me if you find a way without using page-mods:

var pageMod = require("page-mod");
pageMod.PageMod({
    include: "*", // All DOM windows (ie. all pages + all iframes).
    contentScriptWhen: "start", // page starts loading, at this point you have
                                // the head of the document and no more
    contentScript: "", // inject no script, you can even omit this
    onAttach: function onAttach(worker) {
            if (worker.tab.url == worker.url) // test if at top level
                doStuff(worker.tab.url);
            // cleanup the attached worker
            worker.destroy();
        }
    }
);

Also, I don't know about the speed of the onAttach trigger, as with all message passing in ff extensions, it could add some time (maybe 150ms? please come back to me if you have a benchmark on this)

like image 53
BenoitParis Avatar answered Oct 24 '22 17:10

BenoitParis