| 1 | /* global chrome */ |
| 2 | /** @flow */ |
| 3 | |
| 4 | // We can't use chrome.storage domain from scripts which are injected in ExecutionWorld.MAIN |
| 5 | // This is the only purpose of this script - to send persisted settings to installHook.js content script |
| 6 | |
| 7 | import type {UnknownMessageEvent} from './messages'; |
| 8 | import type { |
| 9 | DevToolsHookSettings, |
| 10 | DevToolsSettings, |
| 11 | } from 'react-devtools-shared/src/backend/types'; |
| 12 | import type {ComponentFilter} from 'react-devtools-shared/src/frontend/types'; |
| 13 | import {postMessage} from './messages'; |
| 14 | |
| 15 | import {getDefaultComponentFilters} from 'react-devtools-shared/src/utils'; |
| 16 | |
| 17 | async function messageListener(event: UnknownMessageEvent) { |
| 18 | if (event.source !== window) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (event.data.source === 'react-devtools-hook-installer') { |
| 23 | if (event.data.payload.handshake) { |
| 24 | const settings: Partial<DevToolsSettings> = |
| 25 | await chrome.storage.local.get(); |
| 26 | // If storage was empty (first installation), define default settings |
| 27 | const hookSettings: DevToolsHookSettings = { |
| 28 | appendComponentStack: |
| 29 | typeof settings.appendComponentStack === 'boolean' |
| 30 | ? settings.appendComponentStack |
| 31 | : true, |
| 32 | breakOnConsoleErrors: |
| 33 | typeof settings.breakOnConsoleErrors === 'boolean' |
| 34 | ? settings.breakOnConsoleErrors |
| 35 | : false, |
| 36 | showInlineWarningsAndErrors: |
| 37 | typeof settings.showInlineWarningsAndErrors === 'boolean' |
| 38 | ? settings.showInlineWarningsAndErrors |
| 39 | : true, |
| 40 | hideConsoleLogsInStrictMode: |
| 41 | typeof settings.hideConsoleLogsInStrictMode === 'boolean' |
| 42 | ? settings.hideConsoleLogsInStrictMode |
| 43 | : false, |
| 44 | disableSecondConsoleLogDimmingInStrictMode: |
| 45 | typeof settings.disableSecondConsoleLogDimmingInStrictMode === |
| 46 | 'boolean' |
| 47 | ? settings.disableSecondConsoleLogDimmingInStrictMode |
| 48 | : false, |
| 49 | }; |
| 50 | const componentFilters: Array<ComponentFilter> = Array.isArray( |
| 51 | settings.componentFilters, |
| 52 | ) |
| 53 | ? settings.componentFilters |
| 54 | : getDefaultComponentFilters(); |
| 55 | |
| 56 | postMessage({ |
| 57 | source: 'react-devtools-settings-injector', |
| 58 | payload: {hookSettings, componentFilters}, |
| 59 | }); |
| 60 | |
| 61 | window.removeEventListener('message', messageListener); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | window.addEventListener('message', messageListener); |
| 67 | postMessage({ |
| 68 | source: 'react-devtools-settings-injector', |
| 69 | payload: {handshake: true}, |
| 70 | }); |