main
js 62 lines 1.94 KB
Raw
1 /* global chrome */
2
3 const contentScriptsToInject = [
4 {
5 id: '@react-devtools/proxy',
6 js: ['build/proxy.js'],
7 matches: ['<all_urls>'],
8 persistAcrossSessions: true,
9 runAt: 'document_start',
10 world: chrome.scripting.ExecutionWorld.ISOLATED,
11 },
12 {
13 id: '@react-devtools/file-fetcher',
14 js: ['build/fileFetcher.js'],
15 matches: ['<all_urls>'],
16 persistAcrossSessions: true,
17 runAt: 'document_end',
18 world: chrome.scripting.ExecutionWorld.ISOLATED,
19 },
20 {
21 id: '@react-devtools/fallback-eval-context',
22 js: ['build/fallbackEvalContext.js'],
23 matches: ['<all_urls>'],
24 persistAcrossSessions: true,
25 runAt: 'document_start',
26 world: chrome.scripting.ExecutionWorld.MAIN,
27 },
28 {
29 id: '@react-devtools/hook',
30 js: ['build/installHook.js'],
31 matches: ['<all_urls>'],
32 persistAcrossSessions: true,
33 runAt: 'document_start',
34 world: chrome.scripting.ExecutionWorld.MAIN,
35 },
36 {
37 id: '@react-devtools/hook-settings-injector',
38 js: ['build/hookSettingsInjector.js'],
39 matches: ['<all_urls>'],
40 persistAcrossSessions: true,
41 runAt: 'document_start',
42 },
43 ];
44
45 async function dynamicallyInjectContentScripts() {
46 try {
47 // Using this, instead of filtering registered scrips with `chrome.scripting.getRegisteredScripts`
48 // because of https://bugs.chromium.org/p/chromium/issues/detail?id=1393762
49 // This fixes registering proxy content script in incognito mode
50 await chrome.scripting.unregisterContentScripts();
51
52 // Note: the "world" option in registerContentScripts is only available in Chrome v102+
53 // It's critical since it allows us to directly run scripts on the "main" world on the page
54 // "document_start" allows it to run before the page's scripts
55 // so the hook can be detected by react reconciler
56 await chrome.scripting.registerContentScripts(contentScriptsToInject);
57 } catch (error) {
58 console.error(error);
59 }
60 }
61
62 dynamicallyInjectContentScripts();