Search code examples
firefoxfirefox-addonfirefox-addon-sdk

Load an add-on HTML page on Firefox startup


I have made a plugin using the addon SDK. The plugin adds a button to the nav-bar, and when it is clicked it opens a new tab with some data from an internal indexeddb using code similar to this:

// main.js
tabs.open({
  url: self.data.url('index.html'),
  onReady: runScript
});

function runScript(tab) {
  var worker = tab.attach({
    contentScriptFile: [
      self.data.url("script.js")]
  });
}

Everything works fine, except for the scenario where the user quits Firefox and opens it again, that tab will be restored, but it will contain nothing because it hasn't been triggered by the addon button click. This is because the scripts on the page are loaded through the runScript function in main.js, which is not executed when the HTML file is loaded on a restart.

How can I get this tab to have the same behavior on page startup than on button clicking?


Solution

  • I think you'll have to reload the tab:

    exports.main = function(options) {
      if(options.reason==='startup') for (var i=tabs.length-1; i>=0; i--) {
        var tab = tabs[i];
        if (tab.url!==self.data.url('index.html')) continue;
        tab.once('ready', runScript.bind(null, tab));
        tab.reload();
        /* If it can't reload the tab,
        use tab.url = self.data.url('index.html'); */
      }
      // ...
    }