main
js 112 lines 3.29 KB
Raw
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 export type EvalScriptIds =
11 | 'checkIfReactPresentInInspectedWindow'
12 | 'reload'
13 | 'setBrowserSelectionFromReact'
14 | 'setReactSelectionFromBrowser'
15 | 'viewAttributeSource'
16 | 'viewElementSource';
17
18 /*
19 .fn for fallback in Content Script context
20 .code for chrome.devtools.inspectedWindow.eval()
21 */
22 type EvalScriptEntry = {
23 fn: (...args: any[]) => any,
24 code: (...args: any[]) => string,
25 };
26
27 /*
28 Can not access `Developer Tools Console API` (e.g., inspect(), $0) in this context.
29 So some fallback functions are no-op or throw error.
30 */
31 export const evalScripts: {[key: EvalScriptIds]: EvalScriptEntry} = {
32 checkIfReactPresentInInspectedWindow: {
33 fn: () =>
34 window.__REACT_DEVTOOLS_GLOBAL_HOOK__ &&
35 window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0,
36 code: () =>
37 'window.__REACT_DEVTOOLS_GLOBAL_HOOK__ &&' +
38 'window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0',
39 },
40 reload: {
41 fn: () => window.location.reload(),
42 code: () => 'window.location.reload();',
43 },
44 setBrowserSelectionFromReact: {
45 fn: () => {
46 throw new Error('Not supported in fallback eval context');
47 },
48 code: () =>
49 '(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
50 '(inspect(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0), true) :' +
51 'false',
52 },
53 setReactSelectionFromBrowser: {
54 fn: () => {
55 throw new Error('Not supported in fallback eval context');
56 },
57 code: () =>
58 '(window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 !== $0) ?' +
59 '(window.__REACT_DEVTOOLS_GLOBAL_HOOK__.$0 = $0, true) :' +
60 'false',
61 },
62 viewAttributeSource: {
63 fn: ({rendererID, elementID, path}) => {
64 return false; // Not supported in fallback eval context
65 },
66 code: ({rendererID, elementID, path}) =>
67 '{' + // The outer block is important because it means we can declare local variables.
68 'const renderer = window.__REACT_DEVTOOLS_GLOBAL_HOOK__.rendererInterfaces.get(' +
69 JSON.stringify(rendererID) +
70 ');' +
71 'if (renderer) {' +
72 ' const value = renderer.getElementAttributeByPath(' +
73 JSON.stringify(elementID) +
74 ',' +
75 JSON.stringify(path) +
76 ');' +
77 ' if (value) {' +
78 ' inspect(value);' +
79 ' true;' +
80 ' } else {' +
81 ' false;' +
82 ' }' +
83 '} else {' +
84 ' false;' +
85 '}' +
86 '}',
87 },
88 viewElementSource: {
89 fn: ({rendererID, elementID}) => {
90 return false; // Not supported in fallback eval context
91 },
92 code: ({rendererID, elementID}) =>
93 '{' + // The outer block is important because it means we can declare local variables.
94 'const renderer = window.__REACT_DEVTOOLS_GLOBAL_HOOK__.rendererInterfaces.get(' +
95 JSON.stringify(rendererID) +
96 ');' +
97 'if (renderer) {' +
98 ' const value = renderer.getElementSourceFunctionById(' +
99 JSON.stringify(elementID) +
100 ');' +
101 ' if (value) {' +
102 ' inspect(value);' +
103 ' true;' +
104 ' } else {' +
105 ' false;' +
106 ' }' +
107 '} else {' +
108 ' false;' +
109 '}' +
110 '}',
111 },
112 };