Search code examples
javascriptfirefox-addonfirefox-addon-sdk

Is communicating between bootstrap.js and main.js possible in a Firefox Add-on SDK extension?


I would like to do some processing in bootstrap.js and then send it to main.js to show UI. Is it possible to communicate between the bootstrap.js file and the main.js file in a Bootstrapped Firefox Add-on?


Solution

  • In a bootstraped Firefox add-on, it is certainly possible to communicate between the bootstrap.js file and any other file within your add-on. In fact, it is normal to do so at least to the extent of setting up other entry points (e.g. buttons, UI controls, etc.), loading modules, etc. for your add-on. The install(), startup(), shutdown(), and uninstall() methods in bootstrap.js are the only entries into your add-on prior to you setting up others (usually set up from startup()).

    From the point of view of how your code is organized, it is unusual for bootstrap.js to contain code beyond what is needed to minimally set up your add-on. In general, the code in bootstrap.js, or called directly by bootstrap.js, should not contain any significant amount of processing, or calculations. Everything that does not need to be done to show the Firefox UI should be delayed until after the Firefox UI is fully initialized and available to the user. The reason for this is that it is much more important to have the main Firefox UI available to the user as rapidly as possible than almost any processing/setup that is not immediately needed to show the Firefox user interface. This includes things like setting up data structures or doing calculations which are not actually used prior to the user interacting with your add-on in some way. It is considered much better for such things to be done once the user clicks on your add-on's button (or otherwise interacts with your add-on) rather than delay showing the user the main Firefox UI to do these tasks.

    On the other hand, it is possible for your entire add-on to be simple enough for the functional code to be contained entirely within the bootstrap.js file. One example is an extension I wrote called Print Button is Print. That extension only changes the Firefox print button to cause a print (old default) instead of a print preview (new default as of FF29). Given the simplicity of the add-on, there was no need for any code beyond the code contained in bootstrap.js.

    As to how, exactly, to pass information from bootstrap.js to functions in other files, that will depend on how you have organized your code and the type or amount of data that you are passing. Passing data can be done by many different methodologies from having the data passed as a parameter to a function (e.g. a function foo(data){...}), to assigning the data to a variable defined in the other file, to storing the data in a preference, to creating an external JSON file which is read in by a different part of the add-on, etc.