Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document settings methods #1219

Merged
merged 2 commits into from
Nov 14, 2024
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
39 changes: 39 additions & 0 deletions injected/src/content-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ export default class ContentFeature {

/**
* Return a specific setting from the feature settings
* If the "settings" key within the config has a "domains" key, it will be used to override the settings.
* This uses JSONPatch to apply the patches to settings before getting the setting value.
* For example.com getFeatureSettings('val') will return 1:
* ```json
* {
* "settings": {
* "domains": [
* {
* "domain": "example.com",
* "patchSettings": [
* { "op": "replace", "path": "/val", "value": 1 }
* ]
* }
* ]
* }
* }
* ```
* "domain" can either be a string or an array of strings.

* For boolean states you should consider using getFeatureSettingEnabled.
jonathanKingston marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} featureKeyName
* @param {string} [featureName]
* @returns {any}
Expand Down Expand Up @@ -184,6 +204,23 @@ export default class ContentFeature {
/**
* For simple boolean settings, return true if the setting is 'enabled'
* For objects, verify the 'state' field is 'enabled'.
* This allows for future forwards compatibility with more complex settings if required.
* For example:
* ```json
* {
* "toggle": "enabled"
* }
* ```
* Could become later (without breaking changes):
* ```json
* {
* "toggle": {
* "state": "enabled",
* "someOtherKey": 1
* }
* }
* ```
* This also supports domain overrides as per `getFeatureSetting`.
* @param {string} featureKeyName
* @param {string} [featureName]
* @returns {boolean}
Expand All @@ -198,8 +235,10 @@ export default class ContentFeature {

/**
* Given a config key, interpret the value as a list of domain overrides, and return the elements that match the current page
* Consider using patchSettings instead as per `getFeatureSetting`.
Copy link
Member

@muodov muodov Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanKingston
Hm, I'm not sure what this means. Did you mean "consider using getFeatureSetting instead of this method"?

* @param {string} featureKeyName
* @return {any[]}
* @private
*/
matchDomainFeatureSetting(featureKeyName) {
const domain = this.#args?.site.domain;
Expand Down
2 changes: 2 additions & 0 deletions injected/src/features/element-hiding.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,12 @@ export default class ElementHiding extends ContentFeature {

// determine whether strict hide rules should be injected as a style tag
if (shouldInjectStyleTag) {
// @ts-expect-error: Accessing private method
shouldInjectStyleTag = this.matchDomainFeatureSetting('styleTagExceptions').length === 0;
}

// collect all matching rules for domain
// @ts-expect-error: Accessing private method
const activeDomainRules = this.matchDomainFeatureSetting('domains').flatMap((item) => item.rules);

const overrideRules = activeDomainRules.filter((rule) => {
Expand Down
1 change: 1 addition & 0 deletions injected/src/features/navigator-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ContentFeature from '../content-feature';

export default class NavigatorInterface extends ContentFeature {
load(args) {
// @ts-expect-error: Accessing private method
if (this.matchDomainFeatureSetting('privilegedDomains').length) {
this.injectNavigatorInterface(args);
}
Expand Down
Loading