|
1 | 1 | /* global chrome */ |
2 | 2 |
|
3 | 3 | 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'; |
7 | 13 |
|
8 | 14 | function injectCode(code) { |
9 | 15 | const script = document.createElement('script'); |
@@ -24,16 +30,21 @@ let lastDetectionResult; |
24 | 30 | // So instead, the hook will use postMessage() to pass message to us here. |
25 | 31 | // And when this happens, we'll send a message to the "background page". |
26 | 32 | 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 | + } |
37 | 48 | } |
38 | 49 | }); |
39 | 50 |
|
@@ -86,4 +97,4 @@ if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') { |
86 | 97 | // devtools are installed (and skip its suggestion to install the devtools). |
87 | 98 | injectCode( |
88 | 99 | ';(' + installHook.toString() + '(window))' + saveNativeValues + detectReact, |
89 | | -); |
| 100 | +); |