Accessing objects defined in main addon script from content and page scripts in Firefox addons

77 views Asked by At

I am making bootstrapped extension for Firefox (actually, trying to port working Chrome extension). In Chrome it was:

  • Background page holds backgroundApp which is an instance of Marionette.Application and its modules hold Backbone models of data and do storage and sync stuff.
  • Popup page holds popupApp which is another instance of Marionette.Application and its modules take care of UI with views and routers defined in them. To get data, popup uses reference to backgroundApp accessed via chrome.extension.getBackgroundPage().

Now I am having a really hard time finding how I can pass models to popup panel code in Firefox, all messaging mechanisms I've encountered so far only take JSONable data.

1

There are 1 answers

0
the8472 On BEST ANSWER

You will have no joy if you're trying to use javascript frameworks in firefox addons. At least if you're using them beyond the scope of a single window object.

There are multiple different, fairly isolated environments in which scripts run. If we take e10s (multi-process firefox) into consideration then the addon main code will run in the parent process while anything that interacts with page content will run in the content process(es).

Message-passing is the only way to interact between those environments, and while it is possible to have remote proxies for complex objects those cause some considerable performance penalty and their usage is discouraged.

So you can have your backbone/marionette stuff run in a panel or in an invisible page (that's what the background page API does?) but if you want to have them communicate with each other you will have to get your data into some serialiazable shape.

Depending on your needs it might be sufficient if you implement copy constructors for your models. I.e. constructors which optionally take plain javascript objects (bags of values) and re-create the properly typed models from that. This requires that the objects can be fully reconstructed based on their enumerable own-properties.