2019-01-24 10:06:21 +08:00
|
|
|
/* global chrome */
|
|
|
|
|
|
2019-08-14 08:58:03 +08:00
|
|
|
import {createElement} from 'react';
|
2021-05-12 23:28:14 +08:00
|
|
|
import {createRoot, flushSync} from 'react-dom';
|
2019-08-14 06:59:43 +08:00
|
|
|
import Bridge from 'react-devtools-shared/src/bridge';
|
|
|
|
|
import Store from 'react-devtools-shared/src/devtools/store';
|
2019-12-19 04:12:34 +08:00
|
|
|
import {getBrowserName, getBrowserTheme} from './utils';
|
2019-10-04 02:07:18 +08:00
|
|
|
import {LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY} from 'react-devtools-shared/src/constants';
|
2019-08-14 08:58:03 +08:00
|
|
|
import {
|
|
|
|
|
getAppendComponentStack,
|
2020-05-30 05:34:43 +08:00
|
|
|
getBreakOnConsoleErrors,
|
|
|
|
|
getSavedComponentFilters,
|
2020-12-23 00:09:29 +08:00
|
|
|
getShowInlineWarningsAndErrors,
|
2021-08-26 06:35:38 +08:00
|
|
|
getHideConsoleLogsInStrictMode,
|
2019-08-14 08:58:03 +08:00
|
|
|
} from 'react-devtools-shared/src/utils';
|
2019-06-08 01:51:01 +08:00
|
|
|
import {
|
|
|
|
|
localStorageGetItem,
|
|
|
|
|
localStorageRemoveItem,
|
|
|
|
|
localStorageSetItem,
|
2019-08-14 06:59:43 +08:00
|
|
|
} from 'react-devtools-shared/src/storage';
|
|
|
|
|
import DevTools from 'react-devtools-shared/src/devtools/views/DevTools';
|
2021-09-11 03:11:18 +08:00
|
|
|
import {__DEBUG__} from 'react-devtools-shared/src/constants';
|
2019-02-15 03:45:11 +08:00
|
|
|
|
2019-04-04 05:32:16 +08:00
|
|
|
const LOCAL_STORAGE_SUPPORTS_PROFILING_KEY =
|
|
|
|
|
'React::DevTools::supportsProfiling';
|
2019-03-28 00:41:12 +08:00
|
|
|
|
2019-08-22 05:23:51 +08:00
|
|
|
const isChrome = getBrowserName() === 'Chrome';
|
|
|
|
|
|
2019-01-24 10:06:21 +08:00
|
|
|
let panelCreated = false;
|
|
|
|
|
|
2019-05-09 05:22:06 +08:00
|
|
|
// The renderer interface can't read saved component filters directly,
|
|
|
|
|
// because they are stored in localStorage within the context of the extension.
|
|
|
|
|
// Instead it relies on the extension to pass filters through.
|
2019-07-18 02:12:39 +08:00
|
|
|
function syncSavedPreferences() {
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
|
|
|
|
`window.__REACT_DEVTOOLS_APPEND_COMPONENT_STACK__ = ${JSON.stringify(
|
2020-12-23 00:09:29 +08:00
|
|
|
getAppendComponentStack(),
|
2020-05-30 05:34:43 +08:00
|
|
|
)};
|
|
|
|
|
window.__REACT_DEVTOOLS_BREAK_ON_CONSOLE_ERRORS__ = ${JSON.stringify(
|
2020-12-23 00:09:29 +08:00
|
|
|
getBreakOnConsoleErrors(),
|
2020-05-30 05:34:43 +08:00
|
|
|
)};
|
|
|
|
|
window.__REACT_DEVTOOLS_COMPONENT_FILTERS__ = ${JSON.stringify(
|
2020-12-23 00:09:29 +08:00
|
|
|
getSavedComponentFilters(),
|
|
|
|
|
)};
|
|
|
|
|
window.__REACT_DEVTOOLS_SHOW_INLINE_WARNINGS_AND_ERRORS__ = ${JSON.stringify(
|
|
|
|
|
getShowInlineWarningsAndErrors(),
|
2021-08-26 06:35:38 +08:00
|
|
|
)};
|
|
|
|
|
window.__REACT_DEVTOOLS_HIDE_CONSOLE_LOGS_IN_STRICT_MODE__ = ${JSON.stringify(
|
|
|
|
|
getHideConsoleLogsInStrictMode(),
|
|
|
|
|
)};
|
|
|
|
|
window.__REACT_DEVTOOLS_BROWSER_THEME__ = ${JSON.stringify(
|
|
|
|
|
getBrowserTheme(),
|
2019-08-14 12:59:07 +08:00
|
|
|
)};`,
|
2019-07-18 02:12:39 +08:00
|
|
|
);
|
2019-05-09 05:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 02:12:39 +08:00
|
|
|
syncSavedPreferences();
|
2019-05-09 05:22:06 +08:00
|
|
|
|
2019-01-24 10:06:21 +08:00
|
|
|
function createPanelIfReactLoaded() {
|
|
|
|
|
if (panelCreated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-02-13 23:59:49 +08:00
|
|
|
|
2019-01-24 10:06:21 +08:00
|
|
|
chrome.devtools.inspectedWindow.eval(
|
2019-02-05 02:01:44 +08:00
|
|
|
'window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0',
|
2019-02-15 03:45:11 +08:00
|
|
|
function(pageHasReact, error) {
|
2019-01-24 10:06:21 +08:00
|
|
|
if (!pageHasReact || panelCreated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
panelCreated = true;
|
2019-02-15 03:45:11 +08:00
|
|
|
|
|
|
|
|
clearInterval(loadCheckInterval);
|
|
|
|
|
|
|
|
|
|
let bridge = null;
|
|
|
|
|
let store = null;
|
2019-03-19 00:20:00 +08:00
|
|
|
|
2019-05-23 11:00:21 +08:00
|
|
|
let profilingData = null;
|
|
|
|
|
|
2019-04-03 02:53:46 +08:00
|
|
|
let componentsPortalContainer = null;
|
2019-03-19 00:11:28 +08:00
|
|
|
let profilerPortalContainer = null;
|
2019-03-19 00:20:00 +08:00
|
|
|
|
2019-03-19 00:11:28 +08:00
|
|
|
let cloneStyleTags = null;
|
2019-03-19 00:36:47 +08:00
|
|
|
let mostRecentOverrideTab = null;
|
2019-03-19 00:11:28 +08:00
|
|
|
let render = null;
|
2019-03-19 00:36:47 +08:00
|
|
|
let root = null;
|
2019-02-15 03:45:11 +08:00
|
|
|
|
2019-05-31 00:27:28 +08:00
|
|
|
const tabId = chrome.devtools.inspectedWindow.tabId;
|
|
|
|
|
|
2019-02-15 03:45:11 +08:00
|
|
|
function initBridgeAndStore() {
|
|
|
|
|
const port = chrome.runtime.connect({
|
2019-05-31 00:27:28 +08:00
|
|
|
name: '' + tabId,
|
2019-01-24 10:06:21 +08:00
|
|
|
});
|
Fix for 'Attempting to use a disconnected port object'
Fixes https://github.com/bvaughn/react-devtools-experimental/issues/217
The error reproduces with any two React websites, e.g. `https://reactjs.org` and `https://nextjs.org`, by keeping the DevTools Components tab open and switching between these websites in the same browser tab.
There are several issues with the code that contribute to this:
1. `Bridge` leaves behind a dangling timer that fires `_flush` after the bridge has been abandoned ("shutdown").
2. `bridge.send('shutdown')` is asynchronous, so the event handlers do not get unsubscribed in time.
3. `port.onDisconnect` does not trigger on in-tab navigation like new URL or back/forward navigation.
4. State management design of the code that uses shared variables and callbacks makes it hard to handle race conditions originating from the browser.
This commit cleans up some of the lacking symmetry when using `addListener`/`removeListener`, but the code in `shells/browser/shared/src/main.js` is hard to reason about with regards to race conditions, and there are many possible race conditions originating from the browser, so maybe there could be a better design paradigm (like a formal state machine) to manage the state changes in response to sequences of events than plain old event listeners, callbacks, and shared variables.
Unrelated, but clicking Chrome Back/Forward/Back/Forward very fast makes the browser and the DevTools and the DevTools of DevTools stall and become unresponsive for some time, then recovers but the Back/Forward/Stop/Refresh button and favicon loading indicator may remain broken. Looks like a Chrome bug, some kind of a temporary deadlock in handling the browser history.
2019-04-26 19:30:04 +08:00
|
|
|
// Looks like `port.onDisconnect` does not trigger on in-tab navigation like new URL or back/forward navigation,
|
|
|
|
|
// so it makes no sense to handle it here.
|
2019-02-13 23:59:49 +08:00
|
|
|
|
2019-02-15 03:45:11 +08:00
|
|
|
bridge = new Bridge({
|
|
|
|
|
listen(fn) {
|
Fix for 'Attempting to use a disconnected port object'
Fixes https://github.com/bvaughn/react-devtools-experimental/issues/217
The error reproduces with any two React websites, e.g. `https://reactjs.org` and `https://nextjs.org`, by keeping the DevTools Components tab open and switching between these websites in the same browser tab.
There are several issues with the code that contribute to this:
1. `Bridge` leaves behind a dangling timer that fires `_flush` after the bridge has been abandoned ("shutdown").
2. `bridge.send('shutdown')` is asynchronous, so the event handlers do not get unsubscribed in time.
3. `port.onDisconnect` does not trigger on in-tab navigation like new URL or back/forward navigation.
4. State management design of the code that uses shared variables and callbacks makes it hard to handle race conditions originating from the browser.
This commit cleans up some of the lacking symmetry when using `addListener`/`removeListener`, but the code in `shells/browser/shared/src/main.js` is hard to reason about with regards to race conditions, and there are many possible race conditions originating from the browser, so maybe there could be a better design paradigm (like a formal state machine) to manage the state changes in response to sequences of events than plain old event listeners, callbacks, and shared variables.
Unrelated, but clicking Chrome Back/Forward/Back/Forward very fast makes the browser and the DevTools and the DevTools of DevTools stall and become unresponsive for some time, then recovers but the Back/Forward/Stop/Refresh button and favicon loading indicator may remain broken. Looks like a Chrome bug, some kind of a temporary deadlock in handling the browser history.
2019-04-26 19:30:04 +08:00
|
|
|
const listener = message => fn(message);
|
|
|
|
|
// Store the reference so that we unsubscribe from the same object.
|
|
|
|
|
const portOnMessage = port.onMessage;
|
|
|
|
|
portOnMessage.addListener(listener);
|
|
|
|
|
return () => {
|
|
|
|
|
portOnMessage.removeListener(listener);
|
|
|
|
|
};
|
2019-02-15 03:45:11 +08:00
|
|
|
},
|
|
|
|
|
send(event: string, payload: any, transferable?: Array<any>) {
|
2019-08-14 08:58:03 +08:00
|
|
|
port.postMessage({event, payload}, transferable);
|
2019-02-15 03:45:11 +08:00
|
|
|
},
|
|
|
|
|
});
|
2019-03-28 00:41:12 +08:00
|
|
|
bridge.addListener('reloadAppForProfiling', () => {
|
2019-06-08 01:51:01 +08:00
|
|
|
localStorageSetItem(LOCAL_STORAGE_SUPPORTS_PROFILING_KEY, 'true');
|
2019-03-28 00:41:12 +08:00
|
|
|
chrome.devtools.inspectedWindow.eval('window.location.reload();');
|
|
|
|
|
});
|
2019-04-14 01:55:51 +08:00
|
|
|
bridge.addListener('syncSelectionToNativeElementsPanel', () => {
|
|
|
|
|
setBrowserSelectionFromReact();
|
|
|
|
|
});
|
2019-02-15 03:45:11 +08:00
|
|
|
|
2019-03-28 00:41:12 +08:00
|
|
|
// This flag lets us tip the Store off early that we expect to be profiling.
|
|
|
|
|
// This avoids flashing a temporary "Profiling not supported" message in the Profiler tab,
|
|
|
|
|
// after a user has clicked the "reload and profile" button.
|
2019-04-02 05:43:46 +08:00
|
|
|
let isProfiling = false;
|
2019-03-28 00:41:12 +08:00
|
|
|
let supportsProfiling = false;
|
2019-04-04 05:32:16 +08:00
|
|
|
if (
|
2019-06-08 01:51:01 +08:00
|
|
|
localStorageGetItem(LOCAL_STORAGE_SUPPORTS_PROFILING_KEY) === 'true'
|
2019-04-04 05:32:16 +08:00
|
|
|
) {
|
2019-03-28 00:41:12 +08:00
|
|
|
supportsProfiling = true;
|
2019-04-02 05:43:46 +08:00
|
|
|
isProfiling = true;
|
2019-06-08 01:51:01 +08:00
|
|
|
localStorageRemoveItem(LOCAL_STORAGE_SUPPORTS_PROFILING_KEY);
|
2019-03-28 00:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 08:34:34 +08:00
|
|
|
if (store !== null) {
|
|
|
|
|
profilingData = store.profilerStore.profilingData;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 02:07:18 +08:00
|
|
|
bridge.addListener('extensionBackendInitialized', () => {
|
|
|
|
|
// Initialize the renderer's trace-updates setting.
|
|
|
|
|
// This handles the case of navigating to a new page after the DevTools have already been shown.
|
|
|
|
|
bridge.send(
|
|
|
|
|
'setTraceUpdatesEnabled',
|
|
|
|
|
localStorageGetItem(LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY) ===
|
|
|
|
|
'true',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-28 00:41:12 +08:00
|
|
|
store = new Store(bridge, {
|
2019-04-02 05:43:46 +08:00
|
|
|
isProfiling,
|
2019-08-22 05:23:51 +08:00
|
|
|
supportsReloadAndProfile: isChrome,
|
2019-03-28 00:41:12 +08:00
|
|
|
supportsProfiling,
|
2021-07-23 01:58:57 +08:00
|
|
|
// At this time, the scheduling profiler can only parse Chrome performance profiles.
|
|
|
|
|
supportsSchedulingProfiler: isChrome,
|
2019-10-04 02:07:18 +08:00
|
|
|
supportsTraceUpdates: true,
|
2019-03-28 00:41:12 +08:00
|
|
|
});
|
2019-05-23 11:00:21 +08:00
|
|
|
store.profilerStore.profilingData = profilingData;
|
2019-02-15 03:45:11 +08:00
|
|
|
|
2019-02-21 03:55:48 +08:00
|
|
|
// Initialize the backend only once the Store has been initialized.
|
|
|
|
|
// Otherwise the Store may miss important initial tree op codes.
|
2019-09-26 21:03:07 +08:00
|
|
|
chrome.devtools.inspectedWindow.eval(
|
2019-09-30 23:29:18 +08:00
|
|
|
`window.postMessage({ source: 'react-devtools-inject-backend' }, '*');`,
|
2019-09-26 21:03:07 +08:00
|
|
|
function(response, evalError) {
|
|
|
|
|
if (evalError) {
|
|
|
|
|
console.error(evalError);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-02-21 03:55:48 +08:00
|
|
|
|
2019-12-19 04:12:34 +08:00
|
|
|
const viewAttributeSourceFunction = (id, path) => {
|
|
|
|
|
const rendererID = store.getRendererIDForElement(id);
|
|
|
|
|
if (rendererID != null) {
|
|
|
|
|
// Ask the renderer interface to find the specified attribute,
|
|
|
|
|
// and store it as a global variable on the window.
|
|
|
|
|
bridge.send('viewAttributeSource', {id, path, rendererID});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
// Ask Chrome to display the location of the attribute,
|
|
|
|
|
// assuming the renderer found a match.
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(`
|
|
|
|
|
if (window.$attribute != null) {
|
|
|
|
|
inspect(window.$attribute);
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const viewElementSourceFunction = id => {
|
|
|
|
|
const rendererID = store.getRendererIDForElement(id);
|
|
|
|
|
if (rendererID != null) {
|
|
|
|
|
// Ask the renderer interface to determine the component function,
|
|
|
|
|
// and store it as a global variable on the window
|
|
|
|
|
bridge.send('viewElementSource', {id, rendererID});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
// Ask Chrome to display the location of the component function,
|
|
|
|
|
// or a render method if it is a Class (ideally Class instance, not type)
|
|
|
|
|
// assuming the renderer found one.
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(`
|
|
|
|
|
if (window.$type != null) {
|
|
|
|
|
if (
|
|
|
|
|
window.$type &&
|
|
|
|
|
window.$type.prototype &&
|
|
|
|
|
window.$type.prototype.isReactComponent
|
|
|
|
|
) {
|
|
|
|
|
// inspect Component.render, not constructor
|
|
|
|
|
inspect(window.$type.prototype.render);
|
|
|
|
|
} else {
|
|
|
|
|
// inspect Functional Component
|
|
|
|
|
inspect(window.$type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-03-18 04:52:37 +08:00
|
|
|
|
2021-09-11 03:11:18 +08:00
|
|
|
let debugIDCounter = 0;
|
|
|
|
|
|
2021-09-02 02:10:07 +08:00
|
|
|
// For some reason in Firefox, chrome.runtime.sendMessage() from a content script
|
|
|
|
|
// never reaches the chrome.runtime.onMessage event listener.
|
|
|
|
|
let fetchFileWithCaching = null;
|
|
|
|
|
if (isChrome) {
|
2021-09-11 03:11:18 +08:00
|
|
|
const fetchFromNetworkCache = (url, resolve, reject) => {
|
|
|
|
|
// Debug ID allows us to avoid re-logging (potentially long) URL strings below,
|
|
|
|
|
// while also still associating (potentially) interleaved logs with the original request.
|
|
|
|
|
let debugID = null;
|
|
|
|
|
|
|
|
|
|
if (__DEBUG__) {
|
|
|
|
|
debugID = debugIDCounter++;
|
|
|
|
|
console.log(`[main] fetchFromNetworkCache(${debugID})`, url);
|
2021-09-02 02:10:07 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-11 03:11:18 +08:00
|
|
|
chrome.devtools.network.getHAR(harLog => {
|
|
|
|
|
for (let i = 0; i < harLog.entries.length; i++) {
|
|
|
|
|
const entry = harLog.entries[i];
|
|
|
|
|
if (url === entry.request.url) {
|
|
|
|
|
if (__DEBUG__) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[main] fetchFromNetworkCache(${debugID}) Found matching URL in HAR`,
|
|
|
|
|
url,
|
|
|
|
|
);
|
2021-09-02 02:10:07 +08:00
|
|
|
}
|
2021-09-11 03:11:18 +08:00
|
|
|
|
|
|
|
|
entry.getContent(content => {
|
|
|
|
|
if (content) {
|
|
|
|
|
if (__DEBUG__) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[main] fetchFromNetworkCache(${debugID}) Content retrieved`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(content);
|
|
|
|
|
} else {
|
|
|
|
|
if (__DEBUG__) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[main] fetchFromNetworkCache(${debugID}) Invalid content returned by getContent()`,
|
|
|
|
|
content,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Edge case where getContent() returned null; fall back to fetch.
|
2021-09-11 04:26:46 +08:00
|
|
|
fetchFromPage(url, resolve, reject);
|
2021-09-11 03:11:18 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
2021-09-02 02:10:07 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-11 03:11:18 +08:00
|
|
|
if (__DEBUG__) {
|
|
|
|
|
console.log(
|
|
|
|
|
`[main] fetchFromNetworkCache(${debugID}) No cached request found in getHAR()`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-02 02:10:07 +08:00
|
|
|
|
2021-09-11 03:11:18 +08:00
|
|
|
// No matching URL found; fall back to fetch.
|
2021-09-11 04:26:46 +08:00
|
|
|
fetchFromPage(url, resolve, reject);
|
2021-09-11 03:11:18 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchFromPage = (url, resolve, reject) => {
|
|
|
|
|
if (__DEBUG__) {
|
|
|
|
|
console.log('[main] fetchFromPage()', url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onPortMessage({payload, source}) {
|
|
|
|
|
if (source === 'react-devtools-content-script') {
|
|
|
|
|
switch (payload?.type) {
|
|
|
|
|
case 'fetch-file-with-cache-complete':
|
|
|
|
|
chrome.runtime.onMessage.removeListener(onPortMessage);
|
|
|
|
|
resolve(payload.value);
|
|
|
|
|
break;
|
|
|
|
|
case 'fetch-file-with-cache-error':
|
|
|
|
|
chrome.runtime.onMessage.removeListener(onPortMessage);
|
|
|
|
|
reject(payload.value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.runtime.onMessage.addListener(onPortMessage);
|
|
|
|
|
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(`
|
|
|
|
|
window.postMessage({
|
|
|
|
|
source: 'react-devtools-extension',
|
|
|
|
|
payload: {
|
|
|
|
|
type: 'fetch-file-with-cache',
|
|
|
|
|
url: "${url}",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
`);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Fetching files from the extension won't make use of the network cache
|
|
|
|
|
// for resources that have already been loaded by the page.
|
|
|
|
|
// This helper function allows the extension to request files to be fetched
|
|
|
|
|
// by the content script (running in the page) to increase the likelihood of a cache hit.
|
|
|
|
|
fetchFileWithCaching = url => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// Try fetching from the Network cache first.
|
|
|
|
|
// If DevTools was opened after the page started loading, we may have missed some requests.
|
|
|
|
|
// So fall back to a fetch() from the page and hope we get a cached response that way.
|
|
|
|
|
fetchFromNetworkCache(url, resolve, reject);
|
2021-09-02 02:10:07 +08:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 03:25:26 +08:00
|
|
|
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
|
|
|
|
|
const hookNamesModuleLoaderFunction = () =>
|
2021-09-10 05:26:13 +08:00
|
|
|
import('react-devtools-shared/src/hooks/parseHookNames');
|
2021-09-10 03:25:26 +08:00
|
|
|
|
2019-03-19 00:36:47 +08:00
|
|
|
root = createRoot(document.createElement('div'));
|
|
|
|
|
|
|
|
|
|
render = (overrideTab = mostRecentOverrideTab) => {
|
|
|
|
|
mostRecentOverrideTab = overrideTab;
|
2021-09-10 03:25:26 +08:00
|
|
|
root.render(
|
|
|
|
|
createElement(DevTools, {
|
|
|
|
|
bridge,
|
|
|
|
|
browserTheme: getBrowserTheme(),
|
|
|
|
|
componentsPortalContainer,
|
|
|
|
|
enabledInspectedElementContextMenu: true,
|
|
|
|
|
fetchFileWithCaching,
|
|
|
|
|
hookNamesModuleLoaderFunction,
|
|
|
|
|
overrideTab,
|
|
|
|
|
profilerPortalContainer,
|
|
|
|
|
showTabBar: false,
|
|
|
|
|
store,
|
|
|
|
|
warnIfUnsupportedVersionDetected: true,
|
|
|
|
|
viewAttributeSourceFunction,
|
|
|
|
|
viewElementSourceFunction,
|
|
|
|
|
}),
|
2019-03-18 04:52:37 +08:00
|
|
|
);
|
2019-03-19 00:11:28 +08:00
|
|
|
};
|
2019-03-19 00:36:47 +08:00
|
|
|
|
|
|
|
|
render();
|
2019-03-19 00:11:28 +08:00
|
|
|
}
|
2019-03-18 04:52:37 +08:00
|
|
|
|
2019-03-19 00:11:28 +08:00
|
|
|
cloneStyleTags = () => {
|
|
|
|
|
const linkTags = [];
|
2019-08-14 12:59:07 +08:00
|
|
|
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
2020-04-02 03:35:52 +08:00
|
|
|
for (const linkTag of document.getElementsByTagName('link')) {
|
2019-03-19 00:11:28 +08:00
|
|
|
if (linkTag.rel === 'stylesheet') {
|
|
|
|
|
const newLinkTag = document.createElement('link');
|
2019-08-14 12:59:07 +08:00
|
|
|
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
|
2020-04-02 03:35:52 +08:00
|
|
|
for (const attribute of linkTag.attributes) {
|
2019-03-19 00:11:28 +08:00
|
|
|
newLinkTag.setAttribute(attribute.nodeName, attribute.nodeValue);
|
2019-03-18 04:52:37 +08:00
|
|
|
}
|
2019-03-19 00:11:28 +08:00
|
|
|
linkTags.push(newLinkTag);
|
2019-03-18 04:52:37 +08:00
|
|
|
}
|
2019-02-15 03:45:11 +08:00
|
|
|
}
|
2019-03-19 00:11:28 +08:00
|
|
|
return linkTags;
|
|
|
|
|
};
|
2019-02-15 03:45:11 +08:00
|
|
|
|
|
|
|
|
initBridgeAndStore();
|
|
|
|
|
|
2019-04-08 22:29:51 +08:00
|
|
|
function ensureInitialHTMLIsCleared(container) {
|
|
|
|
|
if (container._hasInitialHTMLBeenCleared) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
container.innerHTML = '';
|
|
|
|
|
container._hasInitialHTMLBeenCleared = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-14 01:55:51 +08:00
|
|
|
function setBrowserSelectionFromReact() {
|
|
|
|
|
// This is currently only called on demand when you press "view DOM".
|
|
|
|
|
// In the future, if Chrome adds an inspect() that doesn't switch tabs,
|
|
|
|
|
// we could make this happen automatically when you select another component.
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
|
|
|
|
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
|
|
|
|
|
'(inspect(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0), true) :' +
|
|
|
|
|
'false',
|
2019-08-14 12:59:07 +08:00
|
|
|
(didSelectionChange, evalError) => {
|
|
|
|
|
if (evalError) {
|
|
|
|
|
console.error(evalError);
|
2019-04-14 01:55:51 +08:00
|
|
|
}
|
2019-08-14 12:59:07 +08:00
|
|
|
},
|
2019-04-14 01:55:51 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 08:30:40 +08:00
|
|
|
function setReactSelectionFromBrowser() {
|
2019-04-09 03:41:01 +08:00
|
|
|
// When the user chooses a different node in the browser Elements tab,
|
|
|
|
|
// copy it over to the hook object so that we can sync the selection.
|
|
|
|
|
chrome.devtools.inspectedWindow.eval(
|
2020-02-03 04:04:48 +08:00
|
|
|
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
|
2019-04-10 03:04:33 +08:00
|
|
|
'(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 = $0, true) :' +
|
|
|
|
|
'false',
|
2019-08-14 12:59:07 +08:00
|
|
|
(didSelectionChange, evalError) => {
|
|
|
|
|
if (evalError) {
|
|
|
|
|
console.error(evalError);
|
2019-04-10 03:04:33 +08:00
|
|
|
} else if (didSelectionChange) {
|
|
|
|
|
// Remember to sync the selection next time we show Components tab.
|
|
|
|
|
needsToSyncElementSelection = true;
|
2019-04-09 03:41:01 +08:00
|
|
|
}
|
2019-08-14 12:59:07 +08:00
|
|
|
},
|
2019-04-09 03:41:01 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 03:04:33 +08:00
|
|
|
setReactSelectionFromBrowser();
|
|
|
|
|
chrome.devtools.panels.elements.onSelectionChanged.addListener(() => {
|
2019-04-09 08:30:40 +08:00
|
|
|
setReactSelectionFromBrowser();
|
2019-04-10 03:04:33 +08:00
|
|
|
});
|
2019-04-09 03:41:01 +08:00
|
|
|
|
2019-03-27 02:13:40 +08:00
|
|
|
let currentPanel = null;
|
2019-04-10 03:04:33 +08:00
|
|
|
let needsToSyncElementSelection = false;
|
2019-03-27 02:13:40 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
chrome.devtools.panels.create(
|
2020-08-13 23:16:35 +08:00
|
|
|
isChrome ? '⚛️ Components' : 'Components',
|
2019-08-15 01:33:33 +08:00
|
|
|
'',
|
|
|
|
|
'panel.html',
|
|
|
|
|
extensionPanel => {
|
|
|
|
|
extensionPanel.onShown.addListener(panel => {
|
|
|
|
|
if (needsToSyncElementSelection) {
|
|
|
|
|
needsToSyncElementSelection = false;
|
|
|
|
|
bridge.send('syncSelectionFromNativeElementsPanel');
|
|
|
|
|
}
|
2019-04-10 03:04:33 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
if (currentPanel === panel) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-27 02:13:40 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
currentPanel = panel;
|
|
|
|
|
componentsPortalContainer = panel.container;
|
2019-03-27 02:13:40 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
if (componentsPortalContainer != null) {
|
|
|
|
|
ensureInitialHTMLIsCleared(componentsPortalContainer);
|
|
|
|
|
render('components');
|
|
|
|
|
panel.injectStyles(cloneStyleTags);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
extensionPanel.onHidden.addListener(panel => {
|
|
|
|
|
// TODO: Stop highlighting and stuff.
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-02-15 03:45:11 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
chrome.devtools.panels.create(
|
2020-08-13 23:16:35 +08:00
|
|
|
isChrome ? '⚛️ Profiler' : 'Profiler',
|
2019-08-15 01:33:33 +08:00
|
|
|
'',
|
|
|
|
|
'panel.html',
|
|
|
|
|
extensionPanel => {
|
|
|
|
|
extensionPanel.onShown.addListener(panel => {
|
|
|
|
|
if (currentPanel === panel) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-27 02:13:40 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
currentPanel = panel;
|
|
|
|
|
profilerPortalContainer = panel.container;
|
2019-03-27 02:13:40 +08:00
|
|
|
|
2019-08-15 01:33:33 +08:00
|
|
|
if (profilerPortalContainer != null) {
|
|
|
|
|
ensureInitialHTMLIsCleared(profilerPortalContainer);
|
|
|
|
|
render('profiler');
|
|
|
|
|
panel.injectStyles(cloneStyleTags);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
);
|
2019-02-15 03:45:11 +08:00
|
|
|
|
|
|
|
|
chrome.devtools.network.onNavigated.removeListener(checkPageForReact);
|
|
|
|
|
|
2019-05-23 11:00:21 +08:00
|
|
|
// Re-initialize DevTools panel when a new page is loaded.
|
2019-02-15 03:45:11 +08:00
|
|
|
chrome.devtools.network.onNavigated.addListener(function onNavigated() {
|
2019-05-09 05:22:06 +08:00
|
|
|
// Re-initialize saved filters on navigation,
|
|
|
|
|
// since global values stored on window get reset in this case.
|
2019-07-18 02:12:39 +08:00
|
|
|
syncSavedPreferences();
|
2019-05-09 05:22:06 +08:00
|
|
|
|
2019-03-19 00:36:47 +08:00
|
|
|
// It's easiest to recreate the DevTools panel (to clean up potential stale state).
|
|
|
|
|
// We can revisit this in the future as a small optimization.
|
2020-01-31 01:36:43 +08:00
|
|
|
flushSync(() => root.unmount());
|
|
|
|
|
|
|
|
|
|
initBridgeAndStore();
|
2019-02-15 03:45:11 +08:00
|
|
|
});
|
2019-08-14 12:59:07 +08:00
|
|
|
},
|
2019-01-24 10:06:21 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 03:45:11 +08:00
|
|
|
// Load (or reload) the DevTools extension when the user navigates to a new page.
|
|
|
|
|
function checkPageForReact() {
|
2019-07-18 02:12:39 +08:00
|
|
|
syncSavedPreferences();
|
2019-01-24 10:06:21 +08:00
|
|
|
createPanelIfReactLoaded();
|
2019-02-15 03:45:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.devtools.network.onNavigated.addListener(checkPageForReact);
|
2019-01-24 10:06:21 +08:00
|
|
|
|
|
|
|
|
// Check to see if React has loaded once per second in case React is added
|
|
|
|
|
// after page load
|
|
|
|
|
const loadCheckInterval = setInterval(function() {
|
|
|
|
|
createPanelIfReactLoaded();
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
createPanelIfReactLoaded();
|