1. /* global chrome */
    
  2. 
    
  3. // Firefox doesn't support ExecutionWorld.MAIN yet
    
  4. // equivalent logic for Firefox is in prepareInjection.js
    
  5. const contentScriptsToInject = __IS_FIREFOX__
    
  6.   ? [
    
  7.       {
    
  8.         id: '@react-devtools/proxy',
    
  9.         js: ['build/proxy.js'],
    
  10.         matches: ['<all_urls>'],
    
  11.         persistAcrossSessions: true,
    
  12.         runAt: 'document_end',
    
  13.       },
    
  14.       {
    
  15.         id: '@react-devtools/file-fetcher',
    
  16.         js: ['build/fileFetcher.js'],
    
  17.         matches: ['<all_urls>'],
    
  18.         persistAcrossSessions: true,
    
  19.         runAt: 'document_end',
    
  20.       },
    
  21.     ]
    
  22.   : [
    
  23.       {
    
  24.         id: '@react-devtools/proxy',
    
  25.         js: ['build/proxy.js'],
    
  26.         matches: ['<all_urls>'],
    
  27.         persistAcrossSessions: true,
    
  28.         runAt: 'document_end',
    
  29.         world: chrome.scripting.ExecutionWorld.ISOLATED,
    
  30.       },
    
  31.       {
    
  32.         id: '@react-devtools/file-fetcher',
    
  33.         js: ['build/fileFetcher.js'],
    
  34.         matches: ['<all_urls>'],
    
  35.         persistAcrossSessions: true,
    
  36.         runAt: 'document_end',
    
  37.         world: chrome.scripting.ExecutionWorld.ISOLATED,
    
  38.       },
    
  39.       {
    
  40.         id: '@react-devtools/hook',
    
  41.         js: ['build/installHook.js'],
    
  42.         matches: ['<all_urls>'],
    
  43.         persistAcrossSessions: true,
    
  44.         runAt: 'document_start',
    
  45.         world: chrome.scripting.ExecutionWorld.MAIN,
    
  46.       },
    
  47.       {
    
  48.         id: '@react-devtools/renderer',
    
  49.         js: ['build/renderer.js'],
    
  50.         matches: ['<all_urls>'],
    
  51.         persistAcrossSessions: true,
    
  52.         runAt: 'document_start',
    
  53.         world: chrome.scripting.ExecutionWorld.MAIN,
    
  54.       },
    
  55.     ];
    
  56. 
    
  57. async function dynamicallyInjectContentScripts() {
    
  58.   try {
    
  59.     // Using this, instead of filtering registered scrips with `chrome.scripting.getRegisteredScripts`
    
  60.     // because of https://bugs.chromium.org/p/chromium/issues/detail?id=1393762
    
  61.     // This fixes registering proxy content script in incognito mode
    
  62.     await chrome.scripting.unregisterContentScripts();
    
  63. 
    
  64.     // equivalent logic for Firefox is in prepareInjection.js
    
  65.     // Manifest V3 method of injecting content script
    
  66.     // TODO(hoxyq): migrate Firefox to V3 manifests
    
  67.     // Note: the "world" option in registerContentScripts is only available in Chrome v102+
    
  68.     // It's critical since it allows us to directly run scripts on the "main" world on the page
    
  69.     // "document_start" allows it to run before the page's scripts
    
  70.     // so the hook can be detected by react reconciler
    
  71.     await chrome.scripting.registerContentScripts(contentScriptsToInject);
    
  72.   } catch (error) {
    
  73.     console.error(error);
    
  74.   }
    
  75. }
    
  76. 
    
  77. dynamicallyInjectContentScripts();