Add support for using optionsContext to select which profile to use
This commit is contained in:
parent
8c4fb28a30
commit
dcfe722ba6
@ -140,7 +140,10 @@ async function apiCommandExec(command) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
toggle: async () => {
|
toggle: async () => {
|
||||||
const optionsContext = {depth: 0};
|
const optionsContext = {
|
||||||
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
|
};
|
||||||
const options = await apiOptionsGet(optionsContext);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
options.general.enable = !options.general.enable;
|
options.general.enable = !options.general.enable;
|
||||||
await apiOptionsSave('popup');
|
await apiOptionsSave('popup');
|
||||||
|
@ -23,7 +23,8 @@ class Backend {
|
|||||||
this.anki = new AnkiNull();
|
this.anki = new AnkiNull();
|
||||||
this.options = null;
|
this.options = null;
|
||||||
this.optionsContext = {
|
this.optionsContext = {
|
||||||
depth: 0
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
};
|
};
|
||||||
|
|
||||||
this.isPreparedResolve = null;
|
this.isPreparedResolve = null;
|
||||||
@ -173,7 +174,40 @@ class Backend {
|
|||||||
if (typeof optionsContext.index === 'number') {
|
if (typeof optionsContext.index === 'number') {
|
||||||
return profiles[optionsContext.index];
|
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) {
|
setExtensionBadgeBackgroundColor(color) {
|
||||||
|
@ -22,7 +22,10 @@ $(document).ready(utilAsync(() => {
|
|||||||
$('#open-options').click(() => apiCommandExec('options'));
|
$('#open-options').click(() => apiCommandExec('options'));
|
||||||
$('#open-help').click(() => apiCommandExec('help'));
|
$('#open-help').click(() => apiCommandExec('help'));
|
||||||
|
|
||||||
const optionsContext = {depth: 0};
|
const optionsContext = {
|
||||||
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
|
};
|
||||||
apiOptionsGet(optionsContext).then(options => {
|
apiOptionsGet(optionsContext).then(options => {
|
||||||
const toggle = $('#enable-search');
|
const toggle = $('#enable-search');
|
||||||
toggle.prop('checked', options.general.enable).change();
|
toggle.prop('checked', options.general.enable).change();
|
||||||
|
@ -32,27 +32,27 @@ const profileConditionsDescriptor = {
|
|||||||
operators: {
|
operators: {
|
||||||
equal: {
|
equal: {
|
||||||
name: '=',
|
name: '=',
|
||||||
test: (value, optionValue) => (value === optionValue)
|
test: ({depth}, optionValue) => (depth === optionValue)
|
||||||
},
|
},
|
||||||
notEqual: {
|
notEqual: {
|
||||||
name: '\u2260',
|
name: '\u2260',
|
||||||
test: (value, optionValue) => (value !== optionValue)
|
test: ({depth}, optionValue) => (depth !== optionValue)
|
||||||
},
|
},
|
||||||
lessThan: {
|
lessThan: {
|
||||||
name: '<',
|
name: '<',
|
||||||
test: (value, optionValue) => (value < optionValue)
|
test: ({depth}, optionValue) => (depth < optionValue)
|
||||||
},
|
},
|
||||||
greaterThan: {
|
greaterThan: {
|
||||||
name: '>',
|
name: '>',
|
||||||
test: (value, optionValue) => (value > optionValue)
|
test: ({depth}, optionValue) => (depth > optionValue)
|
||||||
},
|
},
|
||||||
lessThanOrEqual: {
|
lessThanOrEqual: {
|
||||||
name: '\u2264',
|
name: '\u2264',
|
||||||
test: (value, optionValue) => (value <= optionValue)
|
test: ({depth}, optionValue) => (depth <= optionValue)
|
||||||
},
|
},
|
||||||
greaterThanOrEqual: {
|
greaterThanOrEqual: {
|
||||||
name: '\u2265',
|
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),
|
transform: (optionValue) => optionValue.split(/[,;\s]+/).map(v => v.trim().toLowerCase()).filter(v => v.length > 0),
|
||||||
transformReverse: (transformedOptionValue) => transformedOptionValue.join(', '),
|
transformReverse: (transformedOptionValue) => transformedOptionValue.join(', '),
|
||||||
validateTransformed: (transformedOptionValue) => (transformedOptionValue.length > 0),
|
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: {
|
matchRegExp: {
|
||||||
name: 'Matches RegExp',
|
name: 'Matches RegExp',
|
||||||
@ -78,7 +78,7 @@ const profileConditionsDescriptor = {
|
|||||||
transformCache: {},
|
transformCache: {},
|
||||||
transform: (optionValue) => new RegExp(optionValue, 'i'),
|
transform: (optionValue) => new RegExp(optionValue, 'i'),
|
||||||
transformReverse: (transformedOptionValue) => transformedOptionValue.source,
|
transformReverse: (transformedOptionValue) => transformedOptionValue.source,
|
||||||
test: (value, transformedOptionValue) => (transformedOptionValue !== null && transformedOptionValue.test(value))
|
test: ({url}, transformedOptionValue) => (transformedOptionValue !== null && transformedOptionValue.test(url))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,10 @@
|
|||||||
|
|
||||||
|
|
||||||
async function searchFrontendSetup() {
|
async function searchFrontendSetup() {
|
||||||
const optionsContext = {depth: 0};
|
const optionsContext = {
|
||||||
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
|
};
|
||||||
const options = await apiOptionsGet(optionsContext);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
if (!options.scanning.enableOnSearchPage) { return; }
|
if (!options.scanning.enableOnSearchPage) { return; }
|
||||||
|
|
||||||
|
@ -22,7 +22,8 @@ class DisplaySearch extends Display {
|
|||||||
super($('#spinner'), $('#content'));
|
super($('#spinner'), $('#content'));
|
||||||
|
|
||||||
this.optionsContext = {
|
this.optionsContext = {
|
||||||
depth: 0
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
};
|
};
|
||||||
|
|
||||||
this.search = $('#search').click(this.onSearch.bind(this));
|
this.search = $('#search').click(this.onSearch.bind(this));
|
||||||
|
@ -94,7 +94,10 @@ async function profileFormWrite(optionsFull) {
|
|||||||
$('#profile-condition-groups'),
|
$('#profile-condition-groups'),
|
||||||
$('#profile-add-condition-group')
|
$('#profile-add-condition-group')
|
||||||
);
|
);
|
||||||
profileConditionsContainer.save = () => apiOptionsSave();
|
profileConditionsContainer.save = () => {
|
||||||
|
apiOptionsSave();
|
||||||
|
conditionsClearCaches(profileConditionsDescriptor);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function profileOptionsPopulateSelect(select, profiles, currentValue, ignoreIndices) {
|
function profileOptionsPopulateSelect(select, profiles, currentValue, ignoreIndices) {
|
||||||
|
@ -24,7 +24,8 @@ class DisplayFloat extends Display {
|
|||||||
this.styleNode = null;
|
this.styleNode = null;
|
||||||
|
|
||||||
this.optionsContext = {
|
this.optionsContext = {
|
||||||
depth: 0
|
depth: 0,
|
||||||
|
url: window.location.href
|
||||||
};
|
};
|
||||||
|
|
||||||
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
|
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
|
||||||
@ -78,9 +79,10 @@ class DisplayFloat extends Display {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
popupNestedInitialize: ({id, depth, parentFrameId}) => {
|
popupNestedInitialize: ({id, depth, parentFrameId, url}) => {
|
||||||
this.optionsContext.depth = depth;
|
this.optionsContext.depth = depth;
|
||||||
popupNestedInitialize(id, depth, parentFrameId);
|
this.optionsContext.url = url;
|
||||||
|
popupNestedInitialize(id, depth, parentFrameId, url);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -27,7 +27,8 @@ class Frontend {
|
|||||||
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
|
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
|
||||||
|
|
||||||
this.optionsContext = {
|
this.optionsContext = {
|
||||||
depth: popup.depth
|
depth: popup.depth,
|
||||||
|
url: popup.url
|
||||||
};
|
};
|
||||||
|
|
||||||
this.primaryTouchIdentifier = null;
|
this.primaryTouchIdentifier = null;
|
||||||
@ -42,9 +43,9 @@ class Frontend {
|
|||||||
static create() {
|
static create() {
|
||||||
const initializationData = window.frontendInitializationData;
|
const initializationData = window.frontendInitializationData;
|
||||||
const isNested = (initializationData !== null && typeof initializationData === 'object');
|
const isNested = (initializationData !== null && typeof initializationData === 'object');
|
||||||
const {id, depth, parentFrameId, ignoreNodes} = isNested ? initializationData : {};
|
const {id, depth, parentFrameId, ignoreNodes, url} = isNested ? initializationData : {};
|
||||||
|
|
||||||
const popup = isNested ? new PopupProxy(depth + 1, id, parentFrameId) : PopupProxyHost.instance.createPopup(null);
|
const popup = isNested ? new PopupProxy(depth + 1, id, parentFrameId, url) : PopupProxyHost.instance.createPopup(null);
|
||||||
const frontend = new Frontend(popup, ignoreNodes);
|
const frontend = new Frontend(popup, ignoreNodes);
|
||||||
frontend.prepare();
|
frontend.prepare();
|
||||||
return frontend;
|
return frontend;
|
||||||
@ -52,7 +53,7 @@ class Frontend {
|
|||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
try {
|
try {
|
||||||
this.options = await apiOptionsGet(this.optionsContext);
|
this.options = await apiOptionsGet(this.getOptionsContext());
|
||||||
|
|
||||||
window.addEventListener('message', this.onFrameMessage.bind(this));
|
window.addEventListener('message', this.onFrameMessage.bind(this));
|
||||||
window.addEventListener('mousedown', this.onMouseDown.bind(this));
|
window.addEventListener('mousedown', this.onMouseDown.bind(this));
|
||||||
@ -262,7 +263,7 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateOptions() {
|
async updateOptions() {
|
||||||
this.options = await apiOptionsGet(this.optionsContext);
|
this.options = await apiOptionsGet(this.getOptionsContext());
|
||||||
if (!this.options.enable) {
|
if (!this.options.enable) {
|
||||||
this.searchClear();
|
this.searchClear();
|
||||||
}
|
}
|
||||||
@ -330,7 +331,7 @@ class Frontend {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {definitions, length} = await apiTermsFind(searchText, this.optionsContext);
|
const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext());
|
||||||
if (definitions.length === 0) {
|
if (definitions.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -363,7 +364,7 @@ class Frontend {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const definitions = await apiKanjiFind(searchText, this.optionsContext);
|
const definitions = await apiKanjiFind(searchText, this.getOptionsContext());
|
||||||
if (definitions.length === 0) {
|
if (definitions.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -507,6 +508,11 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOptionsContext() {
|
||||||
|
this.optionsContext.url = this.popup.url;
|
||||||
|
return this.optionsContext;
|
||||||
|
}
|
||||||
|
|
||||||
static isScanningModifierPressed(scanningModifier, mouseEvent) {
|
static isScanningModifierPressed(scanningModifier, mouseEvent) {
|
||||||
switch (scanningModifier) {
|
switch (scanningModifier) {
|
||||||
case 'alt': return mouseEvent.altKey;
|
case 'alt': return mouseEvent.altKey;
|
||||||
|
@ -19,13 +19,13 @@
|
|||||||
|
|
||||||
let popupNestedInitialized = false;
|
let popupNestedInitialized = false;
|
||||||
|
|
||||||
async function popupNestedInitialize(id, depth, parentFrameId) {
|
async function popupNestedInitialize(id, depth, parentFrameId, url) {
|
||||||
if (popupNestedInitialized) {
|
if (popupNestedInitialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
popupNestedInitialized = true;
|
popupNestedInitialized = true;
|
||||||
|
|
||||||
const optionsContext = {depth};
|
const optionsContext = {depth, url};
|
||||||
const options = await apiOptionsGet(optionsContext);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
|
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ async function popupNestedInitialize(id, depth, parentFrameId) {
|
|||||||
|
|
||||||
const ignoreNodes = options.scanning.enableOnPopupExpressions ? [] : [ '.expression', '.expression *' ];
|
const ignoreNodes = options.scanning.enableOnPopupExpressions ? [] : [ '.expression', '.expression *' ];
|
||||||
|
|
||||||
window.frontendInitializationData = {id, depth, parentFrameId, ignoreNodes};
|
window.frontendInitializationData = {id, depth, parentFrameId, ignoreNodes, url};
|
||||||
|
|
||||||
const scriptSrcs = [
|
const scriptSrcs = [
|
||||||
'/fg/js/frontend-api-sender.js',
|
'/fg/js/frontend-api-sender.js',
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
|
|
||||||
class PopupProxy {
|
class PopupProxy {
|
||||||
constructor(depth, parentId, parentFrameId) {
|
constructor(depth, parentId, parentFrameId, url) {
|
||||||
this.parentId = parentId;
|
this.parentId = parentId;
|
||||||
this.parentFrameId = parentFrameId;
|
this.parentFrameId = parentFrameId;
|
||||||
this.id = null;
|
this.id = null;
|
||||||
@ -26,6 +26,7 @@ class PopupProxy {
|
|||||||
this.parent = null;
|
this.parent = null;
|
||||||
this.child = null;
|
this.child = null;
|
||||||
this.depth = depth;
|
this.depth = depth;
|
||||||
|
this.url = url;
|
||||||
|
|
||||||
this.container = null;
|
this.container = null;
|
||||||
|
|
||||||
|
@ -59,7 +59,8 @@ class Popup {
|
|||||||
this.invokeApi('popupNestedInitialize', {
|
this.invokeApi('popupNestedInitialize', {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
depth: this.depth,
|
depth: this.depth,
|
||||||
parentFrameId
|
parentFrameId,
|
||||||
|
url: this.url
|
||||||
});
|
});
|
||||||
this.invokeApi('setOptions', {
|
this.invokeApi('setOptions', {
|
||||||
general: {
|
general: {
|
||||||
@ -311,4 +312,8 @@ class Popup {
|
|||||||
parent.appendChild(this.container);
|
parent.appendChild(this.container);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get url() {
|
||||||
|
return window.location.href;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user