From 9280985306f5a9c79b9bd2c4daea596a5ec78ae5 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 16 Sep 2017 23:08:43 -0700 Subject: add option to automatically hide search results (fixes #71) --- ext/fg/js/frontend.js | 21 ++++++++++----------- ext/fg/js/source.js | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 41c93f00..e4035289 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -169,27 +169,21 @@ class Frontend { } async searchAt(point) { - let textSource = null; + const textSource = docRangeFromPoint(point); + let hideResults = false; try { if (this.pendingLookup) { return; } - textSource = docRangeFromPoint(point); - if (!textSource || !textSource.containsPoint(point)) { - docImposterDestroy(); - return; - } - if (this.textSourceLast && this.textSourceLast.equals(textSource)) { return; } - this.pendingLookup = true; - - if (!await this.searchTerms(textSource)) { - await this.searchKanji(textSource); + if (textSource && textSource.containsPoint(point)) { + this.pendingLookup = true; + hideResults = !await this.searchTerms(textSource) && !await this.searchKanji(textSource); } } catch (e) { if (window.yomichan_orphaned) { @@ -201,6 +195,11 @@ class Frontend { } } finally { docImposterDestroy(); + + if (hideResults && this.options.scanning.autoHideResults) { + this.popup.hide(); + } + this.pendingLookup = false; } } diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index 3b6ecb2a..664dbec7 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -82,7 +82,7 @@ class TextSourceRange { } equals(other) { - return other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; + return other && other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; } static shouldEnter(node) { @@ -239,6 +239,6 @@ class TextSourceElement { } equals(other) { - return other.element === this.element && other.content === this.content; + return other && other.element === this.element && other.content === this.content; } } -- cgit v1.2.3 From 5476c100467e31d7dac02a4b1c14e62a4e9b34d0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 17 Sep 2017 10:09:48 -0700 Subject: more improvements to automatic search result hiding (#71) --- ext/bg/js/options.js | 2 +- ext/fg/js/frontend.js | 23 +++++++++-------------- ext/fg/js/popup.js | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 15 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index a245da80..36ab7694 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -147,7 +147,7 @@ function optionsSetDefaults(options) { selectText: true, alphanumeric: true, autoHideResults: false, - delay: 15, + delay: 20, length: 10, modifier: 'shift' }, diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index e4035289..63cdc25e 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -80,7 +80,6 @@ class Frontend { const search = async () => { try { await this.searchAt({x: e.clientX, y: e.clientY}); - this.pendingLookup = false; } catch (e) { this.onError(e); } @@ -169,19 +168,15 @@ class Frontend { } async searchAt(point) { + if (this.pendingLookup || this.popup.containsPoint(point)) { + return; + } + const textSource = docRangeFromPoint(point); - let hideResults = false; + let hideResults = !textSource || !textSource.containsPoint(point); try { - if (this.pendingLookup) { - return; - } - - if (this.textSourceLast && this.textSourceLast.equals(textSource)) { - return; - } - - if (textSource && textSource.containsPoint(point)) { + if (!hideResults && (!this.textSourceLast || !this.textSourceLast.equals(textSource))) { this.pendingLookup = true; hideResults = !await this.searchTerms(textSource) && !await this.searchKanji(textSource); } @@ -194,10 +189,10 @@ class Frontend { this.onError(e); } } finally { - docImposterDestroy(); - if (hideResults && this.options.scanning.autoHideResults) { - this.popup.hide(); + this.searchClear(); + } else { + docImposterDestroy(); } this.pendingLookup = false; diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 03958832..d1009fe9 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -100,6 +100,21 @@ class Popup { return this.injected && this.container.style.visibility !== 'hidden'; } + containsPoint(point) { + if (!this.isVisible()) { + return false; + } + + const rect = this.container.getBoundingClientRect(); + const contained = + point.x >= rect.left && + point.y >= rect.top && + point.x < rect.right && + point.y < rect.bottom; + + return contained; + } + async termsShow(elementRect, definitions, options, context) { await this.show(elementRect, options); this.invokeApi('termsShow', {definitions, options, context}); -- cgit v1.2.3 From 0554aa0d275dd989260ec8e426f35f946074c674 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 19 Sep 2017 23:05:17 -0700 Subject: make sure ranges are created over text nodes only (fixes #80) --- ext/fg/js/document.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index b1e71777..a1806d77 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -86,7 +86,7 @@ function docRangeFromPoint(point) { } const range = document.caretRangeFromPoint(point.x, point.y); - if (range) { + if (range && range.startContainer.nodeType === 3 && range.endContainer.nodeType === 3) { return new TextSourceRange(range); } } -- cgit v1.2.3 From 7a6e2925bdf94cf93c858323587d24e156d8b7ac Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 22 Sep 2017 19:57:00 -0700 Subject: improved error reporting --- ext/bg/js/backend.js | 2 +- ext/bg/js/search.js | 2 +- ext/fg/js/float.js | 2 +- ext/fg/js/frontend.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 7d68ed84..01340419 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -71,7 +71,7 @@ class Backend { return promise.then(result => { callback({result}); }).catch(error => { - callback({error}); + callback({error: error.toString ? error.toString() : error}); }); }; diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 54cda8ec..40bf2019 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -29,7 +29,7 @@ class DisplaySearch extends Display { } onError(error) { - window.alert(`Error: ${error}`); + window.alert(`Error: ${error.toString ? error.toString() : error}`); } onSearchClear() { diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 22374f8b..ff50483d 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -27,7 +27,7 @@ class DisplayFloat extends Display { if (window.yomichan_orphaned) { this.onOrphaned(); } else { - window.alert(`Error: ${error}`); + window.alert(`Error: ${error.toString ? error.toString() : error}`); } } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 63cdc25e..3a90b3ad 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -152,7 +152,7 @@ class Frontend { } onError(error) { - window.alert(`Error: ${error}`); + window.alert(`Error: ${error.toString ? error.toString() : error}`); } popupTimerSet(callback) { -- cgit v1.2.3