Skip to content

Commit

Permalink
fix[devtools/extension]: added a workaround for proxy content script …
Browse files Browse the repository at this point in the history
…injection in firefox
  • Loading branch information
hoxyq committed Sep 20, 2023
1 parent 1b1dcb8 commit 45142d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions packages/react-devtools-extensions/src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ function isNumeric(str: string): boolean {

chrome.runtime.onConnect.addListener(port => {
if (port.name === 'proxy') {
// Might not be present for restricted pages in Firefox
if (port.sender?.tab?.id == null) {
// Not disconnecting it, so it would not reconnect
return;
}

// Proxy content script is executed in tab, so it should have it specified.
const tabId = port.sender.tab.id;

Expand Down
42 changes: 29 additions & 13 deletions packages/react-devtools-extensions/src/contentScripts/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

'use strict';

let port = null;
let backendInitialized: boolean = false;

connectPort();
sayHelloToBackendManager();
window.addEventListener('pageshow', function ({target}) {
// Firefox's behaviour for injecting this content script can be unpredictable
// While navigating the history, some content scripts might not be re-injected and still be alive
if (!window.__REACT_DEVTOOLS_PROXY_INJECTED__) {
window.__REACT_DEVTOOLS_PROXY_INJECTED__ = true;

// The backend waits to install the global hook until notified by the content script.
// In the event of a page reload, the content script might be loaded before the backend manager is injected.
// Because of this we need to poll the backend manager until it has been initialized.
const intervalID = setInterval(() => {
if (backendInitialized) {
clearInterval(intervalID);
} else {
connectPort();
sayHelloToBackendManager();

// The backend waits to install the global hook until notified by the content script.
// In the event of a page reload, the content script might be loaded before the backend manager is injected.
// Because of this we need to poll the backend manager until it has been initialized.
const intervalID = setInterval(() => {
if (backendInitialized) {
clearInterval(intervalID);
} else {
sayHelloToBackendManager();
}
}, 500);
}
});

window.addEventListener('pagehide', function ({target}) {
if (target !== window.document) {
return;
}
}, 500);

delete window.__REACT_DEVTOOLS_PROXY_INJECTED__;
});

let port = null;
let backendInitialized: boolean = false;

function sayHelloToBackendManager() {
window.postMessage(
Expand Down

0 comments on commit 45142d6

Please sign in to comment.