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/bg/js/options.js | 1 + ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 4 ++++ ext/fg/js/frontend.js | 21 ++++++++++----------- ext/fg/js/source.js | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index dcad97d4..a245da80 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -146,6 +146,7 @@ function optionsSetDefaults(options) { middleMouse: true, selectText: true, alphanumeric: true, + autoHideResults: false, delay: 15, length: 10, modifier: 'shift' diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index a2580c35..d73d7509 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -35,6 +35,7 @@ async function formRead() { optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); optionsNew.scanning.selectText = $('#select-matched-text').prop('checked'); optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked'); + optionsNew.scanning.autoHideResults = $('#auto-hide-results').prop('checked'); optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); optionsNew.scanning.modifier = $('#scan-modifier-key').val(); @@ -136,6 +137,7 @@ async function onReady() { $('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse); $('#select-matched-text').prop('checked', options.scanning.selectText); $('#search-alphanumeric').prop('checked', options.scanning.alphanumeric); + $('#auto-hide-results').prop('checked', options.scanning.autoHideResults); $('#scan-delay').val(options.scanning.delay); $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 0a5c205c..c2612967 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -96,6 +96,10 @@ +
+ +
+
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