From 7a3315d75d91e37f69cf23faa0e5b189ad548e52 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 22 Oct 2019 20:23:03 -0400 Subject: Use chrome.runtime.getURL instead of chrome.extension.getURL --- ext/bg/js/api.js | 4 ++-- ext/bg/js/backend.js | 2 +- ext/bg/js/context.js | 4 ++-- ext/bg/js/translator.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 93d9c155..999ea337 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -153,7 +153,7 @@ async function apiCommandExec(command, params) { } apiCommandExec.handlers = { search: async (params) => { - const url = chrome.extension.getURL('/bg/search.html'); + const url = chrome.runtime.getURL('/bg/search.html'); if (!(params && params.newTab)) { try { const tab = await apiFindTab(1000, (url2) => ( @@ -181,7 +181,7 @@ apiCommandExec.handlers = { chrome.runtime.openOptionsPage(); } else { const manifest = chrome.runtime.getManifest(); - const url = chrome.extension.getURL(manifest.options_ui.page); + const url = chrome.runtime.getURL(manifest.options_ui.page); chrome.tabs.create({url}); } }, diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index f29230a2..23d876f6 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -45,7 +45,7 @@ class Backend { const options = this.getOptionsSync(this.optionsContext); if (options.general.showGuide) { - chrome.tabs.create({url: chrome.extension.getURL('/bg/guide.html')}); + chrome.tabs.create({url: chrome.runtime.getURL('/bg/guide.html')}); } this.isPreparedResolve(); diff --git a/ext/bg/js/context.js b/ext/bg/js/context.js index 8e1dbce6..3fb27f0d 100644 --- a/ext/bg/js/context.js +++ b/ext/bg/js/context.js @@ -55,8 +55,8 @@ $(document).ready(utilAsync(() => { const manifest = chrome.runtime.getManifest(); - setupButtonEvents('.action-open-search', 'search', chrome.extension.getURL('/bg/search.html')); - setupButtonEvents('.action-open-options', 'options', chrome.extension.getURL(manifest.options_ui.page)); + setupButtonEvents('.action-open-search', 'search', chrome.runtime.getURL('/bg/search.html')); + setupButtonEvents('.action-open-options', 'options', chrome.runtime.getURL(manifest.options_ui.page)); setupButtonEvents('.action-open-help', 'help'); const optionsContext = { diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index ee012d96..9d90136b 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -31,7 +31,7 @@ class Translator { } if (!this.deinflector) { - const url = chrome.extension.getURL('/bg/lang/deinflect.json'); + const url = chrome.runtime.getURL('/bg/lang/deinflect.json'); const reasons = await requestJson(url, 'GET'); this.deinflector = new Deinflector(reasons); } -- cgit v1.2.3 From aeac7bf2a87c10f01eafb85949633fc7ce17260c Mon Sep 17 00:00:00 2001 From: Danny Date: Wed, 23 Oct 2019 07:46:43 -0400 Subject: Proper spacing for Anki in {furigana-plain} This should fix #264 Note it does not address broken segmentation, just representation within Anki itself to work properly. --- ext/bg/js/handlebars.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index 92764a20..fba437da 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -49,13 +49,13 @@ function handlebarsFuriganaPlain(options) { let result = ''; for (const seg of segs) { if (seg.furigana) { - result += `${seg.text}[${seg.furigana}]`; + result += ` ${seg.text}[${seg.furigana}]`; } else { result += seg.text; } } - return result; + return result.trimLeft(); } function handlebarsKanjiLinks(options) { -- cgit v1.2.3 From a716a52cab143ae2e02b54c2f075a8731de16193 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 01:26:56 +0300 Subject: make non-hotkey keys focus input on search page The issue was that scanning on search page introduced a way to lose focus of the query input, and the new feature that the search page hotkey focuses an existing search page instead of opening a new one made it more obvious. Now every key that isn't a hotkey focuses the query input, and typing text into the box scrolls it into view in case it wasn't there when searching. There is an accessibility issue that this can cause, because now tab also focuses the query input before it focuses the next element. I didn't implement a workaround for that because it would have been more complicated than this simple fix. Fixes #263 --- ext/bg/js/search.js | 7 +++++++ ext/mixed/js/display.js | 2 ++ 2 files changed, 9 insertions(+) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 431478c9..5c0bffed 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -79,6 +79,7 @@ class DisplaySearch extends Display { onSearchInput() { this.updateSearchButton(); + this.query.scrollIntoView(); } onSearch(e) { @@ -94,6 +95,12 @@ class DisplaySearch extends Display { this.onSearchQueryUpdated(query, true); } + onKeyDown(e) { + if (!super.onKeyDown(e)) { + this.query.focus({preventScroll: true}); + } + } + async onSearchQueryUpdated(query, animate) { try { const valid = (query.length > 0); diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index b40228b0..bdc5e962 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -157,8 +157,10 @@ class Display { const handler = handlers[key]; if (handler(this, e)) { e.preventDefault(); + return true; } } + return false; } onWheel(e) { -- cgit v1.2.3 From 3a70346eb37ce234e49460ed0c97acb3affe68cc Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 03:26:24 +0300 Subject: fix various unwanted focus issues on search page Don't focus input if a modifier or specific keys are pressed --- ext/bg/js/search.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 5c0bffed..f0bbf203 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -96,7 +96,34 @@ class DisplaySearch extends Display { } onKeyDown(e) { - if (!super.onKeyDown(e)) { + const key = Display.getKeyFromEvent(e); + + let activeModifierMap = { + 'Control': e.ctrlKey, + 'Meta': e.metaKey + }; + // true if no known modifier is pressed + activeModifierMap[undefined] = !Object.values(activeModifierMap).includes(true); + + const ignoreKeys = { + undefined: ['Tab'], + 'Control': ['C', 'A', 'V'], + 'Meta': ['C', 'A', 'V'], + 'OS': [], + 'Alt': [], + 'Shift': [] + } + + let preventFocus = false; + for (const [modifier, keys] of Object.entries(ignoreKeys)) { + const modifierActive = activeModifierMap[modifier]; + if (key === modifier || (modifierActive && keys.includes(key))) { + preventFocus = true; + break; + } + } + + if (!super.onKeyDown(e) && !preventFocus) { this.query.focus({preventScroll: true}); } } -- cgit v1.2.3 From fe8eb76928de8ce72689b6ae3eb3a90c626c3720 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 03:39:08 +0300 Subject: tweak ignored keys on search page --- ext/bg/js/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index f0bbf203..d74998d6 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -107,8 +107,8 @@ class DisplaySearch extends Display { const ignoreKeys = { undefined: ['Tab'], - 'Control': ['C', 'A', 'V'], - 'Meta': ['C', 'A', 'V'], + 'Control': ['C', 'A', 'Z', 'Y', 'X', 'F'], + 'Meta': ['C', 'A', 'Z', 'Y', 'X', 'F'], 'OS': [], 'Alt': [], 'Shift': [] -- cgit v1.2.3 From 9132814ccf15ec2d1272c14e4840f4affad94706 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 14:30:36 +0300 Subject: focus and scroll to input only when it's needed --- ext/bg/js/search.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index d74998d6..1a6822f6 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -79,7 +79,11 @@ class DisplaySearch extends Display { onSearchInput() { this.updateSearchButton(); - this.query.scrollIntoView(); + + const queryElementRect = this.query.getBoundingClientRect(); + if (queryElementRect.top < 0 || queryElementRect.bottom > window.innerHeight) { + this.query.scrollIntoView(); + } } onSearch(e) { @@ -123,7 +127,7 @@ class DisplaySearch extends Display { } } - if (!super.onKeyDown(e) && !preventFocus) { + if (!super.onKeyDown(e) && !preventFocus && document.activeElement !== this.query) { this.query.focus({preventScroll: true}); } } -- cgit v1.2.3 From 50769feea71943051eaf12f1d96bc6305746959f Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 15:15:12 +0300 Subject: ignore more keys on search page --- ext/bg/js/search.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 1a6822f6..6481e16a 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -104,17 +104,21 @@ class DisplaySearch extends Display { let activeModifierMap = { 'Control': e.ctrlKey, - 'Meta': e.metaKey + 'Meta': e.metaKey, + 'ANY_MOD': true }; - // true if no known modifier is pressed - activeModifierMap[undefined] = !Object.values(activeModifierMap).includes(true); const ignoreKeys = { - undefined: ['Tab'], - 'Control': ['C', 'A', 'Z', 'Y', 'X', 'F'], - 'Meta': ['C', 'A', 'Z', 'Y', 'X', 'F'], + 'ANY_MOD': ['Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageDown', 'PageUp', 'Home', 'End'] + .concat( + Array.from(Array(24).keys()) + .map(i => `F${i + 1}`) + ), + 'Control': ['C', 'A', 'Z', 'Y', 'X', 'F', 'G'], + 'Meta': ['C', 'A', 'Z', 'Y', 'X', 'F', 'G'], 'OS': [], 'Alt': [], + 'AltGraph': [], 'Shift': [] } -- cgit v1.2.3 From bebd70b4e2d8da54b556b488d68a5111f3ff20c1 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 16:39:43 +0300 Subject: remember search history --- ext/bg/js/search.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 6481e16a..d66e68b8 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -58,6 +58,8 @@ class DisplaySearch extends Display { window.wanakana.bind(this.query); } + window.addEventListener('popstate', (e) => this.onPopState(e)); + this.updateSearchButton(); } catch (e) { this.onError(e); @@ -95,10 +97,23 @@ class DisplaySearch extends Display { const query = this.query.value; const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : ''; - window.history.replaceState(null, '', `${window.location.pathname}${queryString}`); + window.history.pushState({query}, '', `${window.location.pathname}${queryString}`); this.onSearchQueryUpdated(query, true); } + onPopState(e) { + let query = ''; + if (e.state && e.state.query) { + query = e.state.query + } + + if (this.query !== null) { + this.query.value = query; + } + + this.onSearchQueryUpdated(query, false); + } + onKeyDown(e) { const key = Display.getKeyFromEvent(e); -- cgit v1.2.3 From 704864b7b2365de488150c947d50e27c97d3bc4c Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 18:15:28 +0300 Subject: add clipboard monitor to search page Related to issue #262 about APIs --- ext/bg/js/search.js | 44 +++++++++++++++++++++++++++++++++++++++++++- ext/bg/search.html | 9 ++++++++- ext/manifest.json | 1 + 3 files changed, 52 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index d66e68b8..2d130522 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -29,8 +29,13 @@ class DisplaySearch extends Display { this.search = document.querySelector('#search'); this.query = document.querySelector('#query'); this.intro = document.querySelector('#intro'); + this.clipboardMonitorCheck = document.querySelector('#clipboard-monitor'); + this.introVisible = true; this.introAnimationTimer = null; + + this.clipboardMonitorIntervalId = null; + this.clipboardPrevText = null; } static create() { @@ -57,10 +62,20 @@ class DisplaySearch extends Display { window.wanakana.bind(this.query); } + if (this.clipboardMonitorCheck !== null) { + this.clipboardMonitorCheck.addEventListener('change', (e) => { + if (e.target.checked) { + this.startClipboardMonitor(); + } else { + this.stopClipboardMonitor(); + } + }); + } window.addEventListener('popstate', (e) => this.onPopState(e)); this.updateSearchButton(); + this.initClipboardMonitor(); } catch (e) { this.onError(e); } @@ -93,7 +108,9 @@ class DisplaySearch extends Display { return; } - e.preventDefault(); + if (e) { + e.preventDefault(); + } const query = this.query.value; const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : ''; @@ -182,6 +199,31 @@ class DisplaySearch extends Display { } } + initClipboardMonitor() { + // ignore copy from search page + window.addEventListener('copy', (e) => { + this.clipboardPrevText = document.getSelection().toString().trim(); + }); + } + + startClipboardMonitor() { + this.clipboardMonitorIntervalId = setInterval(async () => { + const curText = (await navigator.clipboard.readText()).trim(); + if (curText && (curText !== this.clipboardPrevText)) { + this.query.value = curText; + this.onSearch(); + this.clipboardPrevText = curText; + } + }, 100); + } + + stopClipboardMonitor() { + if (this.clipboardMonitorIntervalId) { + clearInterval(this.clipboardMonitorIntervalId); + this.clipboardMonitorIntervalId = null; + } + } + getOptionsContext() { return this.optionsContext; } diff --git a/ext/bg/search.html b/ext/bg/search.html index 9d28b358..1e650be0 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -19,7 +19,14 @@

Search your installed dictionaries by entering a Japanese expression into the field below.

-
+
+ + + + +
+ + diff --git a/ext/manifest.json b/ext/manifest.json index e913c2b0..2b7fc105 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -41,6 +41,7 @@ "permissions": [ "", "storage", + "clipboardRead", "clipboardWrite", "unlimitedStorage" ], -- cgit v1.2.3 From 303205dc124a1dde981db5b6401961f797e3a6d4 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 18:42:57 +0300 Subject: keep url query in history --- ext/bg/js/search.js | 1 + 1 file changed, 1 insertion(+) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 2d130522..8d2e7bf2 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -57,6 +57,7 @@ class DisplaySearch extends Display { const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); if (query !== null) { this.query.value = window.wanakana.toKana(query); + window.history.replaceState({query}, ''); this.onSearchQueryUpdated(query, false); } -- cgit v1.2.3 From bbbd23c842fae4a6f21afcf91fb797dd6f5709f7 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 19:12:13 +0300 Subject: add button to enable/disable wanakana IME --- ext/bg/js/search.js | 40 +++++++++++++++++++++++++++++++--------- ext/bg/search.html | 8 ++++++-- 2 files changed, 37 insertions(+), 11 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 8d2e7bf2..11d1d871 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -29,7 +29,8 @@ class DisplaySearch extends Display { this.search = document.querySelector('#search'); this.query = document.querySelector('#query'); this.intro = document.querySelector('#intro'); - this.clipboardMonitorCheck = document.querySelector('#clipboard-monitor'); + this.clipboardMonitorEnable = document.querySelector('#clipboard-monitor-enable'); + this.wanakanaEnable = document.querySelector('#wanakana-enable'); this.introVisible = true; this.introAnimationTimer = null; @@ -54,17 +55,31 @@ class DisplaySearch extends Display { if (this.query !== null) { this.query.addEventListener('input', () => this.onSearchInput(), false); - const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); + if (this.wanakanaEnable !== null) { + if (this.wanakanaEnable.checked) { + window.wanakana.bind(this.query); + } + this.wanakanaEnable.addEventListener('change', (e) => { + if (e.target.checked) { + window.wanakana.bind(this.query); + } else { + window.wanakana.unbind(this.query); + } + }); + } + + let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); if (query !== null) { - this.query.value = window.wanakana.toKana(query); + if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { + query = window.wanakana.toKana(query); + } + this.query.value = query; window.history.replaceState({query}, ''); this.onSearchQueryUpdated(query, false); } - - window.wanakana.bind(this.query); } - if (this.clipboardMonitorCheck !== null) { - this.clipboardMonitorCheck.addEventListener('change', (e) => { + if (this.clipboardMonitorEnable !== null) { + this.clipboardMonitorEnable.addEventListener('change', (e) => { if (e.target.checked) { this.startClipboardMonitor(); } else { @@ -203,13 +218,20 @@ class DisplaySearch extends Display { initClipboardMonitor() { // ignore copy from search page window.addEventListener('copy', (e) => { - this.clipboardPrevText = document.getSelection().toString().trim(); + let prevText = document.getSelection().toString().trim(); + if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { + prevText = window.wanakana.toKana(prevText); + } + this.clipboardPrevText = prevText; }); } startClipboardMonitor() { this.clipboardMonitorIntervalId = setInterval(async () => { - const curText = (await navigator.clipboard.readText()).trim(); + let curText = (await navigator.clipboard.readText()).trim(); + if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { + curText = window.wanakana.toKana(curText); + } if (curText && (curText !== this.clipboardPrevText)) { this.query.value = curText; this.onSearch(); diff --git a/ext/bg/search.html b/ext/bg/search.html index 1e650be0..11dca5a2 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -20,9 +20,13 @@
+ + + + - - + +
-- cgit v1.2.3 From 01ffb052e6bfc04c2a0e260769ac9183b2bd60a0 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 26 Oct 2019 19:32:41 +0300 Subject: simplify search history state handling --- ext/bg/js/search.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 11d1d871..bfd88552 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -74,7 +74,6 @@ class DisplaySearch extends Display { query = window.wanakana.toKana(query); } this.query.value = query; - window.history.replaceState({query}, ''); this.onSearchQueryUpdated(query, false); } } @@ -130,16 +129,12 @@ class DisplaySearch extends Display { const query = this.query.value; const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : ''; - window.history.pushState({query}, '', `${window.location.pathname}${queryString}`); + window.history.pushState(null, '', `${window.location.pathname}${queryString}`); this.onSearchQueryUpdated(query, true); } onPopState(e) { - let query = ''; - if (e.state && e.state.query) { - query = e.state.query - } - + const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || ''; if (this.query !== null) { this.query.value = query; } -- cgit v1.2.3 From 7ee87265cd937a8dd584c509d3c8ed45c96c221f Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 01:26:17 +0300 Subject: refactor and tune wanakana toggling --- ext/bg/js/search.js | 54 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index bfd88552..8484e042 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -60,21 +60,26 @@ class DisplaySearch extends Display { window.wanakana.bind(this.query); } 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); } else { window.wanakana.unbind(this.query); + this.query.value = query; } + this.onSearchQueryUpdated(this.query.value, false); }); } - let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); + const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); if (query !== null) { - if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { - query = window.wanakana.toKana(query); + if (this.isWanakanaEnabled()) { + this.query.value = window.wanakana.toKana(query); + } else { + this.query.value = query; } - this.query.value = query; - this.onSearchQueryUpdated(query, false); + this.onSearchQueryUpdated(this.query.value, false); } } if (this.clipboardMonitorEnable !== null) { @@ -123,9 +128,7 @@ class DisplaySearch extends Display { return; } - if (e) { - e.preventDefault(); - } + e.preventDefault(); const query = this.query.value; const queryString = query.length > 0 ? `?query=${encodeURIComponent(query)}` : ''; @@ -136,10 +139,14 @@ class DisplaySearch extends Display { onPopState(e) { const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || ''; if (this.query !== null) { - this.query.value = query; + if (this.isWanakanaEnabled()) { + this.query.value = window.wanakana.toKana(query); + } else { + this.query.value = query; + } } - this.onSearchQueryUpdated(query, false); + this.onSearchQueryUpdated(this.query.value, false); } onKeyDown(e) { @@ -213,23 +220,24 @@ class DisplaySearch extends Display { initClipboardMonitor() { // ignore copy from search page window.addEventListener('copy', (e) => { - let prevText = document.getSelection().toString().trim(); - if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { - prevText = window.wanakana.toKana(prevText); - } - this.clipboardPrevText = prevText; + this.clipboardPrevText = document.getSelection().toString().trim(); }); } startClipboardMonitor() { this.clipboardMonitorIntervalId = setInterval(async () => { - let curText = (await navigator.clipboard.readText()).trim(); - if (this.wanakanaEnable !== null && this.wanakanaEnable.checked) { - curText = window.wanakana.toKana(curText); - } + const curText = (await navigator.clipboard.readText()).trim(); if (curText && (curText !== this.clipboardPrevText)) { - this.query.value = curText; - this.onSearch(); + if (this.isWanakanaEnabled()) { + this.query.value = window.wanakana.toKana(curText); + } else { + this.query.value = curText; + } + + const queryString = curText.length > 0 ? `?query=${encodeURIComponent(curText)}` : ''; + window.history.pushState(null, '', `${window.location.pathname}${queryString}`); + this.onSearchQueryUpdated(this.query.value, true); + this.clipboardPrevText = curText; } }, 100); @@ -242,6 +250,10 @@ class DisplaySearch extends Display { } } + isWanakanaEnabled() { + return this.wanakanaEnable !== null && this.wanakanaEnable.checked; + } + getOptionsContext() { return this.optionsContext; } -- cgit v1.2.3 From d3f51690f8bb236d1ba3c79c20b3a60d3e62dc52 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 01:51:18 +0300 Subject: make clipboardRead an optional permission --- ext/bg/js/search.js | 11 ++++++++++- ext/manifest.json | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 8484e042..a09ca822 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -85,7 +85,16 @@ class DisplaySearch extends Display { if (this.clipboardMonitorEnable !== null) { this.clipboardMonitorEnable.addEventListener('change', (e) => { if (e.target.checked) { - this.startClipboardMonitor(); + chrome.permissions.request( + {permissions: ['clipboardRead']}, + (granted) => { + if (granted) { + this.startClipboardMonitor(); + } else { + e.target.checked = false; + } + } + ); } else { this.stopClipboardMonitor(); } diff --git a/ext/manifest.json b/ext/manifest.json index 2b7fc105..6390cbfb 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -41,10 +41,12 @@ "permissions": [ "", "storage", - "clipboardRead", "clipboardWrite", "unlimitedStorage" ], + "optional_permissions": [ + "clipboardRead" + ], "commands": { "toggle": { "suggested_key": { -- cgit v1.2.3 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/bg/background.html | 2 ++ ext/bg/js/api.js | 8 ++++++++ ext/bg/js/backend.js | 5 ++++- ext/bg/js/search.js | 14 +++++++++++++- ext/fg/js/api.js | 4 ++++ 5 files changed, 31 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/background.html b/ext/bg/background.html index 194d4a45..30b3db48 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -5,6 +5,8 @@ +
+ diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 999ea337..88eef431 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -401,3 +401,11 @@ async function apiFocusTab(tab) { // Edge throws exception for no reason here. } } + +async function apiClipboardGet() { + const clipboardPasteTarget = utilBackend().clipboardPasteTarget; + clipboardPasteTarget.innerText = ''; + clipboardPasteTarget.focus(); + document.execCommand('paste'); + return clipboardPasteTarget.innerText; +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 23d876f6..7192d026 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -30,6 +30,8 @@ class Backend { this.isPreparedResolve = null; this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); + this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); + this.apiForwarder = new BackendApiForwarder(); } @@ -187,7 +189,8 @@ Backend.messageHandlers = { forward: ({action, params}, sender) => apiForward(action, params, sender), frameInformationGet: (params, sender) => apiFrameInformationGet(sender), injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender), - getEnvironmentInfo: () => apiGetEnvironmentInfo() + getEnvironmentInfo: () => apiGetEnvironmentInfo(), + clipboardGet: () => apiClipboardGet() }; window.yomichan_backend = new Backend(); diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index a09ca822..dca4e8fa 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -17,6 +17,12 @@ */ +let IS_FIREFOX = null; +(async () => { + const {browser} = await apiGetEnvironmentInfo(); + IS_FIREFOX = ['firefox', 'firefox-mobile'].includes(browser); +})(); + class DisplaySearch extends Display { constructor() { super(document.querySelector('#spinner'), document.querySelector('#content')); @@ -235,7 +241,13 @@ class DisplaySearch extends Display { startClipboardMonitor() { this.clipboardMonitorIntervalId = setInterval(async () => { - const curText = (await navigator.clipboard.readText()).trim(); + let curText = null; + // TODO get rid of this and figure out why apiClipboardGet doesn't work on Firefox + if (IS_FIREFOX) { + curText = (await navigator.clipboard.readText()).trim(); + } else if (IS_FIREFOX === false) { + curText = (await apiClipboardGet()).trim(); + } if (curText && (curText !== this.clipboardPrevText)) { if (this.isWanakanaEnabled()) { this.query.value = window.wanakana.toKana(curText); 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/bg/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 From 51c35c9f306e48093fc769713675dca5b02d1398 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 22:43:20 +0200 Subject: modify correct profile apiOptionsSet --- ext/bg/js/api.js | 4 ++-- ext/bg/js/backend.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 6c109614..95787d7d 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -23,7 +23,7 @@ function apiOptionsGet(optionsContext) { async function apiOptionsSet(changedOptions, optionsContext, source) { const backend = utilBackend(); - const {depth} = optionsContext; + const {profileIndex} = backend.getProfileFromContext(optionsContext); let options = await apiOptionsGetFull(); function getValuePaths(obj) { @@ -63,7 +63,7 @@ async function apiOptionsSet(changedOptions, optionsContext, source) { } for (let [value, path] of getValuePaths(changedOptions)) { - modifyOption(path, value, options.profiles[depth].options); + modifyOption(path, value, options.profiles[profileIndex].options); } await optionsSave(options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 71393467..6d2e736e 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -122,15 +122,15 @@ class Backend { if (typeof optionsContext.index === 'number') { return profiles[optionsContext.index]; } - const profile = this.getProfileFromContext(optionsContext); + const {profile} = this.getProfileFromContext(optionsContext); return profile !== null ? profile : this.options.profiles[this.options.profileCurrent]; } getProfileFromContext(optionsContext) { - for (const profile of this.options.profiles) { + for (const [profileIndex, profile] of this.options.profiles.entries()) { const conditionGroups = profile.conditionGroups; if (conditionGroups.length > 0 && Backend.testConditionGroups(conditionGroups, optionsContext)) { - return profile; + return {profileIndex, profile}; } } return null; -- cgit v1.2.3 From 9641747ba173eb6cc64b5881759d225253935e4a Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 23:02:41 +0200 Subject: Revert "modify correct profile apiOptionsSet" This reverts commit 51c35c9f306e48093fc769713675dca5b02d1398. --- ext/bg/js/api.js | 4 ++-- ext/bg/js/backend.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 95787d7d..6c109614 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -23,7 +23,7 @@ function apiOptionsGet(optionsContext) { async function apiOptionsSet(changedOptions, optionsContext, source) { const backend = utilBackend(); - const {profileIndex} = backend.getProfileFromContext(optionsContext); + const {depth} = optionsContext; let options = await apiOptionsGetFull(); function getValuePaths(obj) { @@ -63,7 +63,7 @@ async function apiOptionsSet(changedOptions, optionsContext, source) { } for (let [value, path] of getValuePaths(changedOptions)) { - modifyOption(path, value, options.profiles[profileIndex].options); + modifyOption(path, value, options.profiles[depth].options); } await optionsSave(options); diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 6d2e736e..71393467 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -122,15 +122,15 @@ class Backend { if (typeof optionsContext.index === 'number') { return profiles[optionsContext.index]; } - const {profile} = this.getProfileFromContext(optionsContext); + const profile = this.getProfileFromContext(optionsContext); return profile !== null ? profile : this.options.profiles[this.options.profileCurrent]; } getProfileFromContext(optionsContext) { - for (const [profileIndex, profile] of this.options.profiles.entries()) { + for (const profile of this.options.profiles) { const conditionGroups = profile.conditionGroups; if (conditionGroups.length > 0 && Backend.testConditionGroups(conditionGroups, optionsContext)) { - return {profileIndex, profile}; + return profile; } } return null; -- cgit v1.2.3 From 68179607b3ac8ed34f32a4a7699fb95c7826249b Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 23:34:50 +0200 Subject: use apiOptionsGet to modify the correct profile --- ext/bg/js/api.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 6c109614..14fc0279 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -22,9 +22,7 @@ function apiOptionsGet(optionsContext) { } async function apiOptionsSet(changedOptions, optionsContext, source) { - const backend = utilBackend(); - const {depth} = optionsContext; - let options = await apiOptionsGetFull(); + let options = await apiOptionsGet(optionsContext); function getValuePaths(obj) { let valuePaths = []; @@ -63,11 +61,10 @@ async function apiOptionsSet(changedOptions, optionsContext, source) { } for (let [value, path] of getValuePaths(changedOptions)) { - modifyOption(path, value, options.profiles[depth].options); + modifyOption(path, value, options); } - await optionsSave(options); - backend.onOptionsUpdated(source); + await apiOptionsSave(source); } function apiOptionsGetFull() { -- cgit v1.2.3 From d62d04f806d2e6e5f2c3ac69a7d62f00cc31bd1b Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 23:52:16 +0200 Subject: fix wanakana toggle on empty input --- ext/bg/js/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 65cca002..dbfcb15d 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -69,7 +69,7 @@ class DisplaySearch extends Display { this.wanakanaEnable.checked = false; } this.wanakanaEnable.addEventListener('change', (e) => { - let query = DisplaySearch.getSearchQueryFromLocation(window.location.href); + const query = DisplaySearch.getSearchQueryFromLocation(window.location.href) || ''; if (e.target.checked) { window.wanakana.bind(this.query); this.query.value = window.wanakana.toKana(query); -- cgit v1.2.3 From a31ee0a0e9b4f1198cb9ac43d205267b8cf45bb2 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 28 Oct 2019 00:09:36 +0200 Subject: refactoring --- ext/bg/js/api.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 14fc0279..3209cc31 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -22,12 +22,12 @@ function apiOptionsGet(optionsContext) { } async function apiOptionsSet(changedOptions, optionsContext, source) { - let options = await apiOptionsGet(optionsContext); + const options = await apiOptionsGet(optionsContext); function getValuePaths(obj) { let valuePaths = []; let nodes = [{ - obj: changedOptions, + obj, path: [] }]; while (nodes.length > 0) { -- cgit v1.2.3 From dcb6f68826bd64d6cb41d2d7a0d5da1a58da9a1b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:01:04 -0400 Subject: Don't pass null textSource into searchSource --- ext/bg/js/settings-popup-preview.js | 7 ++++++- ext/fg/js/frontend.js | 17 +++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index b12fb726..ce6da4c0 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -158,9 +158,14 @@ class SettingsPopupPreview { const range = document.createRange(); range.selectNode(textNode); const source = new TextSourceRange(range, range.toString(), null); + if (source === null) { return; } this.frontend.textSourceLast = null; - await this.frontend.searchSource(source, 'script'); + try { + await this.frontend.searchSource(source, 'script'); + } finally { + source.cleanup(); + } await this.frontend.lastShowPromise; if (this.frontend.popup.isVisible()) { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 897c7b73..5e2ef529 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -319,19 +319,27 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - return await this.searchSource(textSource, cause); + if (textSource === null) { + return; + } + + try { + return await this.searchSource(textSource, cause); + } finally { + textSource.cleanup(); + } } catch (e) { this.onError(e); } } async searchSource(textSource, cause) { - let hideResults = textSource === null; + let hideResults = false; let searched = false; let success = false; try { - if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) { + if (!this.textSourceLast || !this.textSourceLast.equals(textSource)) { searched = true; this.pendingLookup = true; const focus = (cause === 'mouse'); @@ -351,9 +359,6 @@ class Frontend { this.onError(e); } } finally { - if (textSource !== null) { - textSource.cleanup(); - } if (hideResults && this.options.scanning.autoHideResults) { this.searchClear(true); } -- cgit v1.2.3 From f927f806ba602867948654947dce4d27dfc07ebf Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 24 Oct 2019 20:07:17 -0400 Subject: Move check --- ext/bg/js/settings-popup-preview.js | 1 - ext/fg/js/frontend.js | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index ce6da4c0..b7b3acc5 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -160,7 +160,6 @@ class SettingsPopupPreview { const source = new TextSourceRange(range, range.toString(), null); if (source === null) { return; } - this.frontend.textSourceLast = null; try { await this.frontend.searchSource(source, 'script'); } finally { diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 957f57c9..1ab3c1a1 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -319,7 +319,10 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - if (textSource === null) { + if ( + textSource === null || + (this.textSourceLast !== null && this.textSourceLast.equals(textSource)) + ) { return; } @@ -337,11 +340,9 @@ class Frontend { let hideResults = false; try { - if (!this.textSourceLast || !this.textSourceLast.equals(textSource)) { - this.pendingLookup = true; - const focus = (cause === 'mouse'); - hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); - } + this.pendingLookup = true; + const focus = (cause === 'mouse'); + hideResults = !await this.searchTerms(textSource, focus) && !await this.searchKanji(textSource, focus); } catch (e) { if (window.yomichan_orphaned) { if (textSource && this.options.scanning.modifier !== 'none') { -- cgit v1.2.3 From 75ff05148d5a07c397d11b8bfbc92833be8726ea Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 3 Nov 2019 11:06:31 -0500 Subject: Fix autoHideResults not working --- ext/bg/js/settings-popup-preview.js | 1 - ext/fg/js/frontend.js | 31 ++++++++++++++++--------------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/settings-popup-preview.js b/ext/bg/js/settings-popup-preview.js index b7b3acc5..7d641c46 100644 --- a/ext/bg/js/settings-popup-preview.js +++ b/ext/bg/js/settings-popup-preview.js @@ -158,7 +158,6 @@ class SettingsPopupPreview { const range = document.createRange(); range.selectNode(textNode); const source = new TextSourceRange(range, range.toString(), null); - if (source === null) { return; } try { await this.frontend.searchSource(source, 'script'); diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 94c318d7..802221be 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -336,17 +336,16 @@ class Frontend { } const textSource = docRangeFromPoint(x, y, this.options); - if ( - textSource === null || - (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) - ) { + if (this.textSourceCurrent !== null && this.textSourceCurrent.equals(textSource)) { return; } try { - return await this.searchSource(textSource, cause); + await this.searchSource(textSource, cause); } finally { - textSource.cleanup(); + if (textSource !== null) { + textSource.cleanup(); + } } } catch (e) { this.onError(e); @@ -358,17 +357,19 @@ class Frontend { try { this.pendingLookup = true; - results = ( - await this.findTerms(textSource) || - await this.findKanji(textSource) - ); - if (results !== null) { - const focus = (cause === 'mouse'); - this.showContent(textSource, focus, results.definitions, results.type); + if (textSource !== null) { + results = ( + await this.findTerms(textSource) || + await this.findKanji(textSource) + ); + if (results !== null) { + const focus = (cause === 'mouse'); + this.showContent(textSource, focus, results.definitions, results.type); + } } } catch (e) { if (window.yomichan_orphaned) { - if (textSource && this.options.scanning.modifier !== 'none') { + if (textSource !== null && this.options.scanning.modifier !== 'none') { this.lastShowPromise = this.popup.showContent( textSource.getRect(), textSource.getWritingMode(), @@ -386,7 +387,7 @@ class Frontend { this.pendingLookup = false; } - return results !== null; + return results; } showContent(textSource, focus, definitions, type) { -- cgit v1.2.3