Search code examples
javascriptfirefox-addonfirefox-addon-sdkfirefox-addon-restartless

Initial tab doesn't fire open, ready events


I'm build an SDK Firefox Add-on that is supposed to read a tab's URL and parse it. To this end, I'm listening to the 'ready' event in lib/main.js,

var tabs = require('sdk/tabs');
tabs.on('open', function(tab){
  tab.on('ready', function(tab){
    console.log(tab.url);
  });
});

as described in Mozilla's documentation.

When debugging with cfx run, this appears to work well for new tabs. The tab that's already open on cfx run, however does not fire the open and ready events.

What's the reason for this and how to fix it?


Solution

  • To list all tabs that were open at the moment of loading the addon you could just use the tabs object you got after requiring sdk/tabs

    var tabs = require('sdk/tabs');
    for (var tab of tabs) {
      console.log(tab.url);
    }
    

    So the code from your example could be transformed to something like this:

    var tabs = require('sdk/tabs');
    
    for (let tab of tabs) {
      processTab(tab);
    }
    
    tabs.on('open', function(tab){
      tab.on('ready', processTab);
    });
    
    function processTab(tab) {
      console.log(tab.url);
    }