Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content Script Injection at Install for Firefox Web Extension

If content scripts are specified in manifest.json for Firefox, Firefox will also load content scripts for already opened tabs and execute them even the tabs are already in loaded and ready state.

Google Chrome does not add any content script when extension is installed for already opened tabs. The content script is loaded when the page is refreshed for the older tabs.

I want to ask is Firefox's behavior is expected or bug?

like image 368
John Sewell Avatar asked May 10 '16 00:05

John Sewell


1 Answers

As far as compatibility goes, it's a bug.

  1. Chrome does not do that.
  2. As a result, many extensions implement custom logic to achieve the effect.
  3. One has to take into account side effects. Suppose your content script injects some UI into the page. Then the extension is updated. That amounts to extension restart, and suddenly you have 2 copies of the UI. Also valid if you just attach event listeners, as (at least in case of Chrome) the old content script's context continues to exist (in an "orphaned" state).

The last point is very important and probably the reason why Chrome doesn't do it by default. At some point I made a very long post about this problem - if you're going to report this as a bug to Mozilla, please include that. There's also this feature request that is related.

What would be sensible (and backwards compatible) is to add a parameter to content script description in the manifest - whether to inject into existing pages. It will be up to developers to guarantee that side effects are taken care of. This usually requires even more code to just communicate to the old script that it needs to wind down and clean up.

like image 187
Xan Avatar answered Sep 26 '22 23:09

Xan