@samitouri / QOS-React / commits / 54c2f2a346

fix[devtools/extension]: unregister dynamically injected content scripts instead of filtering (#27369)

Same as https://github.com/facebook/react/pull/26765. Related bug report - https://bugs.chromium.org/p/chromium/issues/detail?id=1393762. This was changed in https://github.com/facebook/react/pull/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.

Ruslan Lesiutin committed Sep 14, 2023 at 15:21 UTC 54c2f2a3469e296fba025cf444d0639c7bf8f39a
1 file changed +13 -20
packages/react-devtools-extensions/src/background/dynamicallyInjectContentScripts.js
+13 -20
@@ -11,7 +11,7 @@ const contentScriptsToInject = IS_FIREFOX
11 js: ['build/proxy.js'],
12 matches: ['<all_urls>'],
13 persistAcrossSessions: true,
14 - runAt: 'document_idle',
14 + runAt: 'document_end',
15 },
16 ]
17 : [
@@ -43,26 +43,19 @@ const contentScriptsToInject = IS_FIREFOX
43
44 async function dynamicallyInjectContentScripts() {
45 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();
50
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);
59 } catch (error) {
60 console.error(error);
61 }