summaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-09-23 17:03:00 -0700
committerGitHub <noreply@github.com>2019-09-23 17:03:00 -0700
commitba2858309ee5646ee25e85caae681937647e419b (patch)
tree60936bf08b4c908eb6a6f287bec4ddb53f7ba2a6 /ext/bg/js/backend.js
parentdb405ffd240b8266e6b1585415ed1e90b629bc3e (diff)
parent352d214203fba929594c6139189fa59c6772c69f (diff)
Merge pull request #218 from toasted-nutbread/settings-profile-conditions
Settings profile conditions
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js38
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) {