From 068b1eef71ed1167e7e39effa00cda7deb9251f2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 23 Nov 2020 20:31:48 -0500 Subject: Text scanner improvements (#1056) * Only ignore nodes on non-web pages * Fix issue where options might not be assigned on nested frontends * Refactor default TextScanner options * Add option to enable search only on click * Simplify restore state assignment * Update options context passing * Fix empty title * Use TextScanner to scan content inside of Display * Rename ignoreNodes to excludeSelector(s) * Fix options update incorrectly triggering a re-search * Fix copy throwing an error on the search page * Replace _onSearchQueryUpdated with _search * Use include selector instead of exclude selector --- ext/bg/js/query-parser.js | 2 -- ext/bg/js/search.js | 66 ++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 37 deletions(-) (limited to 'ext/bg') diff --git a/ext/bg/js/query-parser.js b/ext/bg/js/query-parser.js index 16af77b2..d3065188 100644 --- a/ext/bg/js/query-parser.js +++ b/ext/bg/js/query-parser.js @@ -34,8 +34,6 @@ class QueryParser extends EventDispatcher { this._queryParserModeSelect = document.querySelector('#query-parser-mode-select'); this._textScanner = new TextScanner({ node: this._queryParser, - ignoreElements: () => [], - ignorePoint: null, getOptionsContext, documentUtil, searchTerms: true, diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 498f4ade..476370bf 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -62,7 +62,7 @@ class DisplaySearch extends Display { async prepare() { await super.prepare(); await this.updateOptions(); - yomichan.on('optionsUpdated', () => this.updateOptions()); + yomichan.on('optionsUpdated', this._onOptionsUpdated.bind(this)); this.on('contentUpdating', this._onContentUpdating.bind(this)); this.on('modeChange', this._onModeChange.bind(this)); @@ -126,15 +126,6 @@ class DisplaySearch extends Display { } } - async updateOptions() { - await super.updateOptions(); - if (!this._isPrepared) { return; } - const query = this._queryInput.value; - if (query) { - this._onSearchQueryUpdated(query, false); - } - } - postProcessQuery(query) { if (this._wanakanaEnabled) { try { @@ -148,6 +139,14 @@ class DisplaySearch extends Display { // Private + async _onOptionsUpdated() { + await this.updateOptions(); + const query = this._queryInput.value; + if (query) { + this._search(false); + } + } + _onContentUpdating({type, content, source}) { let animate = false; let valid = false; @@ -183,12 +182,12 @@ class DisplaySearch extends Display { e.preventDefault(); e.stopImmediatePropagation(); this.blurElement(e.currentTarget); - this._search(); + this._search(true); } _onSearch(e) { e.preventDefault(); - this._search(); + this._search(true); } _onCopy() { @@ -197,27 +196,8 @@ class DisplaySearch extends Display { } _onExternalSearchUpdate({text, animate=true}) { - this._onSearchQueryUpdated(text, animate); - } - - _onSearchQueryUpdated(query, animate) { - const details = { - focus: false, - history: false, - params: { - query - }, - state: { - focusEntry: 0, - sentence: {text: query, offset: 0}, - url: window.location.href - }, - content: { - definitions: null, - animate - } - }; - this.setContent(details); + this._queryInput.value = text; + this._search(animate); } _onWanakanaEnableChange(e) { @@ -362,9 +342,25 @@ class DisplaySearch extends Display { }); } - _search() { + _search(animate) { const query = this._queryInput.value; - this._onSearchQueryUpdated(query, true); + const details = { + focus: false, + history: false, + params: { + query + }, + state: { + focusEntry: 0, + sentence: {text: query, offset: 0}, + url: window.location.href + }, + content: { + definitions: null, + animate + } + }; + this.setContent(details); } _updateSearchHeight() { -- cgit v1.2.3