| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import {evalScripts} from '../evalScripts'; |
| 11 | |
| 12 | window.addEventListener('message', event => { |
| 13 | if (event.data?.source === 'react-devtools-content-script-eval') { |
| 14 | const {scriptId, args, requestId} = event.data.payload; |
| 15 | const response = {result: null, error: null}; |
| 16 | try { |
| 17 | if (!evalScripts[scriptId]) { |
| 18 | throw new Error(`No eval script with id "${scriptId}" exists.`); |
| 19 | } |
| 20 | response.result = evalScripts[scriptId].fn.apply(null, args); |
| 21 | } catch (err) { |
| 22 | response.error = err.message; |
| 23 | } |
| 24 | window.postMessage( |
| 25 | { |
| 26 | source: 'react-devtools-content-script-eval-response', |
| 27 | payload: { |
| 28 | requestId, |
| 29 | response, |
| 30 | }, |
| 31 | }, |
| 32 | '*', |
| 33 | ); |
| 34 | } |
| 35 | }); |