Content Script Injection at Install for Firefox Web Extension
Asked Answered
P

1

7

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?

Peepul answered 10/5, 2016 at 0:18 Comment(0)
N
6

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.

Noland answered 10/5, 2016 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.