diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-10 19:55:14 -0400 | 
|---|---|---|
| committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-09-18 19:14:04 -0400 | 
| commit | dcfe722ba626a439db621385005aaa57b61835ca (patch) | |
| tree | 7da108355eb69ae6b9ef26cbf4e7a8064ea5bb3a /ext/bg/js/backend.js | |
| parent | 8c4fb28a300b84ce876c35c370bd43daf2e65228 (diff) | |
Add support for using optionsContext to select which profile to use
Diffstat (limited to 'ext/bg/js/backend.js')
| -rw-r--r-- | ext/bg/js/backend.js | 38 | 
1 files changed, 36 insertions, 2 deletions
| diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 3839da39..4068b760 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -23,7 +23,8 @@ class Backend {          this.anki = new AnkiNull();          this.options = null;          this.optionsContext = { -            depth: 0 +            depth: 0, +            url: window.location.href          };          this.isPreparedResolve = null; @@ -173,7 +174,40 @@ class Backend {          if (typeof optionsContext.index === 'number') {              return profiles[optionsContext.index];          } -        return this.options.profiles[this.options.profileCurrent]; +        const profile = this.getProfileFromContext(optionsContext); +        return profile !== null ? profile : this.options.profiles[this.options.profileCurrent]; +    } + +    getProfileFromContext(optionsContext) { +        for (const profile of this.options.profiles) { +            const conditionGroups = profile.conditionGroups; +            if (conditionGroups.length > 0 && Backend.testConditionGroups(conditionGroups, optionsContext)) { +                return profile; +            } +        } +        return null; +    } + +    static testConditionGroups(conditionGroups, data) { +        if (conditionGroups.length === 0) { return false; } + +        for (const conditionGroup of conditionGroups) { +            const conditions = conditionGroup.conditions; +            if (conditions.length > 0 && Backend.testConditions(conditions, data)) { +                return true; +            } +        } + +        return false; +    } + +    static testConditions(conditions, data) { +        for (const condition of conditions) { +            if (!conditionsTestValue(profileConditionsDescriptor, condition.type, condition.operator, condition.value, data)) { +                return false; +            } +        } +        return true;      }      setExtensionBadgeBackgroundColor(color) { |