Skip to content

Commit 09c0a93

Browse files
Ensure message correctness in extension (#1907)
* Ensure we check isFeatureEnabled correctly * Only trigger updates on listenForUpdateChanges enabled features (CTL) * Add bail out option for invalid messages
1 parent c25e799 commit 09c0a93

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

injected/entry-points/extension-mv3.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ window.addEventListener(secret, ({ detail: encodedMessage }) => {
2727
case 'register':
2828
if (message.argumentsObject) {
2929
message.argumentsObject.messageSecret = secret;
30+
if (!message.argumentsObject?.site?.enabledFeatures) {
31+
// Potentially corrupted site object, don't init
32+
return;
33+
}
3034
init(message.argumentsObject);
3135
}
3236
break;

injected/src/content-feature.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export default class ContentFeature extends ConfigFeature {
3636
*/
3737
listenForUrlChanges = false;
3838

39+
/**
40+
* Set this to true if you wish to get update calls (legacy).
41+
* @type {boolean}
42+
*/
43+
listenForUpdateChanges = false;
44+
3945
/** @type {ImportMeta} */
4046
#importConfig;
4147

injected/src/content-scope-features.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function alwaysInitExtensionFeatures(args, featureName) {
116116
async function updateFeaturesInner(args) {
117117
const resolvedFeatures = await Promise.all(features);
118118
resolvedFeatures.forEach(({ featureInstance, featureName }) => {
119-
if (!isFeatureBroken(initArgs, featureName) && featureInstance.update) {
119+
if (!isFeatureBroken(initArgs, featureName) && featureInstance.listenForUpdateChanges) {
120120
featureInstance.update(args);
121121
}
122122
});

injected/src/features/click-to-load.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,8 @@ export default class ClickToLoad extends ContentFeature {
17781778
/** @type {MessagingContext} */
17791779
#messagingContext;
17801780

1781+
listenForUpdateChanges = true;
1782+
17811783
async init(args) {
17821784
/**
17831785
* Bail if no messaging backend - this is a debugging feature to ensure we don't

injected/src/utils.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,20 @@ export function iterateDataKey(key, callback) {
246246
}
247247
}
248248

249+
/**
250+
* Check if a feature is considered broken/disabled for the current site
251+
* @param {import('./content-scope-features.js').LoadArgs} args - Configuration arguments containing site information
252+
* @param {string} feature - The feature name to check
253+
* @returns {boolean} True if the feature is broken/disabled, false if it should be enabled
254+
*/
249255
export function isFeatureBroken(args, feature) {
250-
return isPlatformSpecificFeature(feature)
251-
? !args.site.enabledFeatures.includes(feature)
252-
: args.site.isBroken || args.site.allowlisted || !args.site.enabledFeatures.includes(feature);
256+
const isFeatureEnabled = args.site.enabledFeatures?.includes(feature) ?? false;
257+
258+
if (isPlatformSpecificFeature(feature)) {
259+
return !isFeatureEnabled;
260+
}
261+
262+
return args.site.isBroken || args.site.allowlisted || !isFeatureEnabled;
253263
}
254264

255265
export function camelcase(dashCaseText) {

0 commit comments

Comments
 (0)