Skip to content

Commit

Permalink
Moved backend injection logic to content script
Browse files Browse the repository at this point in the history
  • Loading branch information
onionymous committed Sep 11, 2019
1 parent 2c98af7 commit c93038f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
11 changes: 3 additions & 8 deletions packages/react-devtools-extensions/src/inject.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
/* global chrome */

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

Expand All @@ -21,4 +16,4 @@ export default function inject(scriptName: string, done: ?Function) {
done();
}
});
}
}
39 changes: 25 additions & 14 deletions packages/react-devtools-extensions/src/injectGlobalHook.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* global chrome */

import nullthrows from 'nullthrows';
import {installHook} from 'react-devtools-shared/src/hook';
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from 'react-devtools-shared/src/constants';
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
import {
installHook
} from 'react-devtools-shared/src/hook';
import {
SESSION_STORAGE_RELOAD_AND_PROFILE_KEY
} from 'react-devtools-shared/src/constants';
import {
sessionStorageGetItem
} from 'react-devtools-shared/src/storage';

function injectCode(code) {
const script = document.createElement('script');
Expand All @@ -24,16 +30,21 @@ let lastDetectionResult;
// So instead, the hook will use postMessage() to pass message to us here.
// And when this happens, we'll send a message to the "background page".
window.addEventListener('message', function(evt) {
if (
evt.source === window &&
evt.data &&
evt.data.source === 'react-devtools-detector'
) {
lastDetectionResult = {
hasDetectedReact: true,
reactBuildType: evt.data.reactBuildType,
};
chrome.runtime.sendMessage(lastDetectionResult);
if (evt.source === window && evt.data) {
if (evt.data.source === 'react-devtools-detector') {
lastDetectionResult = {
hasDetectedReact: true,
reactBuildType: evt.data.reactBuildType,
};
chrome.runtime.sendMessage(lastDetectionResult);
} else if (evt.data.source === 'react-devtools-inject-backend') {
//Inject backend
var script = document.constructor.prototype.createElement.call(document, 'script');
script.src = chrome.runtime.getURL('build/backend.js');
script.charset = "utf-8";
document.documentElement.appendChild(script);
script.parentNode.removeChild(script);
}
}
});

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

0 comments on commit c93038f

Please sign in to comment.