Does "scripting.registerContentScripts" applies to Incognito windows?

137 views
Skip to first unread message

Juraj M.

unread,
Nov 23, 2022, 4:54:32 AM11/23/22
to Chromium Extensions
I'm dealing with some strange behavior in my Incognito windows.
Sometimes it works (it will inject into registered pages in incognito windows), but usually it doesn't.
it works only if I manually reload the extension - it will correctly inject content script into registered pages. But if I disable and enable extension it will not work in Incognito windows.

I'm not using the "split" mode.

Do I need to watch for the Incognito windows to open and re-register my content scripts?

wOxxOm

unread,
Nov 23, 2022, 9:35:29 AM11/23/22
to Chromium Extensions, juraj....@gmail.com
Various API behave differently when the extension is disabled or re-installed. Some of them preserve registrations, some don't. What you describe though is unequivocally a bug because this API loses the registrations only when the extension is toggled, not reloaded. Reloading actually re-installs the extension internally, while toggling doesn't, so a different code path is being used that wasn't wired up correctly. You can report it on https://crbug.com.

wOxxOm

unread,
Nov 23, 2022, 9:40:10 AM11/23/22
to Chromium Extensions, wOxxOm, juraj....@gmail.com
Unfortunately there's no event for an extension being re-enabled, previously it was rejected in https://crbug.com/574531.

The only workaround until these two problems are fixed is to query the current registration each time the service worker starts:
chrome.scripting.getRegisteredContentScripts(scripts => !scripts.length && register())

Juraj M.

unread,
Nov 23, 2022, 10:11:00 AM11/23/22
to Chromium Extensions, wOxxOm, Juraj M.
Thanks!
From what I can see, the `scripting.getRegisteredContentScripts()` correctly returns the list of registered scripts, even when I execute it from incognito context.
However none of them is being injected in the incognito windows.
But this seems to break only after the extension was disabled / enabled. And sadly it then persists between browser restarts.
Or maybe I'm just too tired and I'm missing something obvious here :(
I'll see if I can make some easy STR and I'll report it.

wOxxOm

unread,
Nov 23, 2022, 10:19:34 AM11/23/22
to Chromium Extensions, juraj....@gmail.com, wOxxOm
Well, you can escalate the workaround and just re-register everything each time. To detect re-enabling you'll have to do something like this:

const reenabled = setTimeout(() => {
  // remove + register all scripts again
  ........
});

chrome.api1.onEvent1.addListener(data => {
  clearTimeout(reenabled);
  ............
});

chrome.api2.onEvent2.addListener(data => {
  clearTimeout(reenabled);
  ............
});

Juraj M.

unread,
Nov 26, 2022, 11:10:35 AM11/26/22
to Chromium Extensions, wOxxOm, Juraj M.
Reported with example here:

So as a "simple" workaround I guess I could listen to the " windows.onCreated" event and re-register everything when I detect Incognito window being created.
Reply all
Reply to author
Forward
0 new messages