diff options
author | Alex Yatskov <alex@foosoft.net> | 2019-10-20 11:23:20 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2019-10-20 11:23:20 -0700 |
commit | 438498435227cfa59cf9ed3430045b288cd2a7c0 (patch) | |
tree | 6a05520e5d6fa8d26d372673a9ed3e5d2da7e3fd /ext/bg/js/search.js | |
parent | 06d7713189be9eb51669d3842b78278371e6cfa4 (diff) | |
parent | d32fd1381b6cd5141a21c22f9ef639b2fe9774fb (diff) |
Merge branch 'master' into testing
Diffstat (limited to 'ext/bg/js/search.js')
-rw-r--r-- | ext/bg/js/search.js | 71 |
1 files changed, 56 insertions, 15 deletions
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index ead9ba6f..431478c9 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -31,25 +31,37 @@ class DisplaySearch extends Display { this.intro = document.querySelector('#intro'); this.introVisible = true; this.introAnimationTimer = null; + } - this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); + static create() { + const instance = new DisplaySearch(); + instance.prepare(); + return instance; + } - if (this.search !== null) { - this.search.addEventListener('click', (e) => this.onSearch(e), false); - } - if (this.query !== null) { - this.query.addEventListener('input', () => this.onSearchInput(), false); + async prepare() { + try { + await this.initialize(); - const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); - if (query !== null) { - this.query.value = window.wanakana.toKana(query); - this.onSearchQueryUpdated(query, false); + if (this.search !== null) { + this.search.addEventListener('click', (e) => this.onSearch(e), false); } + if (this.query !== null) { + this.query.addEventListener('input', () => this.onSearchInput(), false); - window.wanakana.bind(this.query); - } + const query = DisplaySearch.getSearchQueryFromLocation(window.location.href); + if (query !== null) { + this.query.value = window.wanakana.toKana(query); + this.onSearchQueryUpdated(query, false); + } - this.updateSearchButton(); + window.wanakana.bind(this.query); + } + + this.updateSearchButton(); + } catch (e) { + this.onError(e); + } } onError(error) { @@ -89,7 +101,11 @@ class DisplaySearch extends Display { this.updateSearchButton(); if (valid) { const {definitions} = await apiTermsFind(query, this.optionsContext); - this.termsShow(definitions, await apiOptionsGet(this.optionsContext)); + this.setContentTerms(definitions, { + focus: false, + sentence: null, + url: window.location.href + }); } else { this.container.textContent = ''; } @@ -98,6 +114,25 @@ class DisplaySearch extends Display { } } + onRuntimeMessage({action, params}, sender, callback) { + const handlers = DisplaySearch.runtimeMessageHandlers; + if (handlers.hasOwnProperty(action)) { + const handler = handlers[action]; + const result = handler(this, params); + callback(result); + } else { + return super.onRuntimeMessage({action, params}, sender, callback); + } + } + + getOptionsContext() { + return this.optionsContext; + } + + setCustomCss() { + // No custom CSS + } + setIntroVisible(visible, animate) { if (this.introVisible === visible) { return; @@ -164,4 +199,10 @@ class DisplaySearch extends Display { } } -window.yomichan_search = new DisplaySearch(); +DisplaySearch.runtimeMessageHandlers = { + getUrl: () => { + return {url: window.location.href}; + } +}; + +window.yomichan_search = DisplaySearch.create(); |