From 13b184707b1bb0c5150645d6cdd186accb345f60 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 6 Sep 2019 21:06:45 -0400 Subject: Remove unnecessary functions apiOptionsSet not required in bg/js/api.js after optionsSave; optionsSave already invokes apiOptionsSet. apiOptionsSet not required in fg/js/api.js since it's never invoked by the foreground. optionsSet handler not required in bg/js/backend.js since the message is never sent by the foreground. --- ext/bg/js/backend.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 39fd4288..c1cef0c5 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -85,10 +85,6 @@ class Backend { forward(apiOptionsGet(), callback); }, - optionsSet: ({options, callback}) => { - forward(apiOptionsSet(options), callback); - }, - kanjiFind: ({text, callback}) => { forward(apiKanjiFind(text), callback); }, -- cgit v1.2.3 From 7db2c661054113966644c9055e5b60e29bbeb068 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 6 Sep 2019 21:07:29 -0400 Subject: Use consistent structure for params --- ext/bg/js/backend.js | 2 +- ext/fg/js/frontend.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index c1cef0c5..f05ae9e6 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -62,7 +62,7 @@ class Backend { const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: options}, callback); + chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: {options}}, callback); } }); } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index b70bf036..6806e2c3 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -261,7 +261,7 @@ class Frontend { onBgMessage({action, params}, sender, callback) { const handlers = { - optionsSet: options => { + optionsSet: ({options}) => { this.options = options; if (!this.options.enable) { this.searchClear(); -- cgit v1.2.3 From eb98dfb1a86d42a0ecfe54d8eb978c47aa1c0f8b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 6 Sep 2019 21:23:00 -0400 Subject: Simplify logic for how option updates are propagated --- ext/bg/js/api.js | 4 ---- ext/bg/js/backend.js | 13 +++++-------- ext/bg/js/options.js | 2 +- 3 files changed, 6 insertions(+), 13 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index ff54ae81..9839aef5 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,10 +17,6 @@ */ -async function apiOptionsSet(options) { - utilBackend().onOptionsUpdated(options); -} - function apiOptionsGetSync() { return utilBackend().options; } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f05ae9e6..b3e737da 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -28,7 +28,7 @@ class Backend { async prepare() { await this.translator.prepare(); - await apiOptionsSet(await optionsLoad()); + this.onOptionsUpdated(await optionsLoad()); if (chrome.commands !== null && typeof chrome.commands === 'object') { chrome.commands.onCommand.addListener(this.onCommand.bind(this)); @@ -41,7 +41,8 @@ class Backend { } onOptionsUpdated(options) { - this.options = utilIsolate(options); + options = utilIsolate(options); + this.options = options; if (!options.general.enable) { this.setExtensionBadgeBackgroundColor('#555555'); @@ -53,11 +54,7 @@ class Backend { this.setExtensionBadgeText(''); } - if (options.anki.enable) { - this.anki = new AnkiConnect(options.anki.server); - } else { - this.anki = new AnkiNull(); - } + this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { @@ -144,7 +141,7 @@ class Backend { chrome.browserAction.setBadgeBackgroundColor({color}); } } - + setExtensionBadgeText(text) { if (typeof chrome.browserAction.setBadgeText === 'function') { chrome.browserAction.setBadgeText({text}); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 0e871567..69c662e6 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -346,6 +346,6 @@ function optionsSave(options) { return new Promise((resolve) => { chrome.storage.local.set({options: JSON.stringify(options)}, resolve); }).then(() => { - apiOptionsSet(options); + utilBackend().onOptionsUpdated(options); }); } -- cgit v1.2.3 From 91bc31d7582fb54908433cd8b6e46b5a0be4e9b3 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 11:21:06 -0400 Subject: Change how options updates are handled on the frontend Only an 'optionsUpdate' signal is now sent to the frontend with empty data. The frontend then responds by performing apiOptionsGet to update the options. This makes it so that there is only a single function which is responsible for requesting options from the backend. --- ext/bg/js/backend.js | 2 +- ext/fg/js/frontend.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index b3e737da..0394c4ec 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -59,7 +59,7 @@ class Backend { const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsSet', params: {options}}, callback); + chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {}}, callback); } }); } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 52620933..83e0cef1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -261,11 +261,8 @@ class Frontend { onBgMessage({action, params}, sender, callback) { const handlers = { - optionsSet: ({options}) => { - this.options = options; - if (!this.options.enable) { - this.searchClear(); - } + optionsUpdate: () => { + this.updateOptions(); }, popupSetVisible: ({visible}) => { @@ -284,6 +281,13 @@ class Frontend { console.log(error); } + async updateOptions() { + this.options = await apiOptionsGet(); + if (!this.options.enable) { + this.searchClear(); + } + } + popupTimerSet(callback) { this.popupTimerClear(); this.popupTimer = window.setTimeout(callback, this.options.scanning.delay); -- cgit v1.2.3 From bc8793eb56b2ce985f2e5dc0a9fd270f98fbf17a Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 13:58:19 -0400 Subject: Add a context object for all calls to fetch options --- ext/bg/js/api.js | 26 ++++++++++++++------------ ext/bg/js/backend.js | 20 ++++++++++---------- ext/bg/js/search-frontend.js | 3 ++- ext/bg/js/search.js | 8 ++++++-- ext/fg/js/api.js | 20 ++++++++++---------- ext/fg/js/float.js | 5 +++++ ext/fg/js/frontend.js | 12 ++++++++---- ext/fg/js/popup-nested.js | 3 ++- ext/mixed/js/display.js | 9 +++++---- 9 files changed, 62 insertions(+), 44 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index b56b3449..0b80f099 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,16 +17,16 @@ */ -function apiOptionsGetSync() { +function apiOptionsGetSync(optionsContext) { return utilBackend().options; } -async function apiOptionsGet() { - return apiOptionsGetSync(); +async function apiOptionsGet(optionsContext) { + return apiOptionsGetSync(optionsContext); } -async function apiTermsFind(text) { - const options = apiOptionsGetSync(); +async function apiTermsFind(text, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const translator = utilBackend().translator; const searcher = { @@ -48,14 +48,14 @@ async function apiTermsFind(text) { }; } -async function apiKanjiFind(text) { - const options = apiOptionsGetSync(); +async function apiKanjiFind(text, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } -async function apiDefinitionAdd(definition, mode, context) { - const options = apiOptionsGetSync(); +async function apiDefinitionAdd(definition, mode, context, optionsContext) { + const options = apiOptionsGetSync(optionsContext); if (mode !== 'kanji') { await audioInject( @@ -77,14 +77,15 @@ async function apiDefinitionAdd(definition, mode, context) { return utilBackend().anki.addNote(note); } -async function apiDefinitionsAddable(definitions, modes) { +async function apiDefinitionsAddable(definitions, modes, optionsContext) { + const options = apiOptionsGetSync(optionsContext); const states = []; try { const notes = []; for (const definition of definitions) { for (const mode of modes) { - const note = await dictNoteFormat(definition, mode, apiOptionsGetSync()); + const note = await dictNoteFormat(definition, mode, options); notes.push(note); } } @@ -132,7 +133,8 @@ async function apiCommandExec(command) { }, toggle: async () => { - const options = apiOptionsGetSync(); + const optionsContext = {depth: 0}; + const options = apiOptionsGetSync(optionsContext); options.general.enable = !options.general.enable; await optionsSave(options); } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 0394c4ec..6afa9617 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -78,24 +78,24 @@ class Backend { }; const handlers = { - optionsGet: ({callback}) => { - forward(apiOptionsGet(), callback); + optionsGet: ({optionsContext, callback}) => { + forward(apiOptionsGet(optionsContext), callback); }, - kanjiFind: ({text, callback}) => { - forward(apiKanjiFind(text), callback); + kanjiFind: ({text, optionsContext, callback}) => { + forward(apiKanjiFind(text, optionsContext), callback); }, - termsFind: ({text, callback}) => { - forward(apiTermsFind(text), callback); + termsFind: ({text, optionsContext, callback}) => { + forward(apiTermsFind(text, optionsContext), callback); }, - definitionAdd: ({definition, mode, context, callback}) => { - forward(apiDefinitionAdd(definition, mode, context), callback); + definitionAdd: ({definition, mode, context, optionsContext, callback}) => { + forward(apiDefinitionAdd(definition, mode, context, optionsContext), callback); }, - definitionsAddable: ({definitions, modes, callback}) => { - forward(apiDefinitionsAddable(definitions, modes), callback); + definitionsAddable: ({definitions, modes, optionsContext, callback}) => { + forward(apiDefinitionsAddable(definitions, modes, optionsContext), callback); }, noteView: ({noteId}) => { diff --git a/ext/bg/js/search-frontend.js b/ext/bg/js/search-frontend.js index 840a1ea8..df5ccf81 100644 --- a/ext/bg/js/search-frontend.js +++ b/ext/bg/js/search-frontend.js @@ -18,7 +18,8 @@ async function searchFrontendSetup() { - const options = await apiOptionsGet(); + const optionsContext = {depth: 0}; + const options = await apiOptionsGet(optionsContext); if (!options.scanning.enableOnSearchPage) { return; } const scriptSrcs = [ diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index a3382398..6bdc47d8 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -21,6 +21,10 @@ class DisplaySearch extends Display { constructor() { super($('#spinner'), $('#content')); + this.optionsContext = { + depth: 0 + }; + this.search = $('#search').click(this.onSearch.bind(this)); this.query = $('#query').on('input', this.onSearchInput.bind(this)); this.intro = $('#intro'); @@ -46,8 +50,8 @@ class DisplaySearch extends Display { try { e.preventDefault(); this.intro.slideUp(); - const {length, definitions} = await apiTermsFind(this.query.val()); - super.termsShow(definitions, await apiOptionsGet()); + const {length, definitions} = await apiTermsFind(this.query.val(), this.optionsContext); + super.termsShow(definitions, await apiOptionsGet(this.optionsContext)); } catch (e) { this.onError(e); } diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index aa3b2629..d0ac649a 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -17,24 +17,24 @@ */ -function apiOptionsGet() { - return utilInvoke('optionsGet'); +function apiOptionsGet(optionsContext) { + return utilInvoke('optionsGet', {optionsContext}); } -function apiTermsFind(text) { - return utilInvoke('termsFind', {text}); +function apiTermsFind(text, optionsContext) { + return utilInvoke('termsFind', {text, optionsContext}); } -function apiKanjiFind(text) { - return utilInvoke('kanjiFind', {text}); +function apiKanjiFind(text, optionsContext) { + return utilInvoke('kanjiFind', {text, optionsContext}); } -function apiDefinitionAdd(definition, mode, context) { - return utilInvoke('definitionAdd', {definition, mode, context}); +function apiDefinitionAdd(definition, mode, context, optionsContext) { + return utilInvoke('definitionAdd', {definition, mode, context, optionsContext}); } -function apiDefinitionsAddable(definitions, modes) { - return utilInvoke('definitionsAddable', {definitions, modes}).catch(() => null); +function apiDefinitionsAddable(definitions, modes, optionsContext) { + return utilInvoke('definitionsAddable', {definitions, modes, optionsContext}).catch(() => null); } function apiNoteView(noteId) { diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 3c521714..348c114e 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -23,6 +23,10 @@ class DisplayFloat extends Display { this.autoPlayAudioTimer = null; this.styleNode = null; + this.optionsContext = { + depth: 0 + }; + this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); $(window).on('message', utilAsync(this.onMessage.bind(this))); @@ -75,6 +79,7 @@ class DisplayFloat extends Display { }, popupNestedInitialize: ({id, depth, parentFrameId}) => { + this.optionsContext.depth = depth; popupNestedInitialize(id, depth, parentFrameId); } }; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 5e12d101..0b60aa2b 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -28,6 +28,10 @@ class Frontend { this.options = null; this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); + this.optionsContext = { + depth: popup.depth + }; + this.primaryTouchIdentifier = null; this.contextMenuChecking = false; this.contextMenuPrevent = false; @@ -50,7 +54,7 @@ class Frontend { async prepare() { try { - this.options = await apiOptionsGet(); + this.options = await apiOptionsGet(this.optionsContext); window.addEventListener('message', this.onFrameMessage.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this)); @@ -282,7 +286,7 @@ class Frontend { } async updateOptions() { - this.options = await apiOptionsGet(); + this.options = await apiOptionsGet(this.optionsContext); if (!this.options.enable) { this.searchClear(); } @@ -351,7 +355,7 @@ class Frontend { return; } - const {definitions, length} = await apiTermsFind(searchText); + const {definitions, length} = await apiTermsFind(searchText, this.optionsContext); if (definitions.length === 0) { return false; } @@ -384,7 +388,7 @@ class Frontend { return; } - const definitions = await apiKanjiFind(searchText); + const definitions = await apiKanjiFind(searchText, this.optionsContext); if (definitions.length === 0) { return false; } diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index e0376bb2..de2acccc 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -25,7 +25,8 @@ async function popupNestedInitialize(id, depth, parentFrameId) { } popupNestedInitialized = true; - const options = await apiOptionsGet(); + const optionsContext = {depth}; + const options = await apiOptionsGet(optionsContext); const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; if (!(typeof popupNestingMaxDepth === 'number' && typeof depth === 'number' && depth < popupNestingMaxDepth)) { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index ebf56897..eca67b5e 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -27,6 +27,7 @@ class Display { this.sequence = 0; this.index = 0; this.audioCache = {}; + this.optionsContext = {}; this.dependencies = {}; @@ -66,7 +67,7 @@ class Display { 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); } catch (e) { this.onError(e); @@ -89,7 +90,7 @@ class Display { try { textSource.setEndOffset(this.options.scanning.length); - ({definitions, length} = await apiTermsFind(textSource.text())); + ({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext)); if (definitions.length === 0) { return false; } @@ -379,7 +380,7 @@ class Display { async adderButtonUpdate(modes, sequence) { try { - const states = await apiDefinitionsAddable(this.definitions, modes); + const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext); if (!states || sequence !== this.sequence) { 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) { const index = this.definitions.indexOf(definition); Display.adderButtonFind(index, mode).addClass('disabled'); -- cgit v1.2.3 From 1b2a1e50ebcd62cf54b397516e991333afa5158c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 14:21:26 -0400 Subject: Add getOptions function to backend --- ext/bg/js/api.js | 2 +- ext/bg/js/backend.js | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 16 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 0b80f099..45dc36e7 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -18,7 +18,7 @@ function apiOptionsGetSync(optionsContext) { - return utilBackend().options; + return utilBackend().getOptions(optionsContext); } async function apiOptionsGet(optionsContext) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6afa9617..59de5a50 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,6 +22,9 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; + this.optionsContext = { + depth: 0 + }; this.apiForwarder = new BackendApiForwarder(); } @@ -35,26 +38,15 @@ class Backend { } chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); - if (this.options.general.showGuide) { + const options = this.getOptions(this.optionsContext); + if (options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } } onOptionsUpdated(options) { - options = utilIsolate(options); - this.options = options; - - if (!options.general.enable) { - this.setExtensionBadgeBackgroundColor('#555555'); - this.setExtensionBadgeText('off'); - } else if (!dictConfigured(options)) { - this.setExtensionBadgeBackgroundColor('#f0ad4e'); - this.setExtensionBadgeText('!'); - } else { - this.setExtensionBadgeText(''); - } - - this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + this.options = utilIsolate(options); + this.applyOptions(); const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { @@ -136,6 +128,25 @@ class Backend { return true; } + applyOptions() { + const options = this.getOptions(this.optionsContext); + if (!options.general.enable) { + this.setExtensionBadgeBackgroundColor('#555555'); + this.setExtensionBadgeText('off'); + } else if (!dictConfigured(options)) { + this.setExtensionBadgeBackgroundColor('#f0ad4e'); + this.setExtensionBadgeText('!'); + } else { + this.setExtensionBadgeText(''); + } + + this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + } + + getOptions(optionsContext) { + return this.options; + } + setExtensionBadgeBackgroundColor(color) { if (typeof chrome.browserAction.setBadgeBackgroundColor === 'function') { chrome.browserAction.setBadgeBackgroundColor({color}); -- cgit v1.2.3 From 8175f80183caa0673a946b2405feae0c9535af48 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 15:06:15 -0400 Subject: Remove calls to apiOptionsGetSync Use apiOptionsGet everywhere to ensure options is initialized. --- ext/bg/js/api.js | 16 ++++++---------- ext/bg/js/backend.js | 20 +++++++++++++++++--- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 45dc36e7..53e25348 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,16 +17,12 @@ */ -function apiOptionsGetSync(optionsContext) { +function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } -async function apiOptionsGet(optionsContext) { - return apiOptionsGetSync(optionsContext); -} - async function apiTermsFind(text, optionsContext) { - const options = apiOptionsGetSync(optionsContext); + const options = await apiOptionsGet(optionsContext); const translator = utilBackend().translator; const searcher = { @@ -49,13 +45,13 @@ async function apiTermsFind(text, optionsContext) { } async function apiKanjiFind(text, optionsContext) { - const options = apiOptionsGetSync(optionsContext); + const options = await apiOptionsGet(optionsContext); const definitions = await utilBackend().translator.findKanji(text, dictEnabledSet(options)); return definitions.slice(0, options.general.maxResults); } async function apiDefinitionAdd(definition, mode, context, optionsContext) { - const options = apiOptionsGetSync(optionsContext); + const options = await apiOptionsGet(optionsContext); if (mode !== 'kanji') { await audioInject( @@ -78,7 +74,7 @@ async function apiDefinitionAdd(definition, mode, context, optionsContext) { } async function apiDefinitionsAddable(definitions, modes, optionsContext) { - const options = apiOptionsGetSync(optionsContext); + const options = await apiOptionsGet(optionsContext); const states = []; try { @@ -134,7 +130,7 @@ async function apiCommandExec(command) { toggle: async () => { const optionsContext = {depth: 0}; - const options = apiOptionsGetSync(optionsContext); + const options = await apiOptionsGet(optionsContext); options.general.enable = !options.general.enable; await optionsSave(options); } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 59de5a50..6dcf8e4d 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -26,6 +26,9 @@ class Backend { depth: 0 }; + this.isPreparedResolve = null; + this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); + this.apiForwarder = new BackendApiForwarder(); } @@ -38,10 +41,14 @@ class Backend { } chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); - const options = this.getOptions(this.optionsContext); + const options = this.getOptionsSync(this.optionsContext); if (options.general.showGuide) { chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); } + + this.isPreparedResolve(); + this.isPreparedResolve = null; + this.isPreparedPromise = null; } onOptionsUpdated(options) { @@ -129,7 +136,7 @@ class Backend { } applyOptions() { - const options = this.getOptions(this.optionsContext); + const options = this.getOptionsSync(this.optionsContext); if (!options.general.enable) { this.setExtensionBadgeBackgroundColor('#555555'); this.setExtensionBadgeText('off'); @@ -143,7 +150,14 @@ class Backend { this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); } - getOptions(optionsContext) { + async getOptions(optionsContext) { + if (this.isPreparedPromise !== null) { + await this.isPreparedPromise; + } + return this.getOptionsSync(optionsContext); + } + + getOptionsSync(optionsContext) { return this.options; } -- cgit v1.2.3 From 05ce350792fd60c1721bff4d0fb971e2bec24818 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 16:15:18 -0400 Subject: Use apiOptionsSave instead of optionsSave --- ext/bg/js/api.js | 9 ++++++++- ext/bg/js/backend.js | 7 +++++++ ext/bg/js/options.js | 13 +++++++++---- ext/bg/js/settings.js | 10 +++++----- 4 files changed, 29 insertions(+), 10 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 53e25348..a50353c1 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,6 +21,13 @@ function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } +async function apiOptionsSave() { + const backend = utilBackend(); + const options = await backend.getFullOptions(); + await optionsSave(options); + backend.onOptionsUpdated(options); +} + async function apiTermsFind(text, optionsContext) { const options = await apiOptionsGet(optionsContext); const translator = utilBackend().translator; @@ -132,7 +139,7 @@ async function apiCommandExec(command) { const optionsContext = {depth: 0}; const options = await apiOptionsGet(optionsContext); options.general.enable = !options.general.enable; - await optionsSave(options); + await apiOptionsSave(); } }; diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6dcf8e4d..1f00f788 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -150,6 +150,13 @@ class Backend { this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); } + async getFullOptions() { + if (this.isPreparedPromise !== null) { + await this.isPreparedPromise; + } + return this.options; + } + async getOptions(optionsContext) { if (this.isPreparedPromise !== null) { await this.isPreparedPromise; diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 69c662e6..ea8f56d5 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -343,9 +343,14 @@ function optionsLoad() { } function optionsSave(options) { - return new Promise((resolve) => { - chrome.storage.local.set({options: JSON.stringify(options)}, resolve); - }).then(() => { - utilBackend().onOptionsUpdated(options); + return new Promise((resolve, reject) => { + chrome.storage.local.set({options: JSON.stringify(options)}, () => { + const error = chrome.runtime.lastError; + if (error) { + reject(error); + } else { + resolve(); + } + }); }); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 1c9198dd..e5786804 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -148,7 +148,7 @@ async function onFormOptionsChanged(e) { const optionsAnkiServerOld = options.anki.server; await formRead(options); - await optionsSave(options); + await apiOptionsSave(); formUpdateVisibility(options); try { @@ -385,7 +385,7 @@ async function onDictionaryPurge(e) { const options = await apiOptionsGet(optionsContext); options.dictionaries = {}; options.general.mainDictionary = ''; - await optionsSave(options); + await apiOptionsSave(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -435,7 +435,7 @@ async function onDictionaryImport(e) { dictionaryErrorsShow(exceptions); } - await optionsSave(options); + await apiOptionsSave(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -579,7 +579,7 @@ async function onAnkiModelChanged(e) { const options = await apiOptionsGet(optionsContext); await formRead(options); options.anki[tabId].fields = {}; - await optionsSave(options); + await apiOptionsSave(); ankiSpinnerShow(true); await ankiFieldsPopulate(element, options); @@ -599,7 +599,7 @@ async function onAnkiFieldTemplatesReset(e) { const fieldTemplates = optionsFieldTemplates(); options.anki.fieldTemplates = fieldTemplates; $('#field-templates').val(fieldTemplates); - await optionsSave(options); + await apiOptionsSave(); } catch (e) { ankiErrorShow(e); } -- cgit v1.2.3 From 87e513dd77ab228953449d534a989c8c68f39858 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 10 Sep 2019 20:04:28 -0400 Subject: Fix options reference being changed --- ext/bg/js/api.js | 2 +- ext/bg/js/backend.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index a50353c1..13c0d73a 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -25,7 +25,7 @@ async function apiOptionsSave() { const backend = utilBackend(); const options = await backend.getFullOptions(); await optionsSave(options); - backend.onOptionsUpdated(options); + backend.onOptionsUpdated(); } async function apiTermsFind(text, optionsContext) { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 1f00f788..4763e85d 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -34,7 +34,8 @@ class Backend { async prepare() { await this.translator.prepare(); - this.onOptionsUpdated(await optionsLoad()); + this.options = await optionsLoad(); + this.onOptionsUpdated(); if (chrome.commands !== null && typeof chrome.commands === 'object') { chrome.commands.onCommand.addListener(this.onCommand.bind(this)); @@ -51,8 +52,7 @@ class Backend { this.isPreparedPromise = null; } - onOptionsUpdated(options) { - this.options = utilIsolate(options); + onOptionsUpdated() { this.applyOptions(); const callback = () => this.checkLastError(chrome.runtime.lastError); -- cgit v1.2.3 From 84bd9ff93b15f419ce1076b7545aeb406917f9b5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 10 Sep 2019 20:46:30 -0400 Subject: Update settings if a different source triggers optionsUpdate --- ext/bg/js/api.js | 6 +++--- ext/bg/js/backend.js | 6 +++--- ext/bg/js/settings.js | 43 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 13c0d73a..81772d08 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,11 +21,11 @@ function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } -async function apiOptionsSave() { +async function apiOptionsSave(source) { const backend = utilBackend(); const options = await backend.getFullOptions(); await optionsSave(options); - backend.onOptionsUpdated(); + backend.onOptionsUpdated(source); } async function apiTermsFind(text, optionsContext) { @@ -139,7 +139,7 @@ async function apiCommandExec(command) { const optionsContext = {depth: 0}; const options = await apiOptionsGet(optionsContext); options.general.enable = !options.general.enable; - await apiOptionsSave(); + await apiOptionsSave('popup'); } }; diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 4763e85d..9a300d62 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -35,7 +35,7 @@ class Backend { async prepare() { await this.translator.prepare(); this.options = await optionsLoad(); - this.onOptionsUpdated(); + this.onOptionsUpdated('background'); if (chrome.commands !== null && typeof chrome.commands === 'object') { chrome.commands.onCommand.addListener(this.onCommand.bind(this)); @@ -52,13 +52,13 @@ class Backend { this.isPreparedPromise = null; } - onOptionsUpdated() { + onOptionsUpdated(source) { this.applyOptions(); const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.query({}, tabs => { for (const tab of tabs) { - chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {}}, callback); + chrome.tabs.sendMessage(tab.id, {action: 'optionsUpdate', params: {source}}, callback); } }); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f2f08b6e..7f3e5c69 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -218,7 +218,7 @@ async function onFormOptionsChanged(e) { const optionsAnkiServerOld = options.anki.server; await formRead(options); - await apiOptionsSave(); + await settingsSaveOptions(); formUpdateVisibility(options); try { @@ -246,11 +246,44 @@ async function onReady() { await formWrite(options); storageInfoInitialize(); + + chrome.runtime.onMessage.addListener(onMessage); } $(document).ready(utilAsync(onReady)); +/* + * Remote options updates + */ + +function settingsGetSource() { + return new Promise((resolve) => { + chrome.tabs.getCurrent((tab) => resolve(`settings${tab ? tab.id : ''}`)); + }); +} + +async function settingsSaveOptions() { + const source = await settingsGetSource(); + await apiOptionsSave(source); +} + +async function onOptionsUpdate({source}) { + const thisSource = await settingsGetSource(); + if (source === thisSource) { return; } + + const optionsContext = getOptionsContext(); + const options = await apiOptionsGet(optionsContext); + await formWrite(options); +} + +function onMessage({action, params}) { + if (action === 'optionsUpdate') { + onOptionsUpdate(params); + } +} + + /* * Dictionary */ @@ -395,7 +428,7 @@ async function onDictionaryPurge(e) { const options = await apiOptionsGet(optionsContext); options.dictionaries = {}; options.general.mainDictionary = ''; - await apiOptionsSave(); + await settingsSaveOptions(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -445,7 +478,7 @@ async function onDictionaryImport(e) { dictionaryErrorsShow(exceptions); } - await apiOptionsSave(); + await settingsSaveOptions(); await dictionaryGroupsPopulate(options); await formMainDictionaryOptionsPopulate(options); @@ -589,7 +622,7 @@ async function onAnkiModelChanged(e) { const options = await apiOptionsGet(optionsContext); await formRead(options); options.anki[tabId].fields = {}; - await apiOptionsSave(); + await settingsSaveOptions(); ankiSpinnerShow(true); await ankiFieldsPopulate(element, options); @@ -609,7 +642,7 @@ async function onAnkiFieldTemplatesReset(e) { const fieldTemplates = optionsFieldTemplates(); options.anki.fieldTemplates = fieldTemplates; $('#field-templates').val(fieldTemplates); - await apiOptionsSave(); + await settingsSaveOptions(); } catch (e) { ankiErrorShow(e); } -- cgit v1.2.3 From c8171f5ec7612f0ba147d7e0887cd8c30a527827 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Sep 2019 19:50:58 -0400 Subject: Add preliminary support for profiles --- ext/bg/js/backend.js | 10 +++++- ext/bg/js/options.js | 91 +++++++++++++++++++++++++++++++++++++++++++-------- ext/bg/js/settings.js | 2 +- 3 files changed, 87 insertions(+), 16 deletions(-) (limited to 'ext/bg/js/backend.js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 9a300d62..3839da39 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -165,7 +165,15 @@ class Backend { } getOptionsSync(optionsContext) { - return this.options; + return this.getProfileSync(optionsContext).options; + } + + getProfileSync(optionsContext) { + const profiles = this.options.profiles; + if (typeof optionsContext.index === 'number') { + return profiles[optionsContext.index]; + } + return this.options.profiles[this.options.profileCurrent]; } setExtensionBadgeBackgroundColor(color) { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 5f04ec31..3dce5221 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -17,7 +17,11 @@ */ -function optionsApplyUpdates(options, updates) { +/* + * Generic options functions + */ + +function optionsGenericApplyUpdates(options, updates) { const targetVersion = updates.length; const currentVersion = options.version; if (typeof currentVersion === 'number' && Number.isFinite(currentVersion)) { @@ -33,7 +37,12 @@ function optionsApplyUpdates(options, updates) { return options; } -const optionsVersionUpdates = [ + +/* + * Per-profile options + */ + +const profileOptionsVersionUpdates = [ null, null, null, @@ -48,7 +57,7 @@ const optionsVersionUpdates = [ options.scanning.modifier = options.scanning.requireShift ? 'shift' : 'none'; }, (options) => { - const fieldTemplatesDefault = optionsFieldTemplates(); + const fieldTemplatesDefault = profileOptionsGetDefaultFieldTemplates(); options.general.resultOutputMode = options.general.groupResults ? 'group' : 'split'; options.anki.fieldTemplates = ( (utilStringHashCode(options.anki.fieldTemplates) !== -805327496) ? @@ -58,17 +67,17 @@ const optionsVersionUpdates = [ }, (options) => { if (utilStringHashCode(options.anki.fieldTemplates) === 1285806040) { - options.anki.fieldTemplates = optionsFieldTemplates(); + options.anki.fieldTemplates = profileOptionsGetDefaultFieldTemplates(); } }, (options) => { if (utilStringHashCode(options.anki.fieldTemplates) === -250091611) { - options.anki.fieldTemplates = optionsFieldTemplates(); + options.anki.fieldTemplates = profileOptionsGetDefaultFieldTemplates(); } } ]; -function optionsFieldTemplates() { +function profileOptionsGetDefaultFieldTemplates() { return ` {{#*inline "glossary-single"}} {{~#unless brief~}} @@ -234,7 +243,7 @@ function optionsFieldTemplates() { `.trim(); } -function optionsCreateDefaults() { +function profileOptionsCreateDefaults() { return { general: { enable: true, @@ -286,13 +295,13 @@ function optionsCreateDefaults() { screenshot: {format: 'png', quality: 92}, terms: {deck: '', model: '', fields: {}}, kanji: {deck: '', model: '', fields: {}}, - fieldTemplates: optionsFieldTemplates() + fieldTemplates: profileOptionsGetDefaultFieldTemplates() } }; } -function optionsSetDefaults(options) { - const defaults = optionsCreateDefaults(); +function profileOptionsSetDefaults(options) { + const defaults = profileOptionsCreateDefaults(); const combine = (target, source) => { for (const key in source) { @@ -312,9 +321,59 @@ function optionsSetDefaults(options) { return options; } -function optionsVersion(options) { - optionsSetDefaults(options); - return optionsApplyUpdates(options, optionsVersionUpdates); +function profileOptionsUpdateVersion(options) { + profileOptionsSetDefaults(options); + return optionsGenericApplyUpdates(options, profileOptionsVersionUpdates); +} + + +/* + * Global options + */ + +const optionsVersionUpdates = []; + +function optionsUpdateVersion(options, defaultProfileOptions) { + // Ensure profiles is an array + if (!Array.isArray(options.profiles)) { + options.profiles = []; + } + + // Remove invalid + const profiles = options.profiles; + for (let i = profiles.length - 1; i >= 0; --i) { + if (!utilIsObject(profiles[i])) { + profiles.splice(i, 1); + } + } + + // Require at least one profile + if (profiles.length === 0) { + profiles.push({ + name: 'Default', + options: defaultProfileOptions + }); + } + + // Ensure profileCurrent is valid + const profileCurrent = options.profileCurrent; + if (!( + typeof profileCurrent === 'number' && + Number.isFinite(profileCurrent) && + Math.floor(profileCurrent) === profileCurrent && + profileCurrent >= 0 && + profileCurrent < profiles.length + )) { + options.profileCurrent = 0; + } + + // Update profile options + for (const profile of profiles) { + profile.options = profileOptionsUpdateVersion(profile.options); + } + + // Generic updates + return optionsGenericApplyUpdates(options, optionsVersionUpdates); } function optionsLoad() { @@ -338,7 +397,11 @@ function optionsLoad() { }).catch(() => { return {}; }).then(options => { - return optionsVersion(options); + return ( + Array.isArray(options.profiles) ? + optionsUpdateVersion(options, {}) : + optionsUpdateVersion({}, options) + ); }); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 3d581ba5..88929c49 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -643,7 +643,7 @@ async function onAnkiFieldTemplatesReset(e) { e.preventDefault(); const optionsContext = getOptionsContext(); const options = await apiOptionsGet(optionsContext); - const fieldTemplates = optionsFieldTemplates(); + const fieldTemplates = profileOptionsGetDefaultFieldTemplates(); options.anki.fieldTemplates = fieldTemplates; $('#field-templates').val(fieldTemplates); await settingsSaveOptions(); -- cgit v1.2.3 From dcfe722ba626a439db621385005aaa57b61835ca Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 10 Sep 2019 19:55:14 -0400 Subject: Add support for using optionsContext to select which profile to use --- ext/bg/js/api.js | 5 ++++- ext/bg/js/backend.js | 38 ++++++++++++++++++++++++++++++++++++-- ext/bg/js/context.js | 5 ++++- ext/bg/js/profile-conditions.js | 16 ++++++++-------- ext/bg/js/search-frontend.js | 5 ++++- ext/bg/js/search.js | 3 ++- ext/bg/js/settings-profiles.js | 5 ++++- ext/fg/js/float.js | 8 +++++--- ext/fg/js/frontend.js | 20 +++++++++++++------- ext/fg/js/popup-nested.js | 6 +++--- ext/fg/js/popup-proxy.js | 3 ++- ext/fg/js/popup.js | 7 ++++++- 12 files changed, 91 insertions(+), 30 deletions(-) (limited to 'ext/bg/js/backend.js') 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) { diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 348c114e..fd7986b8 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -24,7 +24,8 @@ class DisplayFloat extends Display { this.styleNode = null; this.optionsContext = { - depth: 0 + depth: 0, + url: window.location.href }; 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; - popupNestedInitialize(id, depth, parentFrameId); + this.optionsContext.url = url; + popupNestedInitialize(id, depth, parentFrameId, url); } }; diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index c98a9a33..cef7fffd 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -27,7 +27,8 @@ class Frontend { this.ignoreNodes = (Array.isArray(ignoreNodes) && ignoreNodes.length > 0 ? ignoreNodes.join(',') : null); this.optionsContext = { - depth: popup.depth + depth: popup.depth, + url: popup.url }; this.primaryTouchIdentifier = null; @@ -42,9 +43,9 @@ class Frontend { static create() { const initializationData = window.frontendInitializationData; 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); frontend.prepare(); return frontend; @@ -52,7 +53,7 @@ class Frontend { async prepare() { try { - this.options = await apiOptionsGet(this.optionsContext); + this.options = await apiOptionsGet(this.getOptionsContext()); window.addEventListener('message', this.onFrameMessage.bind(this)); window.addEventListener('mousedown', this.onMouseDown.bind(this)); @@ -262,7 +263,7 @@ class Frontend { } async updateOptions() { - this.options = await apiOptionsGet(this.optionsContext); + this.options = await apiOptionsGet(this.getOptionsContext()); if (!this.options.enable) { this.searchClear(); } @@ -330,7 +331,7 @@ class Frontend { return; } - const {definitions, length} = await apiTermsFind(searchText, this.optionsContext); + const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext()); if (definitions.length === 0) { return false; } @@ -363,7 +364,7 @@ class Frontend { return; } - const definitions = await apiKanjiFind(searchText, this.optionsContext); + const definitions = await apiKanjiFind(searchText, this.getOptionsContext()); if (definitions.length === 0) { return false; } @@ -507,6 +508,11 @@ class Frontend { } } + getOptionsContext() { + this.optionsContext.url = this.popup.url; + return this.optionsContext; + } + static isScanningModifierPressed(scanningModifier, mouseEvent) { switch (scanningModifier) { case 'alt': return mouseEvent.altKey; diff --git a/ext/fg/js/popup-nested.js b/ext/fg/js/popup-nested.js index de2acccc..b36de2ec 100644 --- a/ext/fg/js/popup-nested.js +++ b/ext/fg/js/popup-nested.js @@ -19,13 +19,13 @@ let popupNestedInitialized = false; -async function popupNestedInitialize(id, depth, parentFrameId) { +async function popupNestedInitialize(id, depth, parentFrameId, url) { if (popupNestedInitialized) { return; } popupNestedInitialized = true; - const optionsContext = {depth}; + const optionsContext = {depth, url}; const options = await apiOptionsGet(optionsContext); const popupNestingMaxDepth = options.scanning.popupNestingMaxDepth; @@ -35,7 +35,7 @@ async function popupNestedInitialize(id, depth, parentFrameId) { const ignoreNodes = options.scanning.enableOnPopupExpressions ? [] : [ '.expression', '.expression *' ]; - window.frontendInitializationData = {id, depth, parentFrameId, ignoreNodes}; + window.frontendInitializationData = {id, depth, parentFrameId, ignoreNodes, url}; const scriptSrcs = [ '/fg/js/frontend-api-sender.js', diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index f04e24e0..235e1730 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -18,7 +18,7 @@ class PopupProxy { - constructor(depth, parentId, parentFrameId) { + constructor(depth, parentId, parentFrameId, url) { this.parentId = parentId; this.parentFrameId = parentFrameId; this.id = null; @@ -26,6 +26,7 @@ class PopupProxy { this.parent = null; this.child = null; this.depth = depth; + this.url = url; this.container = null; diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 8953cf30..08965084 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -59,7 +59,8 @@ class Popup { this.invokeApi('popupNestedInitialize', { id: this.id, depth: this.depth, - parentFrameId + parentFrameId, + url: this.url }); this.invokeApi('setOptions', { general: { @@ -311,4 +312,8 @@ class Popup { parent.appendChild(this.container); } } + + get url() { + return window.location.href; + } } -- cgit v1.2.3