Files
Ruslan Lesiutin 1b1dcb8a40 feat[devtools/extension]: show disclaimer when page doesnt run react and refactor react polling logic (#27373)
Changes:
1. Refactored react polling logic, now each `.eval()` call is wrapped in
Promise, so we can chain them properly.
2. When user has browser DevTools opened and React DevTools panels were
mounted, user might navigate to the page, which doesn't have React
running. Previously, we would show just blank white page, now we will
show disclaimer. Disclaimer appears after 5 failed attempts to find
React. We will also show this disclaimer if it takes too long to load
the page, but once any React instance is loaded and registered, we will
update the panels.
3. Dark theme support for this disclaimer and popups in Firefox &
Chromium-based browsers

**Important**: this is only valid for case when React DevTools panels
were already created, like when user started debugging React app and
then switched to non-React page. If user starts to debug non-React app
(by opening browser DevTools for it), we will not create these panels,
just like before.

Q: "Why do we poll to get information about react?"
A: To handle case when react is loaded after the page has been loaded,
some sandboxes for example.

| Before | After |
| --- | --- |
| <img width="1840" alt="Screenshot 2023-09-14 at 15 37 37"
src="https://github.com/facebook/react/assets/28902667/2e6ffb39-5698-461d-bfd6-be2defb41aad">
| <img width="1840" alt="Screenshot 2023-09-14 at 15 26 16"
src="https://github.com/facebook/react/assets/28902667/1c8ad2b7-0955-41c5-b8cc-d0fdb03e13ca">
|
2023-09-20 13:30:50 +01:00

104 lines
2.8 KiB
JavaScript

/* global chrome */
class CouldNotFindReactOnThePageError extends Error {
constructor() {
super("Could not find React, or it hasn't been loaded yet");
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CouldNotFindReactOnThePageError);
}
this.name = 'CouldNotFindReactOnThePageError';
}
}
export function startReactPolling(
onReactFound,
attemptsThreshold,
onCouldNotFindReactAfterReachingAttemptsThreshold,
) {
let status = 'idle';
function abort() {
status = 'aborted';
}
// This function will call onSuccess only if React was found and polling is not aborted, onError will be called for every other case
function checkIfReactPresentInInspectedWindow(onSuccess, onError) {
chrome.devtools.inspectedWindow.eval(
'window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0',
(pageHasReact, exceptionInfo) => {
if (status === 'aborted') {
onError(
'Polling was aborted, user probably navigated to the other page',
);
return;
}
if (exceptionInfo) {
const {code, description, isError, isException, value} =
exceptionInfo;
if (isException) {
onError(
`Received error while checking if react has loaded: ${value}`,
);
return;
}
if (isError) {
onError(
`Received error with code ${code} while checking if react has loaded: "${description}"`,
);
return;
}
}
if (pageHasReact) {
onSuccess();
return;
}
onError(new CouldNotFindReactOnThePageError());
},
);
}
// Just a Promise wrapper around `checkIfReactPresentInInspectedWindow`
// returns a Promise, which will resolve only if React has been found on the page
function poll(attempt) {
return new Promise((resolve, reject) => {
checkIfReactPresentInInspectedWindow(resolve, reject);
}).catch(error => {
if (error instanceof CouldNotFindReactOnThePageError) {
if (attempt === attemptsThreshold) {
onCouldNotFindReactAfterReachingAttemptsThreshold();
}
// Start next attempt in 0.5s
return new Promise(r => setTimeout(r, 500)).then(() =>
poll(attempt + 1),
);
}
// Propagating every other Error
throw error;
});
}
poll(1)
.then(onReactFound)
.catch(error => {
// Log propagated errors only if polling was not aborted
// Some errors are expected when user performs in-tab navigation and `.eval()` is still being executed
if (status === 'aborted') {
return;
}
console.error(error);
});
return {abort};
}