Skip to content

Commit 5a6feba

Browse files
committed
fix[devtools/extension]: added a workaround for proxy content script injection in firefox
1 parent 54c2f2a commit 5a6feba

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
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: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22

33
'use strict';
44

5+
window.addEventListener('unload', function ({target}) {
6+
if (target !== window.document) {
7+
return;
8+
}
9+
10+
delete window.__REACT_DEVTOOLS_PROXY_INJECTED__;
11+
});
12+
513
let port = null;
614
let backendInitialized: boolean = false;
715

8-
connectPort();
9-
sayHelloToBackendManager();
10-
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 {
18-
sayHelloToBackendManager();
19-
}
20-
}, 500);
21-
2216
function sayHelloToBackendManager() {
2317
window.postMessage(
2418
{
@@ -76,3 +70,23 @@ function connectPort() {
7670
port.onMessage.addListener(handleMessageFromDevtools);
7771
port.onDisconnect.addListener(handleDisconnect);
7872
}
73+
74+
// Firefox's behaviour for injecting this content script can be unpredictable
75+
// While navigating the history, some content scripts might not be re-injected and still be alive
76+
if (!window.__REACT_DEVTOOLS_PROXY_INJECTED__) {
77+
window.__REACT_DEVTOOLS_PROXY_INJECTED__ = true;
78+
79+
connectPort();
80+
sayHelloToBackendManager();
81+
82+
// The backend waits to install the global hook until notified by the content script.
83+
// In the event of a page reload, the content script might be loaded before the backend manager is injected.
84+
// Because of this we need to poll the backend manager until it has been initialized.
85+
const intervalID = setInterval(() => {
86+
if (backendInitialized) {
87+
clearInterval(intervalID);
88+
} else {
89+
sayHelloToBackendManager();
90+
}
91+
}, 500);
92+
}

0 commit comments

Comments
 (0)