content_scripts can run before background page is loaded

NEW
Unassigned

Status

()

Toolkit
WebExtensions: General
P2
normal
3 months ago
13 days ago

People

(Reporter: ianbicking, Unassigned)

Tracking

(Blocks: 2 bugs)

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: triaged)

(Reporter)

Description

3 months ago
We got this bug report in Screenshots: https://github.com/mozilla-services/screenshots/issues/2955

It's not that easy to reproduce, but I'm guessing it's due to a content script being loaded on a page before the background page is initialized. Since deferred startup for background pages just landed, I'm guessing that's related.

I believe extension authors will expect the background page to be loaded before any content_scripts (or implicitly expect it as we did), and so content_scripts should also be deferred.

Updated

2 months ago
Priority: -- → P2
Whiteboard: triaged

Comment 1

2 months ago
We do wait until the background page is loaded before we begin loading content scripts, in general. It's possible that they may load earlier in some corner cases, though, like when new content processes start in the middle of the startup cycle.

Comment 2

2 months ago
I was about to file the same bug. :)

Content script has chrome.runtime.sendMessage()
Background script has chrome.runtime.onMessage.addListener()

On loading the extension I get the following error:

> Error: Could not establish connection. Receiving end does not exist.  (unknown)

I added debugging and noticed that content scripts are injected into pages before the background scripts is run which seems like the wrong order practically and semantically. 

The "content_scripts" is set to "run_at": "document_start", if that makes any difference.

Updated

14 days ago
Duplicate of this bug: 1388270

Updated

14 days ago
Blocks: 467520, 1226547

Comment 4

14 days ago
(In reply to Ian Bicking (:ianb) from comment #0)

> It's not that easy to reproduce

This happens consistently on Android. Every time you install the extension, it runs the content scripts first and initializes the background page afterwards.
You need to log in before you can comment on or make changes to this bug.