Search code examples
firefox-addon-sdk

It is possible to open .html file located in add on resource (data directory)?


I want to have something like a 'result page' for an add-on, an own page of the add-on that will be open and display annotations and other result things. Ideally I would use an .html file located in the data directory of the add-on.

Does the window module provide the ability to open extension's own files?


Solution

  • Usually you will want to open a new tab, not a window. There is no problem opening pages from your data directory, you simply have to use the URL returned by self.data.url():

    var tabs = require("sdk/tabs");
    var self = require("sdk/self");
    tabs.open({
      url: self.data.url("result-page.html"),
      inBackground: false,  // This can also be: inNewWindow: true
    });
    

    This page won't have any special privileges however. In particular, it won't have any access to your add-on's data and it won't be able to exchange messages with your add-on. For that you need to inject a content script into the newly open tab:

    tabs.open({
      url: self.data.url("result-page.html"),
      inBackground: false,
      onReady: function(tab)
      {
        tab.attach({
          contentScriptFile: self.data.url("result-page.js"),
          onMessage: function(message)
          {
            // Message from content script, send a response?
          }
        });
      }
    });
    

    See tab.attach() and Communicating with content scripts.