Add a context object for all calls to fetch options
This commit is contained in:
parent
99ca60d4c1
commit
bc8793eb56
@ -17,16 +17,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function apiOptionsGetSync() {
|
function apiOptionsGetSync(optionsContext) {
|
||||||
return utilBackend().options;
|
return utilBackend().options;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiOptionsGet() {
|
async function apiOptionsGet(optionsContext) {
|
||||||
return apiOptionsGetSync();
|
return apiOptionsGetSync(optionsContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiTermsFind(text) {
|
async function apiTermsFind(text, optionsContext) {
|
||||||
const options = apiOptionsGetSync();
|
const options = apiOptionsGetSync(optionsContext);
|
||||||
const translator = utilBackend().translator;
|
const translator = utilBackend().translator;
|
||||||
|
|
||||||
const searcher = {
|
const searcher = {
|
||||||
@ -48,14 +48,14 @@ async function apiTermsFind(text) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiKanjiFind(text) {
|
async function apiKanjiFind(text, optionsContext) {
|
||||||
const options = apiOptionsGetSync();
|
const options = apiOptionsGetSync(optionsContext);
|
||||||
const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options));
|
const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options));
|
||||||
return definitions.slice(0, options.general.maxResults);
|
return definitions.slice(0, options.general.maxResults);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiDefinitionAdd(definition, mode, context) {
|
async function apiDefinitionAdd(definition, mode, context, optionsContext) {
|
||||||
const options = apiOptionsGetSync();
|
const options = apiOptionsGetSync(optionsContext);
|
||||||
|
|
||||||
if (mode !== 'kanji') {
|
if (mode !== 'kanji') {
|
||||||
await audioInject(
|
await audioInject(
|
||||||
@ -77,14 +77,15 @@ async function apiDefinitionAdd(definition, mode, context) {
|
|||||||
return utilBackend().anki.addNote(note);
|
return utilBackend().anki.addNote(note);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function apiDefinitionsAddable(definitions, modes) {
|
async function apiDefinitionsAddable(definitions, modes, optionsContext) {
|
||||||
|
const options = apiOptionsGetSync(optionsContext);
|
||||||
const states = [];
|
const states = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const notes = [];
|
const notes = [];
|
||||||
for (const definition of definitions) {
|
for (const definition of definitions) {
|
||||||
for (const mode of modes) {
|
for (const mode of modes) {
|
||||||
const note = await dictNoteFormat(definition, mode, apiOptionsGetSync());
|
const note = await dictNoteFormat(definition, mode, options);
|
||||||
notes.push(note);
|
notes.push(note);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,7 +133,8 @@ async function apiCommandExec(command) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
toggle: async () => {
|
toggle: async () => {
|
||||||
const options = apiOptionsGetSync();
|
const optionsContext = {depth: 0};
|
||||||
|
const options = apiOptionsGetSync(optionsContext);
|
||||||
options.general.enable = !options.general.enable;
|
options.general.enable = !options.general.enable;
|
||||||
await optionsSave(options);
|
await optionsSave(options);
|
||||||
}
|
}
|
||||||
|
@ -78,24 +78,24 @@ class Backend {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handlers = {
|
const handlers = {
|
||||||
optionsGet: ({callback}) => {
|
optionsGet: ({optionsContext, callback}) => {
|
||||||
forward(apiOptionsGet(), callback);
|
forward(apiOptionsGet(optionsContext), callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
kanjiFind: ({text, callback}) => {
|
kanjiFind: ({text, optionsContext, callback}) => {
|
||||||
forward(apiKanjiFind(text), callback);
|
forward(apiKanjiFind(text, optionsContext), callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
termsFind: ({text, callback}) => {
|
termsFind: ({text, optionsContext, callback}) => {
|
||||||
forward(apiTermsFind(text), callback);
|
forward(apiTermsFind(text, optionsContext), callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
definitionAdd: ({definition, mode, context, callback}) => {
|
definitionAdd: ({definition, mode, context, optionsContext, callback}) => {
|
||||||
forward(apiDefinitionAdd(definition, mode, context), callback);
|
forward(apiDefinitionAdd(definition, mode, context, optionsContext), callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
definitionsAddable: ({definitions, modes, callback}) => {
|
definitionsAddable: ({definitions, modes, optionsContext, callback}) => {
|
||||||
forward(apiDefinitionsAddable(definitions, modes), callback);
|
forward(apiDefinitionsAddable(definitions, modes, optionsContext), callback);
|
||||||
},
|
},
|
||||||
|
|
||||||
noteView: ({noteId}) => {
|
noteView: ({noteId}) => {
|
||||||
|
@ -18,7 +18,8 @@
|
|||||||
|
|
||||||
|
|
||||||
async function searchFrontendSetup() {
|
async function searchFrontendSetup() {
|
||||||
const options = await apiOptionsGet();
|
const optionsContext = {depth: 0};
|
||||||
|
const options = await apiOptionsGet(optionsContext);
|
||||||
if (!options.scanning.enableOnSearchPage) { return; }
|
if (!options.scanning.enableOnSearchPage) { return; }
|
||||||
|
|
||||||
const scriptSrcs = [
|
const scriptSrcs = [
|
||||||
|
@ -21,6 +21,10 @@ class DisplaySearch extends Display {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super($('#spinner'), $('#content'));
|
super($('#spinner'), $('#content'));
|
||||||
|
|
||||||
|
this.optionsContext = {
|
||||||
|
depth: 0
|
||||||
|
};
|
||||||
|
|
||||||
this.search = $('#search').click(this.onSearch.bind(this));
|
this.search = $('#search').click(this.onSearch.bind(this));
|
||||||
this.query = $('#query').on('input', this.onSearchInput.bind(this));
|
this.query = $('#query').on('input', this.onSearchInput.bind(this));
|
||||||
this.intro = $('#intro');
|
this.intro = $('#intro');
|
||||||
@ -46,8 +50,8 @@ class DisplaySearch extends Display {
|
|||||||
try {
|
try {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.intro.slideUp();
|
this.intro.slideUp();
|
||||||
const {length, definitions} = await apiTermsFind(this.query.val());
|
const {length, definitions} = await apiTermsFind(this.query.val(), this.optionsContext);
|
||||||
super.termsShow(definitions, await apiOptionsGet());
|
super.termsShow(definitions, await apiOptionsGet(this.optionsContext));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.onError(e);
|
this.onError(e);
|
||||||
}
|
}
|
||||||
|
@ -17,24 +17,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function apiOptionsGet() {
|
function apiOptionsGet(optionsContext) {
|
||||||
return utilInvoke('optionsGet');
|
return utilInvoke('optionsGet', {optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiTermsFind(text) {
|
function apiTermsFind(text, optionsContext) {
|
||||||
return utilInvoke('termsFind', {text});
|
return utilInvoke('termsFind', {text, optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiKanjiFind(text) {
|
function apiKanjiFind(text, optionsContext) {
|
||||||
return utilInvoke('kanjiFind', {text});
|
return utilInvoke('kanjiFind', {text, optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiDefinitionAdd(definition, mode, context) {
|
function apiDefinitionAdd(definition, mode, context, optionsContext) {
|
||||||
return utilInvoke('definitionAdd', {definition, mode, context});
|
return utilInvoke('definitionAdd', {definition, mode, context, optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiDefinitionsAddable(definitions, modes) {
|
function apiDefinitionsAddable(definitions, modes, optionsContext) {
|
||||||
return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null);
|
return utilInvoke('definitionsAddable', {definitions, modes, optionsContext}).catch(() => null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function apiNoteView(noteId) {
|
function apiNoteView(noteId) {
|
||||||
|
@ -23,6 +23,10 @@ class DisplayFloat extends Display {
|
|||||||
this.autoPlayAudioTimer = null;
|
this.autoPlayAudioTimer = null;
|
||||||
this.styleNode = null;
|
this.styleNode = null;
|
||||||
|
|
||||||
|
this.optionsContext = {
|
||||||
|
depth: 0
|
||||||
|
};
|
||||||
|
|
||||||
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
|
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
|
||||||
|
|
||||||
$(window).on('message', utilAsync(this.onMessage.bind(this)));
|
$(window).on('message', utilAsync(this.onMessage.bind(this)));
|
||||||
@ -75,6 +79,7 @@ class DisplayFloat extends Display {
|
|||||||
},
|
},
|
||||||
|
|
||||||
popupNestedInitialize: ({id, depth, parentFrameId}) => {
|
popupNestedInitialize: ({id, depth, parentFrameId}) => {
|
||||||
|
this.optionsContext.depth = depth;
|
||||||
popupNestedInitialize(id, depth, parentFrameId);
|
popupNestedInitialize(id, depth, parentFrameId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -28,6 +28,10 @@ class Frontend {
|
|||||||
this.options = null;
|
this.options = null;
|
||||||
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
|
this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null);
|
||||||
|
|
||||||
|
this.optionsContext = {
|
||||||
|
depth: popup.depth
|
||||||
|
};
|
||||||
|
|
||||||
this.primaryTouchIdentifier = null;
|
this.primaryTouchIdentifier = null;
|
||||||
this.contextMenuChecking = false;
|
this.contextMenuChecking = false;
|
||||||
this.contextMenuPrevent = false;
|
this.contextMenuPrevent = false;
|
||||||
@ -50,7 +54,7 @@ class Frontend {
|
|||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
try {
|
try {
|
||||||
this.options = await apiOptionsGet();
|
this.options = await apiOptionsGet(this.optionsContext);
|
||||||
|
|
||||||
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));
|
||||||
@ -282,7 +286,7 @@ class Frontend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async updateOptions() {
|
async updateOptions() {
|
||||||
this.options = await apiOptionsGet();
|
this.options = await apiOptionsGet(this.optionsContext);
|
||||||
if (!this.options.enable) {
|
if (!this.options.enable) {
|
||||||
this.searchClear();
|
this.searchClear();
|
||||||
}
|
}
|
||||||
@ -351,7 +355,7 @@ class Frontend {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {definitions, length} = await apiTermsFind(searchText);
|
const {definitions, length} = await apiTermsFind(searchText, this.optionsContext);
|
||||||
if (definitions.length === 0) {
|
if (definitions.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -384,7 +388,7 @@ class Frontend {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const definitions = await apiKanjiFind(searchText);
|
const definitions = await apiKanjiFind(searchText, this.optionsContext);
|
||||||
if (definitions.length === 0) {
|
if (definitions.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,8 @@ async function popupNestedInitialize(id, depth, parentFrameId) {
|
|||||||
}
|
}
|
||||||
popupNestedInitialized = true;
|
popupNestedInitialized = true;
|
||||||
|
|
||||||
const options = await apiOptionsGet();
|
const optionsContext = {depth};
|
||||||
|
const options = await apiOptionsGet(optionsContext);
|
||||||
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
|
const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth;
|
||||||
|
|
||||||
if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) {
|
if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) {
|
||||||
|
@ -27,6 +27,7 @@ class Display {
|
|||||||
this.sequence = 0;
|
this.sequence = 0;
|
||||||
this.index = 0;
|
this.index = 0;
|
||||||
this.audioCache = {};
|
this.audioCache = {};
|
||||||
|
this.optionsContext = {};
|
||||||
|
|
||||||
this.dependencies = {};
|
this.dependencies = {};
|
||||||
|
|
||||||
@ -66,7 +67,7 @@ class Display {
|
|||||||
context.source.source = this.context.source;
|
context.source.source = this.context.source;
|
||||||
}
|
}
|
||||||
|
|
||||||
const kanjiDefs = await apiKanjiFind(link.text());
|
const kanjiDefs = await apiKanjiFind(link.text(), this.optionsContext);
|
||||||
this.kanjiShow(kanjiDefs, this.options, context);
|
this.kanjiShow(kanjiDefs, this.options, context);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.onError(e);
|
this.onError(e);
|
||||||
@ -89,7 +90,7 @@ class Display {
|
|||||||
try {
|
try {
|
||||||
textSource.setEndOffset(this.options.scanning.length);
|
textSource.setEndOffset(this.options.scanning.length);
|
||||||
|
|
||||||
({definitions, length} = await apiTermsFind(textSource.text()));
|
({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext));
|
||||||
if (definitions.length === 0) {
|
if (definitions.length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -379,7 +380,7 @@ class Display {
|
|||||||
|
|
||||||
async adderButtonUpdate(modes, sequence) {
|
async adderButtonUpdate(modes, sequence) {
|
||||||
try {
|
try {
|
||||||
const states = await apiDefinitionsAddable(this.definitions, modes);
|
const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext);
|
||||||
if (!states || sequence !== this.sequence) {
|
if (!states || sequence !== this.sequence) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -453,7 +454,7 @@ class Display {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const noteId = await apiDefinitionAdd(definition, mode, context);
|
const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext);
|
||||||
if (noteId) {
|
if (noteId) {
|
||||||
const index = this.definitions.indexOf(definition);
|
const index = this.definitions.indexOf(definition);
|
||||||
Display.adderButtonFind(index, mode).addClass('disabled');
|
Display.adderButtonFind(index, mode).addClass('disabled');
|
||||||
|
Loading…
Reference in New Issue
Block a user