Does a web extension need to explicitly load content scripts? Does a web extension need to explicitly load content scripts? google-chrome google-chrome

Does a web extension need to explicitly load content scripts?


Short answer: it's needed in Chrome but not Firefox.


Chrome does not load content scripts into matching pages at extension load (which includes extension updates, not just initial load).

So if you want content script functionality in tabs open at extension load (as opposed to future navigations), this code (or similar) is needed.

You can modernize this code a bit with chrome.tabs.query.


Firefox is incompatible with Chrome in this regard: it does automatically inject content script in everything that matches at load time. So you should employ some browser detection and/or inject-only-once guard code.

I wish they didn't introduce that as a breaking change. It would make sense to at least provide a manifest key to choose the behaviour for ease of transition.


Note: in an extension reload scenario content scripts from old instance continue to exist (but extension APIs will fail); it's your responsibility to handle that gracefully.


You can explicitly load content (foreground) pages into any tab, or create a tab then load a content page. Do this from the background script. You don't need entries in the manifest file other than requiring active tab permission.

browser.tabs.executeScript(null,{file:"/content.js"}).then(NextAction);function NextAction(Array of last evaluated statement of foreground script in each frame)   {...}