[DevTools] Log page URL in internal build (#24799)
* test log * fix attribute name * fix lint * tabs can be empty * improve coding style per comments
Mengdi Chen committed
Jun 29, 2022 at 13:02 UTC
f01e119b7d6b32104d37a7a9247fdd5a25bd29d2
3 files changed
+26
-4
packages/react-devtools-extensions/src/main.js
+11
-1
@@ -115,7 +115,17 @@ function createPanelIfReactLoaded() {
115
116
const tabId = chrome.devtools.inspectedWindow.tabId;
117
118
- registerDevToolsEventLogger('extension');
118
+ registerDevToolsEventLogger('extension', async () => {
119
+ // TODO: after we upgrade to Manifest V3, chrome.tabs.query returns a Promise
120
+ // without the callback.
121
+ return new Promise(resolve => {
122
+ chrome.tabs.query({active: true, currentWindow: true}, tabs => {
123
+ resolve({
124
+ page_url: tabs[0]?.url,
125
+ });
126
+ });
127
+ });
128
+ });
129
130
function initBridgeAndStore() {
131
const port = chrome.runtime.connect({
packages/react-devtools-shared/src/Logger.js
+1
-1
@@ -33,7 +33,7 @@ export type LogEvent =
33
+inspected_element_number_of_hooks: number | null,
34
|};
35
36
-export type LogFunction = LogEvent => void;
36
+export type LogFunction = LogEvent => void | Promise<void>;
37
38
let logFunctions: Array<LogFunction> = [];
39
export const logEvent: LogFunction =
packages/react-devtools-shared/src/registerDevToolsEventLogger.js
+14
-2
@@ -15,8 +15,17 @@ import {enableLogger} from 'react-devtools-feature-flags';
15
let loggingIFrame = null;
16
let missedEvents = [];
17
18
-export function registerDevToolsEventLogger(surface: string) {
19
- function logEvent(event: LogEvent) {
18
+type LoggerContext = {|
19
+ page_url: ?string,
20
+|};
21
+
22
+export function registerDevToolsEventLogger(
23
+ surface: string,
24
+ fetchAdditionalContext: ?() =>
25
+ | LoggerContext
26
+ | ?(() => Promise<LoggerContext>),
27
+): void {
28
+ async function logEvent(event: LogEvent) {
29
if (enableLogger) {
30
if (loggingIFrame != null) {
31
loggingIFrame.contentWindow.postMessage(
@@ -26,6 +35,9 @@ export function registerDevToolsEventLogger(surface: string) {
35
context: {
36
surface,
37
version: process.env.DEVTOOLS_VERSION,
38
+ ...(fetchAdditionalContext != null
39
+ ? await fetchAdditionalContext()
40
+ : {}),
41
},
42
},
43
'*',