summaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-10 19:55:14 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-09-18 19:14:04 -0400
commitdcfe722ba626a439db621385005aaa57b61835ca (patch)
tree7da108355eb69ae6b9ef26cbf4e7a8064ea5bb3a /ext/bg
parent8c4fb28a300b84ce876c35c370bd43daf2e65228 (diff)
Add support for using optionsContext to select which profile to use
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/api.js5
-rw-r--r--ext/bg/js/backend.js38
-rw-r--r--ext/bg/js/context.js5
-rw-r--r--ext/bg/js/profile-conditions.js16
-rw-r--r--ext/bg/js/search-frontend.js5
-rw-r--r--ext/bg/js/search.js3
-rw-r--r--ext/bg/js/settings-profiles.js5
7 files changed, 62 insertions, 15 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index f32b984f..474fe604 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -140,7 +140,10 @@ async function apiCommandExec(command) {
},
toggle: async () => {
- const optionsContext = {depth: 0};
+ const optionsContext = {
+ depth: 0,
+ url: window.location.href
+ };
const options = await apiOptionsGet(optionsContext);
options.general.enable = !options.general.enable;
await apiOptionsSave('popup');
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) {
diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js
index dfa224a7..0f88e9c0 100644
--- a/ext/bg/js/context.js
+++ b/ext/bg/js/context.js
@@ -22,7 +22,10 @@ $(document).ready(utilAsync(() => {
$('#open-options').click(() => apiCommandExec('options'));
$('#open-help').click(() => apiCommandExec('help'));
- const optionsContext = {depth: 0};
+ const optionsContext = {
+ depth: 0,
+ url: window.location.href
+ };
apiOptionsGet(optionsContext).then(options => {
const toggle = $('#enable-search');
toggle.prop('checked', options.general.enable).change();
diff --git a/ext/bg/js/profile-conditions.js b/ext/bg/js/profile-conditions.js
index 86bafa95..5daa904e 100644
--- a/ext/bg/js/profile-conditions.js
+++ b/ext/bg/js/profile-conditions.js
@@ -32,27 +32,27 @@ const profileConditionsDescriptor = {
operators: {
equal: {
name: '=',
- test: (value, optionValue) => (value === optionValue)
+ test: ({depth}, optionValue) => (depth === optionValue)
},
notEqual: {
name: '\u2260',
- test: (value, optionValue) => (value !== optionValue)
+ test: ({depth}, optionValue) => (depth !== optionValue)
},
lessThan: {
name: '<',
- test: (value, optionValue) => (value < optionValue)
+ test: ({depth}, optionValue) => (depth < optionValue)
},
greaterThan: {
name: '>',
- test: (value, optionValue) => (value > optionValue)
+ test: ({depth}, optionValue) => (depth > optionValue)
},
lessThanOrEqual: {
name: '\u2264',
- test: (value, optionValue) => (value <= optionValue)
+ test: ({depth}, optionValue) => (depth <= optionValue)
},
greaterThanOrEqual: {
name: '\u2265',
- test: (value, optionValue) => (value >= optionValue)
+ test: ({depth}, optionValue) => (depth >= optionValue)
}
}
},
@@ -69,7 +69,7 @@ const profileConditionsDescriptor = {
transform: (optionValue) => optionValue.split(/[,;\s]+/).map(v => v.trim().toLowerCase()).filter(v => v.length > 0),
transformReverse: (transformedOptionValue) => transformedOptionValue.join(', '),
validateTransformed: (transformedOptionValue) => (transformedOptionValue.length > 0),
- test: (value, transformedOptionValue) => (transformedOptionValue.indexOf(new URL(value).hostname.toLowerCase()) >= 0)
+ test: ({url}, transformedOptionValue) => (transformedOptionValue.indexOf(new URL(url).hostname.toLowerCase()) >= 0)
},
matchRegExp: {
name: 'Matches RegExp',
@@ -78,7 +78,7 @@ const profileConditionsDescriptor = {
transformCache: {},
transform: (optionValue) => new RegExp(optionValue, 'i'),
transformReverse: (transformedOptionValue) => transformedOptionValue.source,
- test: (value, transformedOptionValue) => (transformedOptionValue !== null && transformedOptionValue.test(value))
+ test: ({url}, transformedOptionValue) => (transformedOptionValue !== null && transformedOptionValue.test(url))
}
}
}
diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js
index df5ccf81..faec29ef 100644
--- a/ext/bg/js/search-frontend.js
+++ b/ext/bg/js/search-frontend.js
@@ -18,7 +18,10 @@
async function searchFrontendSetup() {
- const optionsContext = {depth: 0};
+ const optionsContext = {
+ depth: 0,
+ url: window.location.href
+ };
const options = await apiOptionsGet(optionsContext);
if (!options.scanning.enableOnSearchPage) { return; }
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index 6bdc47d8..6ff710f0 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -22,7 +22,8 @@ class DisplaySearch extends Display {
super($('#spinner'), $('#content'));
this.optionsContext = {
- depth: 0
+ depth: 0,
+ url: window.location.href
};
this.search = $('#search').click(this.onSearch.bind(this));
diff --git a/ext/bg/js/settings-profiles.js b/ext/bg/js/settings-profiles.js
index 70f77d7b..8796770d 100644
--- a/ext/bg/js/settings-profiles.js
+++ b/ext/bg/js/settings-profiles.js
@@ -94,7 +94,10 @@ async function profileFormWrite(optionsFull) {
$('#profile-condition-groups'),
$('#profile-add-condition-group')
);
- profileConditionsContainer.save = () => apiOptionsSave();
+ profileConditionsContainer.save = () => {
+ apiOptionsSave();
+ conditionsClearCaches(profileConditionsDescriptor);
+ };
}
function profileOptionsPopulateSelect(select, profiles, currentValue, ignoreIndices) {