Skip to content

Commit da35983

Browse files
Inject name conditional (#1884)
* Add enum devices debugging * Disable device enumeration remotely * Fix lint * Add frame flexibility * Move to webCompat * Conditional frame matching * Test case * Frame test changes, still not working * Fix up frame testing * Simplify test checks * Remove bundle for debugging * Add injectName conditional matching * Improve comment
1 parent c1eeeeb commit da35983

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

injected/src/config-feature.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ export default class ConfigFeature {
7878
return this.#args?.featureSettings;
7979
}
8080

81+
/**
82+
* Getter for injectName, will be overridden by subclasses (namely ContentFeature)
83+
* @returns {string | undefined}
84+
*/
85+
get injectName() {
86+
return undefined;
87+
}
88+
8189
/**
8290
* Given a config key, interpret the value as a list of conditionals objects, and return the elements that match the current page
8391
* Consider in your feature using patchSettings instead as per `getFeatureSetting`.
@@ -122,6 +130,7 @@ export default class ConfigFeature {
122130
* @property {object} [context]
123131
* @property {boolean} [context.frame] - true if the condition applies to frames
124132
* @property {boolean} [context.top] - true if the condition applies to the top frame
133+
* @property {string} [injectName] - the inject name to match against (e.g., "apple-isolated")
125134
*/
126135

127136
/**
@@ -151,6 +160,7 @@ export default class ConfigFeature {
151160
urlPattern: this._matchUrlPatternConditional,
152161
experiment: this._matchExperimentConditional,
153162
minSupportedVersion: this._matchMinSupportedVersion,
163+
injectName: this._matchInjectNameConditional,
154164
};
155165

156166
for (const key in conditionBlock) {
@@ -261,6 +271,19 @@ export default class ConfigFeature {
261271
return matchHostname(domain, conditionBlock.domain);
262272
}
263273

274+
/**
275+
* Takes a condition block and returns true if the current inject name matches the injectName.
276+
* @param {ConditionBlock} conditionBlock
277+
* @returns {boolean}
278+
*/
279+
_matchInjectNameConditional(conditionBlock) {
280+
if (!conditionBlock.injectName) return false;
281+
// Access injectName through the ContentFeature's getter
282+
const currentInjectName = this.injectName;
283+
if (!currentInjectName) return false;
284+
return conditionBlock.injectName === currentInjectName;
285+
}
286+
264287
/**
265288
* Takes a condition block and returns true if the platform version satisfies the `minSupportedFeature`
266289
* @param {ConditionBlock} conditionBlock

injected/unit-test/content-feature.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,108 @@ describe('ContentFeature class', () => {
489489
expect(object.someProp.toString.toString.toString()).not.toBe(fn.toString.toString.toString());
490490
});
491491
});
492+
493+
describe('injectName condition', () => {
494+
it('should match when injectName condition is met', () => {
495+
class MyTestFeature extends ContentFeature {
496+
/** @returns {'apple-isolated'} */
497+
get injectName() {
498+
return 'apple-isolated';
499+
}
500+
501+
testMatchInjectNameConditional(conditionBlock) {
502+
return this._matchInjectNameConditional(conditionBlock);
503+
}
504+
}
505+
506+
const args = {
507+
site: {
508+
domain: 'example.com',
509+
url: 'http://example.com',
510+
},
511+
};
512+
513+
const feature = new MyTestFeature('test', {}, args);
514+
const result = feature.testMatchInjectNameConditional({
515+
injectName: 'apple-isolated',
516+
});
517+
expect(result).toBe(true);
518+
});
519+
520+
it('should not match when injectName condition is not met', () => {
521+
class MyTestFeature extends ContentFeature {
522+
/** @returns {'apple-isolated'} */
523+
get injectName() {
524+
return 'apple-isolated';
525+
}
526+
527+
testMatchInjectNameConditional(conditionBlock) {
528+
return this._matchInjectNameConditional(conditionBlock);
529+
}
530+
}
531+
532+
const args = {
533+
site: {
534+
domain: 'example.com',
535+
url: 'http://example.com',
536+
},
537+
};
538+
539+
const feature = new MyTestFeature('test', {}, args);
540+
const result = feature.testMatchInjectNameConditional({
541+
injectName: 'firefox',
542+
});
543+
expect(result).toBe(false);
544+
});
545+
546+
it('should handle undefined injectName gracefully', () => {
547+
class MyTestFeature extends ContentFeature {
548+
/** @returns {undefined} */
549+
get injectName() {
550+
return undefined;
551+
}
552+
553+
testMatchInjectNameConditional(conditionBlock) {
554+
return this._matchInjectNameConditional(conditionBlock);
555+
}
556+
}
557+
558+
const args = {
559+
site: {
560+
domain: 'example.com',
561+
url: 'http://example.com',
562+
},
563+
};
564+
565+
const feature = new MyTestFeature('test', {}, args);
566+
const result = feature.testMatchInjectNameConditional({
567+
injectName: 'apple-isolated',
568+
});
569+
expect(result).toBe(false);
570+
});
571+
572+
it('should handle missing injectName condition', () => {
573+
class MyTestFeature extends ContentFeature {
574+
/** @returns {'apple-isolated'} */
575+
get injectName() {
576+
return 'apple-isolated';
577+
}
578+
579+
testMatchInjectNameConditional(conditionBlock) {
580+
return this._matchInjectNameConditional(conditionBlock);
581+
}
582+
}
583+
584+
const args = {
585+
site: {
586+
domain: 'example.com',
587+
url: 'http://example.com',
588+
},
589+
};
590+
591+
const feature = new MyTestFeature('test', {}, args);
592+
const result = feature.testMatchInjectNameConditional({});
593+
expect(result).toBe(false);
594+
});
595+
});
492596
});

0 commit comments

Comments
 (0)