From 1e839cd230e53f822478f945cb415a8af2b09aef Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 1 Aug 2020 16:22:00 -0400 Subject: More display refactoring (#697) * Remove some unnecessary _setQuery calls * Add support for forcing the query parser to be visible or hidden * Move _setEventListenersActive calls * Remove URL for kanji links * Refactor _setContentTermsOrKanji * Move search query text assignment into Display * Move title updates into Display * Move popup close calls * Prevent infinite loop of extension unload events --- ext/bg/js/search.js | 58 ++++--------- ext/bg/search.html | 2 +- ext/fg/float.html | 4 +- ext/mixed/css/display.css | 1 + ext/mixed/js/display-generator.js | 1 - ext/mixed/js/display.js | 172 +++++++++++++++++++++++++++----------- ext/mixed/js/yomichan.js | 9 +- 7 files changed, 153 insertions(+), 94 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index fc857368..d95fd5e4 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -63,11 +63,14 @@ class DisplaySearch extends Display { this.on('contentUpdating', this._onContentUpdating.bind(this)); + this.queryParserVisible = true; this.setHistorySettings({useBrowserHistory: true}); const options = this.getOptions(); - const {queryParams: {query='', mode=''}} = parseUrl(window.location.href); + const urlSearchParams = new URLSearchParams(location.search); + let mode = urlSearchParams.get('mode'); + if (mode === null) { mode = ''; } document.documentElement.dataset.searchMode = mode; @@ -78,8 +81,6 @@ class DisplaySearch extends Display { this._wanakanaEnable.checked = false; } - this._setQuery(query); - if (mode !== 'popup') { if (options.general.enableClipboardMonitor === true) { this._clipboardMonitorEnable.checked = true; @@ -147,14 +148,24 @@ class DisplaySearch extends Display { if (!this._isPrepared) { return; } const query = this._query.value; if (query) { - this._setQuery(query); this._onSearchQueryUpdated(query, false); } } + postProcessQuery(query) { + if (this._isWanakanaEnabled()) { + try { + query = wanakana.toKana(query); + } catch (e) { + // NOP + } + } + return query; + } + // Private - _onContentUpdating({type, source, content, urlSearchParams}) { + _onContentUpdating({type, content, source}) { let animate = false; let valid = false; switch (type) { @@ -173,13 +184,8 @@ class DisplaySearch extends Display { if (typeof source !== 'string') { source = ''; } - let full = urlSearchParams.get('full'); - if (full === null) { full = source; } - - this._closePopups(); - this._setQuery(full); + this._query.value = source; this._setIntroVisible(!valid, animate); - this._setTitleText(source); this._updateSearchButton(); } @@ -289,19 +295,6 @@ class DisplaySearch extends Display { return this._wanakanaEnable !== null && this._wanakanaEnable.checked; } - _setQuery(query) { - let interpretedQuery = query; - if (this._isWanakanaEnabled()) { - try { - interpretedQuery = wanakana.toKana(query); - } catch (e) { - // NOP - } - } - this._query.value = interpretedQuery; - this.setQueryParserText(interpretedQuery); - } - _setIntroVisible(visible, animate) { if (this._introVisible === visible) { return; @@ -362,19 +355,6 @@ class DisplaySearch extends Display { this._search.disabled = this._introVisible && (this._query === null || this._query.value.length === 0); } - _setTitleText(text) { - // Chrome limits title to 1024 characters - if (text.length > 1000) { - text = text.substring(0, 1000) + '...'; - } - - if (text.length === 0) { - document.title = 'Yomichan Search'; - } else { - document.title = `${text} - Yomichan Search`; - } - } - async _prepareNestedPopups() { let complete = false; @@ -401,8 +381,4 @@ class DisplaySearch extends Display { await onOptionsUpdated(); } - - _closePopups() { - yomichan.trigger('closePopups'); - } } diff --git a/ext/bg/search.html b/ext/bg/search.html index 653a708f..eb85e368 100644 --- a/ext/bg/search.html +++ b/ext/bg/search.html @@ -46,7 +46,7 @@ -
+
diff --git a/ext/fg/float.html b/ext/fg/float.html index 427a7e57..17378713 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -3,7 +3,7 @@ - + Yomichan Search @@ -21,7 +21,7 @@
-