Skip to content

Commit 3099c61

Browse files
Improve logging for features (#2004)
* Improve logging for features * Lint fix
1 parent 77b0a64 commit 3099c61

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

injected/src/captured-globals.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export const String = globalThis.String;
2424
export const Map = globalThis.Map;
2525
export const Error = globalThis.Error;
2626
export const randomUUID = globalThis.crypto?.randomUUID?.bind(globalThis.crypto);
27+
export const console = globalThis.console;
28+
export const consoleLog = console.log.bind(console);
29+
export const consoleWarn = console.warn.bind(console);
30+
export const consoleError = console.error.bind(console);

injected/src/content-feature.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { processAttr } from './utils.js';
22
import { PerformanceMonitor } from './performance.js';
33
import { defineProperty, shimInterface, shimProperty, wrapMethod, wrapProperty, wrapToString } from './wrapper-utils.js';
44
// eslint-disable-next-line no-redeclare
5-
import { Proxy, Reflect } from './captured-globals.js';
5+
import { Proxy, Reflect, consoleLog, consoleWarn, consoleError } from './captured-globals.js';
66
import { Messaging, MessagingContext } from '../../messaging/index.js';
77
import { extensionConstructMessagingConfig } from './sendmessage-transport.js';
88
import { isTrackerOrigin } from './trackers.js';
@@ -79,19 +79,19 @@ export default class ContentFeature extends ConfigFeature {
7979
if (!shouldLog) {
8080
return () => {};
8181
}
82-
return console.log.bind(console, prefix);
82+
return consoleLog.bind(console, prefix);
8383
},
8484
get warn() {
8585
if (!shouldLog) {
8686
return () => {};
8787
}
88-
return console.warn.bind(console, prefix);
88+
return consoleWarn.bind(console, prefix);
8989
},
9090
get error() {
9191
if (!shouldLog) {
9292
return () => {};
9393
}
94-
return console.error.bind(console, prefix);
94+
return consoleError.bind(console, prefix);
9595
},
9696
};
9797
}

injected/src/features/message-bridge/create-page-world-bridge.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
} from './schema.js';
1212
import { isBeingFramed } from '../../utils.js';
1313

14+
/**
15+
* @typedef {import('../../content-feature.js').default} ContentFeature
16+
*/
1417
/**
1518
* @import { MessagingInterface } from "./schema.js"
1619
* @typedef {Pick<import("../../captured-globals.js"),
@@ -29,10 +32,11 @@ export const ERROR_MSG = 'Did not install Message Bridge';
2932
*
3033
* @param {string} featureName
3134
* @param {string} [token]
35+
* @param {ContentFeature} [context]
3236
* @return {MessagingInterface}
3337
* @throws {Error}
3438
*/
35-
export function createPageWorldBridge(featureName, token) {
39+
export function createPageWorldBridge(featureName, token, context) {
3640
/**
3741
* This feature never operates without a featureName or token
3842
*/
@@ -90,7 +94,7 @@ export function createPageWorldBridge(featureName, token) {
9094
throw new captured.Error(ERROR_MSG);
9195
}
9296

93-
return createMessagingInterface(featureName, send, appendToken);
97+
return createMessagingInterface(featureName, send, appendToken, context);
9498
}
9599

96100
/**
@@ -105,15 +109,17 @@ function random() {
105109
* @param {string} featureName
106110
* @param {(evt: {name: string} & Record<string, any>) => void} send
107111
* @param {(s: string) => string} appendToken
112+
* @param {ContentFeature} [context]
108113
* @returns {MessagingInterface}
109114
*/
110-
function createMessagingInterface(featureName, send, appendToken) {
115+
function createMessagingInterface(featureName, send, appendToken, context) {
111116
return {
112117
/**
113118
* @param {string} method
114119
* @param {Record<string, any>} params
115120
*/
116121
notify(method, params) {
122+
context?.log.info('sending notify', method, params);
117123
send(
118124
new ProxyNotification({
119125
method,
@@ -129,6 +135,7 @@ function createMessagingInterface(featureName, send, appendToken) {
129135
* @returns {Promise<any>}
130136
*/
131137
request(method, params) {
138+
context?.log.info('sending request', method, params);
132139
const id = random();
133140

134141
send(
@@ -143,6 +150,7 @@ function createMessagingInterface(featureName, send, appendToken) {
143150
return new Promise((resolve, reject) => {
144151
const responseName = appendToken(ProxyResponse.NAME + '-' + id);
145152
const handler = (/** @type {CustomEvent<unknown>} */ e) => {
153+
context?.log.info('received response', e.detail);
146154
const response = ProxyResponse.create(e.detail);
147155
if (response && response.id === id) {
148156
if ('error' in response && response.error) {
@@ -164,6 +172,7 @@ function createMessagingInterface(featureName, send, appendToken) {
164172
*/
165173
subscribe(name, callback) {
166174
const id = random();
175+
context?.log.info('subscribing', name);
167176

168177
send(
169178
new SubscriptionRequest({
@@ -174,6 +183,7 @@ function createMessagingInterface(featureName, send, appendToken) {
174183
);
175184

176185
const handler = (/** @type {CustomEvent<unknown>} */ e) => {
186+
context?.log.info('received subscription response', e.detail);
177187
const subscriptionEvent = SubscriptionResponse.create(e.detail);
178188
if (subscriptionEvent) {
179189
const { id: eventId, params } = subscriptionEvent;

injected/src/features/navigator-interface.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default class NavigatorInterface extends ContentFeature {
2424
if (!args.platform || !args.platform.name) {
2525
return;
2626
}
27+
// eslint-disable-next-line @typescript-eslint/no-this-alias
28+
const context = this;
2729
this.defineProperty(Navigator.prototype, 'duckduckgo', {
2830
value: {
2931
platform: args.platform.name,
@@ -40,7 +42,7 @@ export default class NavigatorInterface extends ContentFeature {
4042
const existingBridge = store[featureName];
4143
if (existingBridge) return existingBridge;
4244

43-
const bridge = createPageWorldBridge(featureName, args.messageSecret);
45+
const bridge = createPageWorldBridge(featureName, args.messageSecret, context);
4446

4547
store[featureName] = bridge;
4648
return bridge;

0 commit comments

Comments
 (0)