-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactored devtools browser extension scripts to improve po…
…rts management and service worker lifetime
- Loading branch information
Showing
22 changed files
with
1,159 additions
and
847 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
packages/react-devtools-extensions/src/background/dynamicallyInjectContentScripts.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* global chrome */ | ||
|
||
import {IS_FIREFOX} from '../utils'; | ||
|
||
async function dynamicallyInjectContentScripts() { | ||
const contentScriptsToInject = [ | ||
{ | ||
id: '@react-devtools/hook', | ||
js: ['build/installHook.js'], | ||
matches: ['<all_urls>'], | ||
persistAcrossSessions: true, | ||
runAt: 'document_start', | ||
world: chrome.scripting.ExecutionWorld.MAIN, | ||
}, | ||
{ | ||
id: '@react-devtools/renderer', | ||
js: ['build/renderer.js'], | ||
matches: ['<all_urls>'], | ||
persistAcrossSessions: true, | ||
runAt: 'document_start', | ||
world: chrome.scripting.ExecutionWorld.MAIN, | ||
}, | ||
]; | ||
|
||
try { | ||
const alreadyRegisteredContentScripts = | ||
await chrome.scripting.getRegisteredContentScripts(); | ||
|
||
const scriptsToInjectNow = contentScriptsToInject.filter( | ||
scriptToInject => | ||
!alreadyRegisteredContentScripts.some( | ||
registeredScript => registeredScript.id === scriptToInject.id, | ||
), | ||
); | ||
|
||
if (scriptsToInjectNow.length) { | ||
// equivalent logic for Firefox is in prepareInjection.js | ||
// Manifest V3 method of injecting content script | ||
// TODO(hoxyq): migrate Firefox to V3 manifests | ||
// Note: the "world" option in registerContentScripts is only available in Chrome v102+ | ||
// 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 | ||
await chrome.scripting.registerContentScripts(scriptsToInjectNow); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
if (!IS_FIREFOX) { | ||
dynamicallyInjectContentScripts(); | ||
} |
Oops, something went wrong.