Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions injected/src/captured-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export const String = globalThis.String;
export const Map = globalThis.Map;
export const Error = globalThis.Error;
export const randomUUID = globalThis.crypto?.randomUUID?.bind(globalThis.crypto);
export const console = globalThis.console;
export const consoleLog = console.log.bind(console);
export const consoleWarn = console.warn.bind(console);
export const consoleError = console.error.bind(console);
8 changes: 4 additions & 4 deletions injected/src/content-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { processAttr } from './utils.js';
import { PerformanceMonitor } from './performance.js';
import { defineProperty, shimInterface, shimProperty, wrapMethod, wrapProperty, wrapToString } from './wrapper-utils.js';
// eslint-disable-next-line no-redeclare
import { Proxy, Reflect } from './captured-globals.js';
import { Proxy, Reflect, consoleLog, consoleWarn, consoleError } from './captured-globals.js';
import { Messaging, MessagingContext } from '../../messaging/index.js';
import { extensionConstructMessagingConfig } from './sendmessage-transport.js';
import { isTrackerOrigin } from './trackers.js';
Expand Down Expand Up @@ -79,19 +79,19 @@ export default class ContentFeature extends ConfigFeature {
if (!shouldLog) {
return () => {};
}
return console.log.bind(console, prefix);
return consoleLog.bind(console, prefix);
},
get warn() {
if (!shouldLog) {
return () => {};
}
return console.warn.bind(console, prefix);
return consoleWarn.bind(console, prefix);
},
get error() {
if (!shouldLog) {
return () => {};
}
return console.error.bind(console, prefix);
return consoleError.bind(console, prefix);
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import {
} from './schema.js';
import { isBeingFramed } from '../../utils.js';

/**
* @typedef {import('../../content-feature.js').default} ContentFeature
*/
/**
* @import { MessagingInterface } from "./schema.js"
* @typedef {Pick<import("../../captured-globals.js"),
Expand All @@ -29,10 +32,11 @@ export const ERROR_MSG = 'Did not install Message Bridge';
*
* @param {string} featureName
* @param {string} [token]
* @param {ContentFeature} [context]
* @return {MessagingInterface}
* @throws {Error}
*/
export function createPageWorldBridge(featureName, token) {
export function createPageWorldBridge(featureName, token, context) {
/**
* This feature never operates without a featureName or token
*/
Expand Down Expand Up @@ -90,7 +94,7 @@ export function createPageWorldBridge(featureName, token) {
throw new captured.Error(ERROR_MSG);
}

return createMessagingInterface(featureName, send, appendToken);
return createMessagingInterface(featureName, send, appendToken, context);
}

/**
Expand All @@ -105,15 +109,17 @@ function random() {
* @param {string} featureName
* @param {(evt: {name: string} & Record<string, any>) => void} send
* @param {(s: string) => string} appendToken
* @param {ContentFeature} [context]
* @returns {MessagingInterface}
*/
function createMessagingInterface(featureName, send, appendToken) {
function createMessagingInterface(featureName, send, appendToken, context) {
return {
/**
* @param {string} method
* @param {Record<string, any>} params
*/
notify(method, params) {
context?.log.info('sending notify', method, params);
send(
new ProxyNotification({
method,
Expand All @@ -129,6 +135,7 @@ function createMessagingInterface(featureName, send, appendToken) {
* @returns {Promise<any>}
*/
request(method, params) {
context?.log.info('sending request', method, params);
const id = random();

send(
Expand All @@ -143,6 +150,7 @@ function createMessagingInterface(featureName, send, appendToken) {
return new Promise((resolve, reject) => {
const responseName = appendToken(ProxyResponse.NAME + '-' + id);
const handler = (/** @type {CustomEvent<unknown>} */ e) => {
context?.log.info('received response', e.detail);
const response = ProxyResponse.create(e.detail);
if (response && response.id === id) {
if ('error' in response && response.error) {
Expand All @@ -164,6 +172,7 @@ function createMessagingInterface(featureName, send, appendToken) {
*/
subscribe(name, callback) {
const id = random();
context?.log.info('subscribing', name);

send(
new SubscriptionRequest({
Expand All @@ -174,6 +183,7 @@ function createMessagingInterface(featureName, send, appendToken) {
);

const handler = (/** @type {CustomEvent<unknown>} */ e) => {
context?.log.info('received subscription response', e.detail);
const subscriptionEvent = SubscriptionResponse.create(e.detail);
if (subscriptionEvent) {
const { id: eventId, params } = subscriptionEvent;
Expand Down
4 changes: 3 additions & 1 deletion injected/src/features/navigator-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class NavigatorInterface extends ContentFeature {
if (!args.platform || !args.platform.name) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
this.defineProperty(Navigator.prototype, 'duckduckgo', {
value: {
platform: args.platform.name,
Expand All @@ -40,7 +42,7 @@ export default class NavigatorInterface extends ContentFeature {
const existingBridge = store[featureName];
if (existingBridge) return existingBridge;

const bridge = createPageWorldBridge(featureName, args.messageSecret);
const bridge = createPageWorldBridge(featureName, args.messageSecret, context);

store[featureName] = bridge;
return bridge;
Expand Down
Loading