Skip to content

Commit 64fc660

Browse files
hoxyqAndyPengc12
authored andcommitted
refactor: refactored devtools browser extension scripts to improve port management and service worker lifetime (facebook#27215)
Fixes facebook#27119, facebook#27185. Fixed: - React DevTools now works as expected when user performs in-tab navigation, previously it was just stuck. https://github.com/facebook/react/assets/28902667/b11c5f84-7155-47a5-8b5a-7e90baca5347 - When user closes browser DevTools panel, we now do some cleanup to disconnect ports and emit shutdown event for bridge. This should fix the issue with registering duplicated fibers with the same id in Store. Changed: - We reconnect proxy port once in 25 seconds, in order to [keep service worker alive](https://developer.chrome.com/docs/extensions/whatsnew/#m110-sw-idle). - Instead of unregistering dynamically injected content scripts, wen now get list of already registered scripts and filter them out from scripts that we want to inject again, see dynamicallyInjectContentScripts.js. - Split `main.js` and `background.js` into multiple files. Tested on Chromium and Firefox browsers.
1 parent 87783a2 commit 64fc660

22 files changed

+1159
-847
lines changed

packages/react-devtools-extensions/firefox/manifest.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"applications": {
77
"gecko": {
88
"id": "@react-devtools",
9-
"strict_min_version": "55.0"
9+
"strict_min_version": "102.0"
1010
}
1111
},
1212
"icons": {
@@ -41,7 +41,9 @@
4141
"file:///*",
4242
"http://*/*",
4343
"https://*/*",
44-
"clipboardWrite"
44+
"clipboardWrite",
45+
"scripting",
46+
"devtools"
4547
],
4648
"content_scripts": [
4749
{

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

Lines changed: 0 additions & 245 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* global chrome */
2+
3+
import {IS_FIREFOX} from '../utils';
4+
5+
async function dynamicallyInjectContentScripts() {
6+
const contentScriptsToInject = [
7+
{
8+
id: '@react-devtools/hook',
9+
js: ['build/installHook.js'],
10+
matches: ['<all_urls>'],
11+
persistAcrossSessions: true,
12+
runAt: 'document_start',
13+
world: chrome.scripting.ExecutionWorld.MAIN,
14+
},
15+
{
16+
id: '@react-devtools/renderer',
17+
js: ['build/renderer.js'],
18+
matches: ['<all_urls>'],
19+
persistAcrossSessions: true,
20+
runAt: 'document_start',
21+
world: chrome.scripting.ExecutionWorld.MAIN,
22+
},
23+
];
24+
25+
try {
26+
const alreadyRegisteredContentScripts =
27+
await chrome.scripting.getRegisteredContentScripts();
28+
29+
const scriptsToInjectNow = contentScriptsToInject.filter(
30+
scriptToInject =>
31+
!alreadyRegisteredContentScripts.some(
32+
registeredScript => registeredScript.id === scriptToInject.id,
33+
),
34+
);
35+
36+
if (scriptsToInjectNow.length) {
37+
// equivalent logic for Firefox is in prepareInjection.js
38+
// Manifest V3 method of injecting content script
39+
// TODO(hoxyq): migrate Firefox to V3 manifests
40+
// Note: the "world" option in registerContentScripts is only available in Chrome v102+
41+
// It's critical since it allows us to directly run scripts on the "main" world on the page
42+
// "document_start" allows it to run before the page's scripts
43+
// so the hook can be detected by react reconciler
44+
await chrome.scripting.registerContentScripts(scriptsToInjectNow);
45+
}
46+
} catch (error) {
47+
console.error(error);
48+
}
49+
}
50+
51+
if (!IS_FIREFOX) {
52+
dynamicallyInjectContentScripts();
53+
}

0 commit comments

Comments
 (0)