Skip to content

Commit cdb8036

Browse files
Merge branch 'main' into jkt/trimLinksAndIncludeImages
2 parents 8562d64 + 3099c61 commit cdb8036

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ injected/src/features/click-to-load.js @duckduckgo/content-scope-scripts-owners
1111
injected/src/features/click-to-load/ @duckduckgo/content-scope-scripts-owners @kzar @ladamski @franfaccin @jonathanKingston @shakyShane
1212
injected/src/locales/click-to-load/ @duckduckgo/content-scope-scripts-owners @kzar @ladamski @franfaccin @jonathanKingston @shakyShane
1313
injected/src/features/autofill-import.js @duckduckgo/content-scope-scripts-owners @dbajpeyi
14+
injected/src/features/page-context.js @duckduckgo/content-scope-scripts-owners @noisysocks
1415

1516
# Broker protection
1617
injected/src/features/broker-protection.js @duckduckgo/content-scope-scripts-owners @duckduckgo/injected-broker-protection

injected/scripts/utils/build.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { commentPlugin } from './comment-plugin.js';
77
const ROOT = join(cwd(import.meta.url), '..', '..');
88
const DEBUG = false;
99

10-
const prefixMessage = '/*! © DuckDuckGo ContentScopeScripts protections https://github.com/duckduckgo/content-scope-scripts/ */';
10+
const prefixMessage = '/*! © DuckDuckGo ContentScopeScripts $INJECT_NAME$ https://github.com/duckduckgo/content-scope-scripts/ */';
1111

1212
/**
1313
* @param {object} params
@@ -30,6 +30,8 @@ export async function bundle(params) {
3030
const loadFeaturesPlugin = loadFeatures(platform, featureNames);
3131
// The code is using a global, that we define here which means once tree shaken we get a browser specific output.
3232

33+
const outputPrefixName = prefixMessage.replace('$INJECT_NAME$', platform);
34+
3335
/** @type {import("esbuild").BuildOptions} */
3436
const buildOptions = {
3537
entryPoints: [scriptPath],
@@ -52,7 +54,7 @@ export async function bundle(params) {
5254
},
5355
plugins: [loadFeaturesPlugin, commentPlugin()],
5456
banner: {
55-
js: prefixMessage,
57+
js: outputPrefixName,
5658
},
5759
};
5860

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;

injected/src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ export function withRetry(fn, maxAttempts = 4, delay = 500, strategy = 'exponent
846846
export function isDuckAi() {
847847
const tabUrl = getTabUrl();
848848
const domains = ['duckduckgo.com', 'duck.ai', 'duck.co'];
849-
if (tabUrl?.hostname && domains.includes(tabUrl?.hostname)) {
849+
if (tabUrl?.hostname && domains.some((domain) => matchHostname(tabUrl?.hostname, domain))) {
850850
const url = new URL(tabUrl?.href);
851851
return url.searchParams.has('duckai') || url.searchParams.get('ia') === 'chat';
852852
}

0 commit comments

Comments
 (0)