From 48776145d6bdb8aff82e82546583c790353e75b6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 15:46:27 +0200 Subject: add workaround to Chrome clipboard.readText For some reason this doesn't work on Firefox, so keep using the new API for Firefox --- ext/fg/js/api.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ext/fg/js/api.js') diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index b0746b85..bbc9b5fc 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -72,3 +72,7 @@ function apiInjectStylesheet(css) { function apiGetEnvironmentInfo() { return utilInvoke('getEnvironmentInfo'); } + +function apiClipboardGet() { + return utilInvoke('clipboardGet'); +} -- cgit v1.2.3 From 70418202cf2739258a4e95fdc7f0fbe4c7c46821 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 20:11:23 +0200 Subject: make search page checkbox options persist --- ext/bg/js/api.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/backend.js | 1 + ext/bg/js/options.js | 4 +++- ext/bg/js/search.js | 15 ++++++++++++++- ext/bg/search.html | 2 +- ext/fg/js/api.js | 4 ++++ 6 files changed, 72 insertions(+), 3 deletions(-) (limited to 'ext/fg/js/api.js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 88eef431..6c109614 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,6 +21,55 @@ function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } +async function apiOptionsSet(changedOptions, optionsContext, source) { + const backend = utilBackend(); + const {depth} = optionsContext; + let options = await apiOptionsGetFull(); + + function getValuePaths(obj) { + let valuePaths = []; + let nodes = [{ + obj: changedOptions, + path: [] + }]; + while (nodes.length > 0) { + let node = nodes.pop(); + Object.keys(node.obj).forEach((key) => { + let path = node.path.concat(key); + let value = node.obj[key]; + if (typeof value === 'object') { + nodes.unshift({ + obj: value, + path: path + }); + } else { + valuePaths.push([value, path]); + } + }); + } + return valuePaths; + } + + function modifyOption(path, value, options) { + let pivot = options; + for (let pathKey of path.slice(0, -1)) { + if (!(pathKey in pivot)) { + return false; + } + pivot = pivot[pathKey]; + } + pivot[path[path.length - 1]] = value; + return true; + } + + for (let [value, path] of getValuePaths(changedOptions)) { + modifyOption(path, value, options.profiles[depth].options); + } + + await optionsSave(options); + backend.onOptionsUpdated(source); +} + function apiOptionsGetFull() { return utilBackend().getFullOptions(); } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 7192d026..71393467 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -177,6 +177,7 @@ class Backend { Backend.messageHandlers = { optionsGet: ({optionsContext}) => apiOptionsGet(optionsContext), + optionsSet: ({changedOptions, optionsContext, source}) => apiOptionsSet(changedOptions, optionsContext, source), kanjiFind: ({text, optionsContext}) => apiKanjiFind(text, optionsContext), termsFind: ({text, optionsContext}) => apiTermsFind(text, optionsContext), definitionAdd: ({definition, mode, context, optionsContext}) => apiDefinitionAdd(definition, mode, context, optionsContext), diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 4854cd65..be1ccfbb 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -279,7 +279,9 @@ function profileOptionsCreateDefaults() { popupTheme: 'default', popupOuterTheme: 'default', customPopupCss: '', - customPopupOuterCss: '' + customPopupOuterCss: '', + enableWanakana: true, + enableClipboardMonitor: false }, audio: { diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index dca4e8fa..65cca002 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -62,17 +62,22 @@ class DisplaySearch extends Display { this.query.addEventListener('input', () => this.onSearchInput(), false); if (this.wanakanaEnable !== null) { - if (this.wanakanaEnable.checked) { + if (this.options.general.enableWanakana === true) { + this.wanakanaEnable.checked = true; window.wanakana.bind(this.query); + } else { + this.wanakanaEnable.checked = false; } this.wanakanaEnable.addEventListener('change', (e) => { let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); if (e.target.checked) { window.wanakana.bind(this.query); this.query.value = window.wanakana.toKana(query); + apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext()); } else { window.wanakana.unbind(this.query); this.query.value = query; + apiOptionsSet({general: {enableWanakana: false}}, this.getOptionsContext()); } this.onSearchQueryUpdated(this.query.value, false); }); @@ -89,6 +94,12 @@ class DisplaySearch extends Display { } } if (this.clipboardMonitorEnable !== null) { + if (this.options.general.enableClipboardMonitor === true) { + this.clipboardMonitorEnable.checked = true; + this.startClipboardMonitor(); + } else { + this.clipboardMonitorEnable.checked = false; + } this.clipboardMonitorEnable.addEventListener('change', (e) => { if (e.target.checked) { chrome.permissions.request( @@ -96,6 +107,7 @@ class DisplaySearch extends Display { (granted) => { if (granted) { this.startClipboardMonitor(); + apiOptionsSet({general: {enableClipboardMonitor: true}}, this.getOptionsContext()); } else { e.target.checked = false; } @@ -103,6 +115,7 @@ class DisplaySearch extends Display { ); } else { this.stopClipboardMonitor(); + apiOptionsSet({general: {enableClipboardMonitor: false}}, this.getOptionsContext()); } }); } diff --git a/ext/bg/search.html b/ext/bg/search.html index 11dca5a2..8b339cc7 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -22,7 +22,7 @@
- + diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index bbc9b5fc..54818702 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -21,6 +21,10 @@ function apiOptionsGet(optionsContext) { return utilInvoke('optionsGet', {optionsContext}); } +function apiOptionsSet(changedOptions, optionsContext, source) { + return utilInvoke('optionsSet', {changedOptions, optionsContext, source}); +} + function apiTermsFind(text, optionsContext) { return utilInvoke('termsFind', {text, optionsContext}); } -- cgit v1.2.3