Skip to content

Commit

Permalink
Document settings methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Nov 13, 2024
1 parent 7f1e8d2 commit 3632c58
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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.conm 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.
* @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`.
* @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

0 comments on commit 3632c58

Please sign in to comment.