1. /* global chrome */
    
  2. 
    
  3. import setExtensionIconAndPopup from './setExtensionIconAndPopup';
    
  4. import {executeScriptInMainWorld} from './executeScript';
    
  5. 
    
  6. import {EXTENSION_CONTAINED_VERSIONS} from '../utils';
    
  7. 
    
  8. export function handleReactDevToolsHookMessage(message, sender) {
    
  9.   const {payload} = message;
    
  10. 
    
  11.   switch (payload?.type) {
    
  12.     case 'react-renderer-attached': {
    
  13.       setExtensionIconAndPopup(payload.reactBuildType, sender.tab.id);
    
  14. 
    
  15.       break;
    
  16.     }
    
  17.   }
    
  18. }
    
  19. 
    
  20. export function handleBackendManagerMessage(message, sender) {
    
  21.   const {payload} = message;
    
  22. 
    
  23.   switch (payload?.type) {
    
  24.     case 'require-backends': {
    
  25.       payload.versions.forEach(version => {
    
  26.         if (EXTENSION_CONTAINED_VERSIONS.includes(version)) {
    
  27.           executeScriptInMainWorld({
    
  28.             target: {tabId: sender.tab.id},
    
  29.             files: [`/build/react_devtools_backend_${version}.js`],
    
  30.           });
    
  31.         }
    
  32.       });
    
  33. 
    
  34.       break;
    
  35.     }
    
  36.   }
    
  37. }
    
  38. 
    
  39. export function handleDevToolsPageMessage(message) {
    
  40.   const {payload} = message;
    
  41. 
    
  42.   switch (payload?.type) {
    
  43.     // Proxy this message from DevTools page to content script via chrome.tabs.sendMessage
    
  44.     case 'fetch-file-with-cache': {
    
  45.       const {
    
  46.         payload: {tabId, url},
    
  47.       } = message;
    
  48. 
    
  49.       if (!tabId) {
    
  50.         throw new Error("Couldn't fetch file sources: tabId not specified");
    
  51.       }
    
  52. 
    
  53.       if (!url) {
    
  54.         throw new Error("Couldn't fetch file sources: url not specified");
    
  55.       }
    
  56. 
    
  57.       chrome.tabs.sendMessage(tabId, {
    
  58.         source: 'devtools-page',
    
  59.         payload: {
    
  60.           type: 'fetch-file-with-cache',
    
  61.           url,
    
  62.         },
    
  63.       });
    
  64. 
    
  65.       break;
    
  66.     }
    
  67. 
    
  68.     case 'inject-backend-manager': {
    
  69.       const {
    
  70.         payload: {tabId},
    
  71.       } = message;
    
  72. 
    
  73.       if (!tabId) {
    
  74.         throw new Error("Couldn't inject backend manager: tabId not specified");
    
  75.       }
    
  76. 
    
  77.       executeScriptInMainWorld({
    
  78.         target: {tabId},
    
  79.         files: ['/build/backendManager.js'],
    
  80.       });
    
  81. 
    
  82.       break;
    
  83.     }
    
  84.   }
    
  85. }
    
  86. 
    
  87. export function handleFetchResourceContentScriptMessage(message) {
    
  88.   const {payload} = message;
    
  89. 
    
  90.   switch (payload?.type) {
    
  91.     case 'fetch-file-with-cache-complete':
    
  92.     case 'fetch-file-with-cache-error':
    
  93.       // Forward the result of fetch-in-page requests back to the DevTools page.
    
  94.       // We switch the source here because of inconsistency between Firefox and Chrome
    
  95.       // In Chromium this message will be propagated from content script to DevTools page
    
  96.       // For Firefox, only background script will get this message, so we need to forward it to DevTools page
    
  97.       chrome.runtime.sendMessage({
    
  98.         source: 'react-devtools-background',
    
  99.         payload,
    
  100.       });
    
  101.       break;
    
  102.   }
    
  103. }