Skip to content

Commit 49b0cb6

Browse files
linshunghuanggaearon
authored andcommitted
Moving backend injection to the content script (#16900)
1 parent 3694a3b commit 49b0cb6

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ let lastDetectionResult;
2424
// So instead, the hook will use postMessage() to pass message to us here.
2525
// And when this happens, we'll send a message to the "background page".
2626
window.addEventListener('message', function(evt) {
27-
if (
28-
evt.source === window &&
29-
evt.data &&
30-
evt.data.source === 'react-devtools-detector'
31-
) {
27+
if (evt.source !== window || !evt.data) {
28+
return;
29+
}
30+
if (evt.data.source === 'react-devtools-detector') {
3231
lastDetectionResult = {
3332
hasDetectedReact: true,
3433
reactBuildType: evt.data.reactBuildType,
3534
};
3635
chrome.runtime.sendMessage(lastDetectionResult);
36+
} else if (evt.data.source === 'react-devtools-inject-backend') {
37+
const script = document.createElement('script');
38+
script.src = chrome.runtime.getURL('build/backend.js');
39+
document.documentElement.appendChild(script);
40+
script.parentNode.removeChild(script);
3741
}
3842
});
3943

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {createElement} from 'react';
44
import {unstable_createRoot as createRoot, flushSync} from 'react-dom';
55
import Bridge from 'react-devtools-shared/src/bridge';
66
import Store from 'react-devtools-shared/src/devtools/store';
7-
import inject from './inject';
87
import {
98
createViewElementSource,
109
getBrowserName,
@@ -135,7 +134,14 @@ function createPanelIfReactLoaded() {
135134

136135
// Initialize the backend only once the Store has been initialized.
137136
// Otherwise the Store may miss important initial tree op codes.
138-
inject(chrome.runtime.getURL('build/backend.js'));
137+
chrome.devtools.inspectedWindow.eval(
138+
`window.postMessage({ source: 'react-devtools-inject-backend' }, window.origin);`,
139+
function(response, evalError) {
140+
if (evalError) {
141+
console.error(evalError);
142+
}
143+
},
144+
);
139145

140146
const viewElementSourceFunction = createViewElementSource(
141147
bridge,

0 commit comments

Comments
 (0)