Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit b94a3ce

Browse files
committed
Merge pull request #166 from spicyj/check-for-react
Show React panel after navigating or when loading React async
2 parents ebe9fea + 6a78b2f commit b94a3ce

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

shells/chrome/src/main.js

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type Panel = { // eslint-disable-line no-unused-vars
2121

2222
declare var chrome: {
2323
devtools: {
24+
network: {
25+
onNavigated: {
26+
addListener: (cb: (url: string) => void) => void,
27+
},
28+
},
2429
panels: {
2530
create: (title: string, icon: string, filename: string, cb: (panel: Panel) => void) => void,
2631
},
@@ -30,26 +35,46 @@ declare var chrome: {
3035
},
3136
};
3237

33-
chrome.devtools.inspectedWindow.eval(`!!(
34-
Object.keys(window.__REACT_DEVTOOLS_GLOBAL_HOOK__._renderers).length || window.React || (window.require && (require('react') || require('React')))
35-
)`, function (pageHasReact, err) {
36-
if (!pageHasReact) {
38+
var panelCreated = false;
39+
40+
function createPanelIfReactLoaded() {
41+
if (panelCreated) {
3742
return;
3843
}
44+
chrome.devtools.inspectedWindow.eval(`!!(
45+
Object.keys(window.__REACT_DEVTOOLS_GLOBAL_HOOK__._renderers).length || window.React
46+
)`, function (pageHasReact, err) {
47+
if (!pageHasReact || panelCreated) {
48+
return;
49+
}
3950

40-
chrome.devtools.panels.create('React', '', 'panel.html', function (panel) {
41-
var reactPanel = null;
42-
panel.onShown.addListener(function (window) {
43-
// when the user switches to the panel, check for an elements tab
44-
// selection
45-
window.panel.getNewSelection();
46-
reactPanel = window.panel;
47-
});
48-
panel.onHidden.addListener(function () {
49-
if (reactPanel) {
50-
reactPanel.hideHighlight();
51-
}
51+
clearInterval(loadCheckInterval);
52+
panelCreated = true;
53+
chrome.devtools.panels.create('React', '', 'panel.html', function (panel) {
54+
var reactPanel = null;
55+
panel.onShown.addListener(function (window) {
56+
// when the user switches to the panel, check for an elements tab
57+
// selection
58+
window.panel.getNewSelection();
59+
reactPanel = window.panel;
60+
});
61+
panel.onHidden.addListener(function () {
62+
if (reactPanel) {
63+
reactPanel.hideHighlight();
64+
}
65+
});
5266
});
5367
});
68+
}
69+
70+
chrome.devtools.network.onNavigated.addListener(function() {
71+
createPanelIfReactLoaded();
5472
});
5573

74+
// Check to see if React has loaded once per second in case React is added
75+
// after page load
76+
var loadCheckInterval = setInterval(function() {
77+
createPanelIfReactLoaded();
78+
}, 1000);
79+
80+
createPanelIfReactLoaded();

0 commit comments

Comments
 (0)