From c1f236dda89613554915fed1ed43bd01097d3ad1 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 4 Aug 2021 13:15:42 -0700 Subject: [PATCH] early attach in devtools extension so we can patch the console --- .../src/injectGlobalHook.js | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/react-devtools-extensions/src/injectGlobalHook.js b/packages/react-devtools-extensions/src/injectGlobalHook.js index 980fe21f96d51..442c1c8602090 100644 --- a/packages/react-devtools-extensions/src/injectGlobalHook.js +++ b/packages/react-devtools-extensions/src/injectGlobalHook.js @@ -2,8 +2,6 @@ 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'; function injectCode(code) { const script = document.createElement('script'); @@ -67,24 +65,23 @@ window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeWeakMap = WeakMap; window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeSet = Set; `; -// If we have just reloaded to profile, we need to inject the renderer interface before the app loads. -if (sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true') { - const rendererURL = chrome.runtime.getURL('build/renderer.js'); - let rendererCode; +// We need to inject the renderer interface before the app loads so we can patch the +// console before initial render. +const rendererURL = chrome.runtime.getURL('build/renderer.js'); +let rendererCode; - // We need to inject in time to catch the initial mount. - // This means we need to synchronously read the renderer code itself, - // and synchronously inject it into the page. - // There are very few ways to actually do this. - // This seems to be the best approach. - const request = new XMLHttpRequest(); - request.addEventListener('load', function() { - rendererCode = this.responseText; - }); - request.open('GET', rendererURL, false); - request.send(); - injectCode(rendererCode); -} +// We need to inject in time to catch the initial mount. +// This means we need to synchronously read the renderer code itself, +// and synchronously inject it into the page. +// There are very few ways to actually do this. +// This seems to be the best approach. +const request = new XMLHttpRequest(); +request.addEventListener('load', function() { + rendererCode = this.responseText; +}); +request.open('GET', rendererURL, false); +request.send(); +injectCode(rendererCode); // Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with. // Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.