aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/search.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-06-21 16:14:05 -0400
committerGitHub <noreply@github.com>2020-06-21 16:14:05 -0400
commitf2991fb9ee8e83738b726eb558af992f4bb5d9dc (patch)
tree6323a3ec9549131a6ef19e16595fd08fb5c31b9f /ext/bg/js/search.js
parent244ab31bb2edb53ff7aecb51d2dd60b50a24c194 (diff)
Frontend initialization refactor (#610)
* Create member functions for ignoreElements and ignorePoint * Create addFullscreenChangeEventListener utility * Move popup creation management into Frontend * Move getUrl implementation * Remove old setup * Remove try/catch block * Error wrap * Add prepare call to TextScanner * Update depth when popup changes * Refactor how Frontend gets PopupFactory and frameId * Update popup preview to work * Update popup preview frame to use the frontend's popup * Update how nested popups are set up * Error wrap * Update how popups are set up on the search page * Error wrap * Error unwrap * Add missing prepare * Remove use of frontendInitializationData * Catch and log errors
Diffstat (limited to 'ext/bg/js/search.js')
-rw-r--r--ext/bg/js/search.js120
1 files changed, 85 insertions, 35 deletions
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index 08c02624..88be335f 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -19,8 +19,11 @@
* ClipboardMonitor
* DOM
* Display
+ * Frontend
+ * PopupFactory
* QueryParser
* api
+ * dynamicLoader
* wanakana
*/
@@ -73,51 +76,49 @@ class DisplaySearch extends Display {
}
async prepare() {
- try {
- await super.prepare();
- await this.updateOptions();
- yomichan.on('optionsUpdated', () => this.updateOptions());
- await this.queryParser.prepare();
+ await super.prepare();
+ await this.updateOptions();
+ yomichan.on('optionsUpdated', () => this.updateOptions());
+ await this.queryParser.prepare();
+
+ const {queryParams: {query='', mode=''}} = parseUrl(window.location.href);
+
+ document.documentElement.dataset.searchMode = mode;
- const {queryParams: {query='', mode=''}} = parseUrl(window.location.href);
+ if (this.options.general.enableWanakana === true) {
+ this.wanakanaEnable.checked = true;
+ wanakana.bind(this.query);
+ } else {
+ this.wanakanaEnable.checked = false;
+ }
- document.documentElement.dataset.searchMode = mode;
+ this.setQuery(query);
+ this.onSearchQueryUpdated(this.query.value, false);
- if (this.options.general.enableWanakana === true) {
- this.wanakanaEnable.checked = true;
- wanakana.bind(this.query);
+ if (mode !== 'popup') {
+ if (this.options.general.enableClipboardMonitor === true) {
+ this.clipboardMonitorEnable.checked = true;
+ this.clipboardMonitor.start();
} else {
- this.wanakanaEnable.checked = false;
+ this.clipboardMonitorEnable.checked = false;
}
+ this.clipboardMonitorEnable.addEventListener('change', this.onClipboardMonitorEnableChange.bind(this));
+ }
- this.setQuery(query);
- this.onSearchQueryUpdated(this.query.value, false);
-
- if (mode !== 'popup') {
- if (this.options.general.enableClipboardMonitor === true) {
- this.clipboardMonitorEnable.checked = true;
- this.clipboardMonitor.start();
- } else {
- this.clipboardMonitorEnable.checked = false;
- }
- this.clipboardMonitorEnable.addEventListener('change', this.onClipboardMonitorEnableChange.bind(this));
- }
+ chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
- chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
+ this.search.addEventListener('click', this.onSearch.bind(this), false);
+ this.query.addEventListener('input', this.onSearchInput.bind(this), false);
+ this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this));
+ window.addEventListener('popstate', this.onPopState.bind(this));
+ window.addEventListener('copy', this.onCopy.bind(this));
+ this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this));
- this.search.addEventListener('click', this.onSearch.bind(this), false);
- this.query.addEventListener('input', this.onSearchInput.bind(this), false);
- this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this));
- window.addEventListener('popstate', this.onPopState.bind(this));
- window.addEventListener('copy', this.onCopy.bind(this));
- this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this));
+ this.updateSearchButton();
- this.updateSearchButton();
+ await this._prepareNestedPopups();
- this._isPrepared = true;
- } catch (e) {
- this.onError(e);
- }
+ this._isPrepared = true;
}
onError(error) {
@@ -401,4 +402,53 @@ class DisplaySearch extends Display {
document.title = `${text} - Yomichan Search`;
}
}
+
+ async _prepareNestedPopups() {
+ let complete = false;
+
+ const onOptionsUpdated = async () => {
+ const optionsContext = this.getOptionsContext();
+ const options = await api.optionsGet(optionsContext);
+ if (!options.scanning.enableOnSearchPage || complete) { return; }
+
+ complete = true;
+ yomichan.off('optionsUpdated', onOptionsUpdated);
+
+ try {
+ await this._setupNestedPopups();
+ } catch (e) {
+ yomichan.logError(e);
+ }
+ };
+
+ yomichan.on('optionsUpdated', onOptionsUpdated);
+
+ await onOptionsUpdated();
+ }
+
+ async _setupNestedPopups() {
+ await dynamicLoader.loadScripts([
+ '/mixed/js/text-scanner.js',
+ '/fg/js/frame-offset-forwarder.js',
+ '/fg/js/popup.js',
+ '/fg/js/popup-factory.js',
+ '/fg/js/frontend.js'
+ ]);
+
+ const {frameId} = await api.frameInformationGet();
+
+ const popupFactory = new PopupFactory(frameId);
+ await popupFactory.prepare();
+
+ const frontend = new Frontend(
+ frameId,
+ popupFactory,
+ {
+ depth: 1,
+ proxy: false,
+ isSearchPage: true
+ }
+ );
+ await frontend.prepare();
+ }
}