mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
feat[react-devtools]: support Manifest v3 for Firefox extension (#30824)
Firefox [finally supports `ExecutionWorld.MAIN`](https://bugzilla.mozilla.org/show_bug.cgi?id=1736575) in content scripts, which means we can migrate the browser extension to Manifest V3. This PR also removes a bunch of no longer required explicit branching for Firefox case, when we are using Manifest V3-only APIs. We are also removing XMLHttpRequest injection, which is no longer needed and restricted in Manifest V3. The new standardized approach (same as in Chromium) doesn't violate CSP rules, which means that extension can finally be used for apps running in production mode.
This commit is contained in:
1 parent
fc0df475c4
commit
537c74e16a
7 files changed
+66
-175
No files matched your search
@@ -1,31 +1,5 @@
|
||||
/* global chrome */
|
||||
|
||||
import nullthrows from 'nullthrows';
|
||||
|
||||
// We run scripts on the page via the service worker (background/index.js) for
|
||||
// Manifest V3 extensions (Chrome & Edge).
|
||||
// We need to inject this code for Firefox only because it does not support ExecutionWorld.MAIN
|
||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/ExecutionWorld
|
||||
// In this content script we have access to DOM, but don't have access to the webpage's window,
|
||||
// so we inject this inline script tag into the webpage (allowed in Manifest V2).
|
||||
function injectScriptSync(src) {
|
||||
let code = '';
|
||||
const request = new XMLHttpRequest();
|
||||
request.addEventListener('load', function () {
|
||||
code = this.responseText;
|
||||
});
|
||||
request.open('GET', src, false);
|
||||
request.send();
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.textContent = code;
|
||||
|
||||
// This script runs before the <head> element is created,
|
||||
// so we add the script to <html> instead.
|
||||
nullthrows(document.documentElement).appendChild(script);
|
||||
nullthrows(script.parentNode).removeChild(script);
|
||||
}
|
||||
|
||||
let lastSentDevToolsHookMessage;
|
||||
|
||||
// We want to detect when a renderer attaches, and notify the "background page"
|
||||
@@ -60,17 +34,3 @@ window.addEventListener('pageshow', function ({target}) {
|
||||
|
||||
chrome.runtime.sendMessage(lastSentDevToolsHookMessage);
|
||||
});
|
||||
|
||||
if (__IS_FIREFOX__) {
|
||||
injectScriptSync(chrome.runtime.getURL('build/renderer.js'));
|
||||
|
||||
// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.
|
||||
// Only do this for HTML documents though, to avoid e.g. breaking syntax highlighting for XML docs.
|
||||
switch (document.contentType) {
|
||||
case 'text/html':
|
||||
case 'application/xhtml+xml': {
|
||||
injectScriptSync(chrome.runtime.getURL('build/installHook.js'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user