Skip to content

Commit 45142d6

Browse files
committed
fix[devtools/extension]: added a workaround for proxy content script injection in firefox
1 parent 1b1dcb8 commit 45142d6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

packages/react-devtools-extensions/src/background/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ function isNumeric(str: string): boolean {
6060

6161
chrome.runtime.onConnect.addListener(port => {
6262
if (port.name === 'proxy') {
63+
// Might not be present for restricted pages in Firefox
64+
if (port.sender?.tab?.id == null) {
65+
// Not disconnecting it, so it would not reconnect
66+
return;
67+
}
68+
6369
// Proxy content script is executed in tab, so it should have it specified.
6470
const tabId = port.sender.tab.id;
6571

packages/react-devtools-extensions/src/contentScripts/proxy.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,38 @@
22

33
'use strict';
44

5-
let port = null;
6-
let backendInitialized: boolean = false;
7-
8-
connectPort();
9-
sayHelloToBackendManager();
5+
window.addEventListener('pageshow', function ({target}) {
6+
// Firefox's behaviour for injecting this content script can be unpredictable
7+
// While navigating the history, some content scripts might not be re-injected and still be alive
8+
if (!window.__REACT_DEVTOOLS_PROXY_INJECTED__) {
9+
window.__REACT_DEVTOOLS_PROXY_INJECTED__ = true;
1010

11-
// The backend waits to install the global hook until notified by the content script.
12-
// In the event of a page reload, the content script might be loaded before the backend manager is injected.
13-
// Because of this we need to poll the backend manager until it has been initialized.
14-
const intervalID = setInterval(() => {
15-
if (backendInitialized) {
16-
clearInterval(intervalID);
17-
} else {
11+
connectPort();
1812
sayHelloToBackendManager();
13+
14+
// The backend waits to install the global hook until notified by the content script.
15+
// In the event of a page reload, the content script might be loaded before the backend manager is injected.
16+
// Because of this we need to poll the backend manager until it has been initialized.
17+
const intervalID = setInterval(() => {
18+
if (backendInitialized) {
19+
clearInterval(intervalID);
20+
} else {
21+
sayHelloToBackendManager();
22+
}
23+
}, 500);
24+
}
25+
});
26+
27+
window.addEventListener('pagehide', function ({target}) {
28+
if (target !== window.document) {
29+
return;
1930
}
20-
}, 500);
31+
32+
delete window.__REACT_DEVTOOLS_PROXY_INJECTED__;
33+
});
34+
35+
let port = null;
36+
let backendInitialized: boolean = false;
2137

2238
function sayHelloToBackendManager() {
2339
window.postMessage(

0 commit comments

Comments
 (0)