Some years ago, I made this extension where most of the business logic happens in the background script. It works well when triggered from the custom button showing in the toolbar.
I would now very much like to knock things up a notch and have:
- background script executed as soon as Firefox has finished starting
- open the freshly backed myextension.html
- pin this tab
As of now, I am not quite sure how to detect the event “as soon as Firefox has finished starting”. Initially I had tried to react to browser.windows.onCreated, but it would seem that this event fires too early in the startup sequence.
browser.windows.onCreated.addListener(function () {
//do stuff with bookmarks and tabs
});
The event that I am looking for would be one that would fire as soon as the browser is finally fully armed and loaded, but not before. (Split second later would be acceptable, too).
Isn’t background script executed automatically for each addon when browser starts? (I’ve never even thought about such thing, but it’s possible it’s lazy loaded)
In any case, you can use:
Although, it may not fire when you install or update your addon. For those cases there is:
1 Like
Why does your extension need Firefox to be “fully armed and loaded”?
Or to put it differently, if your extension were to execute its background script, open myextension.html and pin its tab before Firefox is fully armed and loaded, what would the negative consequences be?
Maybe in MV2, but in MV3, you have to rely on browser.runtime.onStartup
and browser.runtime.onInstalled
.
We had a question about this a few months ago: MV3 and Customizable Action Icon and Title
1 Like
Yes, of course, I’ve long tried browser.windows.onCreated.addListener
. It doesn’t work. I hear that it’s in a bug list somewhere. The Mozilla team is now working on it. For years it had been tagged as wontfix
.
myextension.html
is built upon being loaded. It aggregates data it finds in browser.bookmarks
with others previously stored in an indexed database.
I thus need at least those two data sources to be responsive, hence the “fully armed and loaded” which, I admit, might be a bit excessive a phrasing 
To pin the tab, I use browser.tabs.(query|create|move|update)
. If I understand correctly, browser
can only be instanciated in the background.
All in all, what I’m trying to do is to create the tab, pin it, then fill the page it holds, building on what is found in bookmarks
and indexedDB
. Well, the button does it well, I now want to save me that click.
Thus, when I first wrote this extension three or four years ago, I jumped on runtime.onStartup
, which does not work (cf supra). Although I use this extension every day, I’ve left things as is since. It’s now my pet project again.
I’m currently thinking of playing signal ping-pong between fore and back grounds.
Thanks to hints and bits droped in this thread, I’ve finally found my way out this wet paper bag. Strictly speaking, this is not a solution to the exact question as initially worded, but the following gets the job done.
-
backgound.js create
s a new tab, loads it with a minimal extension page, pins it.
This browser.tabs.create
does not respond to any specific event but, I would suppose, executes as soon as loaded: close enough.
-
on DOMContentLoaded
for this extension page, a script traverses browser.bookmarks
and populate the same page accordingly
Today I’ve discovered a more precise answer to juraj.masiar’s question
Isn’t background script executed automatically for each addon when browser starts?
What is actually executed is the function that is associated with the click
event of the browser action button, if, and only if, said function is is invocked on its own, “from column 1”.
I don’t know what happens if no browser_action
is defined in the manifest. Haven’t tried yet.