Skip to content

Commit 54c2f2a

Browse files
authored
fix[devtools/extension]: unregister dynamically injected content scripts instead of filtering (#27369)
Same as #26765. Related bug report - https://bugs.chromium.org/p/chromium/issues/detail?id=1393762. This was changed in #27215, when I have refactored background logic in the extension. I've missed this case, because the extension was working in incognito mode. Turns out, it stops working after the first reload, and it stops only in incognito mode, so I am rolling out back the previous workaround. Tested on Chrome that it fixes the extension in incognito mode and that it doesn't affect default mode.
1 parent caa716d commit 54c2f2a

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

packages/react-devtools-extensions/src/background/dynamicallyInjectContentScripts.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const contentScriptsToInject = IS_FIREFOX
1111
js: ['build/proxy.js'],
1212
matches: ['<all_urls>'],
1313
persistAcrossSessions: true,
14-
runAt: 'document_idle',
14+
runAt: 'document_end',
1515
},
1616
]
1717
: [
@@ -43,26 +43,19 @@ const contentScriptsToInject = IS_FIREFOX
4343

4444
async function dynamicallyInjectContentScripts() {
4545
try {
46-
const alreadyRegisteredContentScripts =
47-
await chrome.scripting.getRegisteredContentScripts();
48-
49-
const scriptsToInjectNow = contentScriptsToInject.filter(
50-
scriptToInject =>
51-
!alreadyRegisteredContentScripts.some(
52-
registeredScript => registeredScript.id === scriptToInject.id,
53-
),
54-
);
46+
// Using this, instead of filtering registered scrips with `chrome.scripting.getRegisteredScripts`
47+
// because of https://bugs.chromium.org/p/chromium/issues/detail?id=1393762
48+
// This fixes registering proxy content script in incognito mode
49+
await chrome.scripting.unregisterContentScripts();
5550

56-
if (scriptsToInjectNow.length) {
57-
// equivalent logic for Firefox is in prepareInjection.js
58-
// Manifest V3 method of injecting content script
59-
// TODO(hoxyq): migrate Firefox to V3 manifests
60-
// Note: the "world" option in registerContentScripts is only available in Chrome v102+
61-
// It's critical since it allows us to directly run scripts on the "main" world on the page
62-
// "document_start" allows it to run before the page's scripts
63-
// so the hook can be detected by react reconciler
64-
await chrome.scripting.registerContentScripts(scriptsToInjectNow);
65-
}
51+
// equivalent logic for Firefox is in prepareInjection.js
52+
// Manifest V3 method of injecting content script
53+
// TODO(hoxyq): migrate Firefox to V3 manifests
54+
// Note: the "world" option in registerContentScripts is only available in Chrome v102+
55+
// It's critical since it allows us to directly run scripts on the "main" world on the page
56+
// "document_start" allows it to run before the page's scripts
57+
// so the hook can be detected by react reconciler
58+
await chrome.scripting.registerContentScripts(contentScriptsToInject);
6659
} catch (error) {
6760
console.error(error);
6861
}

0 commit comments

Comments
 (0)