Skip to content

Commit c93038f

Browse files
committed
Moved backend injection logic to content script
1 parent 2c98af7 commit c93038f

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
/* global chrome */
22

3-
export default function inject(scriptName: string, done: ?Function) {
3+
export default function inject(scriptName: string, done: ? Function) {
44
const source = `
5-
// the prototype stuff is in case document.createElement has been modified
65
(function () {
7-
var script = document.constructor.prototype.createElement.call(document, 'script');
8-
script.src = "${scriptName}";
9-
script.charset = "utf-8";
10-
document.documentElement.appendChild(script);
11-
script.parentNode.removeChild(script);
6+
window.postMessage({ source: 'react-devtools-inject-backend', type: "FROM_PAGE", text: "Hello from the webpage!" }, "*");
127
})()
138
`;
149

@@ -21,4 +16,4 @@ export default function inject(scriptName: string, done: ?Function) {
2116
done();
2217
}
2318
});
24-
}
19+
}

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
/* global chrome */
22

33
import nullthrows from 'nullthrows';
4-
import {installHook} from 'react-devtools-shared/src/hook';
5-
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from 'react-devtools-shared/src/constants';
6-
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
4+
import {
5+
installHook
6+
} from 'react-devtools-shared/src/hook';
7+
import {
8+
SESSION_STORAGE_RELOAD_AND_PROFILE_KEY
9+
} from 'react-devtools-shared/src/constants';
10+
import {
11+
sessionStorageGetItem
12+
} from 'react-devtools-shared/src/storage';
713

814
function injectCode(code) {
915
const script = document.createElement('script');
@@ -24,16 +30,21 @@ let lastDetectionResult;
2430
// So instead, the hook will use postMessage() to pass message to us here.
2531
// And when this happens, we'll send a message to the "background page".
2632
window.addEventListener('message', function(evt) {
27-
if (
28-
evt.source === window &&
29-
evt.data &&
30-
evt.data.source === 'react-devtools-detector'
31-
) {
32-
lastDetectionResult = {
33-
hasDetectedReact: true,
34-
reactBuildType: evt.data.reactBuildType,
35-
};
36-
chrome.runtime.sendMessage(lastDetectionResult);
33+
if (evt.source === window && evt.data) {
34+
if (evt.data.source === 'react-devtools-detector') {
35+
lastDetectionResult = {
36+
hasDetectedReact: true,
37+
reactBuildType: evt.data.reactBuildType,
38+
};
39+
chrome.runtime.sendMessage(lastDetectionResult);
40+
} else if (evt.data.source === 'react-devtools-inject-backend') {
41+
//Inject backend
42+
var script = document.constructor.prototype.createElement.call(document, 'script');
43+
script.src = chrome.runtime.getURL('build/backend.js');
44+
script.charset = "utf-8";
45+
document.documentElement.appendChild(script);
46+
script.parentNode.removeChild(script);
47+
}
3748
}
3849
});
3950

@@ -86,4 +97,4 @@ if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') {
8697
// devtools are installed (and skip its suggestion to install the devtools).
8798
injectCode(
8899
';(' + installHook.toString() + '(window))' + saveNativeValues + detectReact,
89-
);
100+
);

0 commit comments

Comments
 (0)