-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[DevTools] upgrade to Manifest V3 #25145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
83e8123
2c8b0bc
9b23880
6ae72f0
12a081c
78f3570
e53b5fe
4835a51
0edb72a
ba9dc3b
5357227
26ba645
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,41 @@ | |
|
||
'use strict'; | ||
|
||
import {IS_FIREFOX} from './utils'; | ||
|
||
const ports = {}; | ||
|
||
const IS_FIREFOX = navigator.userAgent.indexOf('Firefox') >= 0; | ||
if (!IS_FIREFOX) { | ||
// Manifest V3 method of injecting content scripts (not yet supported in Firefox) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi. Not sure if this is the best place to mention it but this is now supported in Firefox and, in fact, feature detection works generally better than user-agent detection. This extension should be trivial to "port" on Firefox MV3 ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for letting me know! Unfortunately we are extremely understaffed now, so we won't be able to improve it soon. I'll make it as a TODO and hope we'll find time. |
||
// Note: the "world" option in registerContentScripts is only available in Chrome v102+ | ||
mondaychen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// It's critical since it allows us to directly run scripts on the "main" world on the page | ||
// "document_start" allows it to run before the page's scripts | ||
// so the hook can be detected by react reconciler | ||
chrome.scripting.registerContentScripts([ | ||
{ | ||
id: 'hook', | ||
matches: ['<all_urls>'], | ||
js: ['build/installHook.js'], | ||
runAt: 'document_start', | ||
world: chrome.scripting.ExecutionWorld.MAIN, | ||
}, | ||
{ | ||
id: 'renderer', | ||
matches: ['<all_urls>'], | ||
js: ['build/renderer.js'], | ||
runAt: 'document_start', | ||
world: chrome.scripting.ExecutionWorld.MAIN, | ||
}, | ||
]); | ||
} | ||
|
||
chrome.runtime.onConnect.addListener(function(port) { | ||
let tab = null; | ||
let name = null; | ||
if (isNumeric(port.name)) { | ||
tab = port.name; | ||
name = 'devtools'; | ||
installContentScript(+port.name); | ||
installProxy(+port.name); | ||
} else { | ||
tab = port.sender.tab.id; | ||
name = 'content-script'; | ||
|
@@ -35,12 +59,15 @@ function isNumeric(str: string): boolean { | |
return +str + '' === str; | ||
} | ||
|
||
function installContentScript(tabId: number) { | ||
chrome.tabs.executeScript( | ||
tabId, | ||
{file: '/build/contentScript.js'}, | ||
function() {}, | ||
); | ||
function installProxy(tabId: number) { | ||
if (IS_FIREFOX) { | ||
chrome.tabs.executeScript(tabId, {file: '/build/proxy.js'}, function() {}); | ||
} else { | ||
chrome.scripting.executeScript({ | ||
target: {tabId: tabId}, | ||
files: ['/build/proxy.js'], | ||
}); | ||
} | ||
} | ||
|
||
function doublePipe(one, two) { | ||
|
@@ -63,18 +90,19 @@ function doublePipe(one, two) { | |
} | ||
|
||
function setIconAndPopup(reactBuildType, tabId) { | ||
chrome.browserAction.setIcon({ | ||
const action = IS_FIREFOX ? chrome.browserAction : chrome.action; | ||
action.setIcon({ | ||
tabId: tabId, | ||
path: { | ||
'16': 'icons/16-' + reactBuildType + '.png', | ||
'32': 'icons/32-' + reactBuildType + '.png', | ||
'48': 'icons/48-' + reactBuildType + '.png', | ||
'128': 'icons/128-' + reactBuildType + '.png', | ||
'16': chrome.runtime.getURL(`icons/16-${reactBuildType}.png`), | ||
'32': chrome.runtime.getURL(`icons/32-${reactBuildType}.png`), | ||
'48': chrome.runtime.getURL(`icons/48-${reactBuildType}.png`), | ||
'128': chrome.runtime.getURL(`icons/128-${reactBuildType}.png`), | ||
}, | ||
}); | ||
chrome.browserAction.setPopup({ | ||
action.setPopup({ | ||
tabId: tabId, | ||
popup: 'popups/' + reactBuildType + '.html', | ||
popup: chrome.runtime.getURL(`popups/${reactBuildType}.html`), | ||
}); | ||
} | ||
|
||
|
@@ -123,9 +151,6 @@ chrome.runtime.onMessage.addListener((request, sender) => { | |
// This is sent from the hook content script. | ||
// It tells us a renderer has attached. | ||
if (request.hasDetectedReact) { | ||
// We use browserAction instead of pageAction because this lets us | ||
// display a custom default popup when React is *not* detected. | ||
// It is specified in the manifest. | ||
mondaychen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setIconAndPopup(request.reactBuildType, id); | ||
} else { | ||
switch (request.payload?.type) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {installHook} from 'react-devtools-shared/src/hook'; | ||
|
||
// avoid double execution | ||
if (!window.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) { | ||
installHook(window); | ||
|
||
// detect react | ||
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.on('renderer', function({ | ||
reactBuildType, | ||
}) { | ||
window.postMessage( | ||
{ | ||
source: 'react-devtools-detector', | ||
reactBuildType, | ||
}, | ||
'*', | ||
); | ||
}); | ||
|
||
// save native values | ||
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeObjectCreate = Object.create; | ||
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeMap = Map; | ||
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeWeakMap = WeakMap; | ||
window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeSet = Set; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.