From c967b7a9f13ab7c3e0fe47e83fe5f05debf3aa73 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 3 Oct 2019 20:22:12 -0400 Subject: Remove event listeners when scanning is disabled --- ext/fg/js/frontend.js | 73 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 22 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 58dc0e4a..78f94cd9 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -38,6 +38,9 @@ class Frontend { this.mouseDownPrevent = false; this.clickPrevent = false; this.scrollPrevent = false; + + this.enabled = false; + this.eventListeners = []; } static create() { @@ -53,23 +56,7 @@ class Frontend { async prepare() { try { - this.options = await apiOptionsGet(this.getOptionsContext()); - - window.addEventListener('message', this.onWindowMessage.bind(this)); - window.addEventListener('mousedown', this.onMouseDown.bind(this)); - window.addEventListener('mousemove', this.onMouseMove.bind(this)); - window.addEventListener('mouseover', this.onMouseOver.bind(this)); - window.addEventListener('mouseout', this.onMouseOut.bind(this)); - window.addEventListener('resize', this.onResize.bind(this)); - - if (this.options.scanning.touchInputEnabled) { - window.addEventListener('click', this.onClick.bind(this)); - window.addEventListener('touchstart', this.onTouchStart.bind(this)); - window.addEventListener('touchend', this.onTouchEnd.bind(this)); - window.addEventListener('touchcancel', this.onTouchCancel.bind(this)); - window.addEventListener('touchmove', this.onTouchMove.bind(this), {passive: false}); - window.addEventListener('contextmenu', this.onContextMenu.bind(this)); - } + await this.updateOptions(); chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this)); } catch (e) { @@ -88,7 +75,6 @@ class Frontend { if ( this.pendingLookup || - !this.options.general.enable || (e.buttons & 0x1) !== 0x0 // Left mouse button ) { return; @@ -245,13 +231,56 @@ class Frontend { console.log(error); } - async updateOptions() { - this.options = await apiOptionsGet(this.getOptionsContext()); - if (!this.options.enable) { + setEnabled(enabled) { + if (enabled) { + if (!this.enabled) { + this.hookEvents(); + this.enabled = true; + } + } else { + if (this.enabled) { + this.clearEventListeners(); + this.enabled = false; + } this.searchClear(false); } } + hookEvents() { + this.addEventListener(window, 'message', this.onWindowMessage.bind(this)); + this.addEventListener(window, 'mousedown', this.onMouseDown.bind(this)); + this.addEventListener(window, 'mousemove', this.onMouseMove.bind(this)); + this.addEventListener(window, 'mouseover', this.onMouseOver.bind(this)); + this.addEventListener(window, 'mouseout', this.onMouseOut.bind(this)); + this.addEventListener(window, 'resize', this.onResize.bind(this)); + + if (this.options.scanning.touchInputEnabled) { + this.addEventListener(window, 'click', this.onClick.bind(this)); + this.addEventListener(window, 'touchstart', this.onTouchStart.bind(this)); + this.addEventListener(window, 'touchend', this.onTouchEnd.bind(this)); + this.addEventListener(window, 'touchcancel', this.onTouchCancel.bind(this)); + this.addEventListener(window, 'touchmove', this.onTouchMove.bind(this), {passive: false}); + this.addEventListener(window, 'contextmenu', this.onContextMenu.bind(this)); + } + } + + addEventListener(node, type, listener, options) { + node.addEventListener(type, listener, options); + this.eventListeners.push([node, type, listener, options]); + } + + clearEventListeners() { + for (const [node, type, listener, options] of this.eventListeners) { + node.removeEventListener(type, listener, options); + } + this.eventListeners = []; + } + + async updateOptions() { + this.options = await apiOptionsGet(this.getOptionsContext()); + this.setEnabled(this.options.general.enable); + } + popupTimerSet(callback) { const delay = this.options.scanning.delay; if (delay > 0) { @@ -452,7 +481,7 @@ class Frontend { searchFromTouch(x, y, cause) { this.popupTimerClear(); - if (!this.options.general.enable || this.pendingLookup) { + if (this.pendingLookup) { return; } -- cgit v1.2.3 From 2255fadf52bff17d183f48e0bc72a078568481f2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 5 Oct 2019 21:38:13 -0400 Subject: Rename Popup.setVisible to setVisibleOverride --- ext/fg/js/frontend.js | 4 ++-- ext/fg/js/popup-proxy-host.js | 6 +++--- ext/fg/js/popup-proxy.js | 4 ++-- ext/fg/js/popup.js | 2 +- ext/mixed/js/display.js | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 58dc0e4a..3fa70357 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -527,8 +527,8 @@ Frontend.runtimeMessageHandlers = { self.updateOptions(); }, - popupSetVisible: (self, {visible}) => { - self.popup.setVisible(visible); + popupSetVisibleOverride: (self, {visible}) => { + self.popup.setVisibleOverride(visible); } }; diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js index cb9741be..7fad71c1 100644 --- a/ext/fg/js/popup-proxy-host.js +++ b/ext/fg/js/popup-proxy-host.js @@ -41,7 +41,7 @@ class PopupProxyHost { show: ({id, elementRect, options}) => this.show(id, elementRect, options), showOrphaned: ({id, elementRect, options}) => this.show(id, elementRect, options), hide: ({id, changeFocus}) => this.hide(id, changeFocus), - setVisible: ({id, visible}) => this.setVisible(id, visible), + setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible), containsPoint: ({id, x, y}) => this.containsPoint(id, x, y), termsShow: ({id, elementRect, writingMode, definitions, options, context}) => this.termsShow(id, elementRect, writingMode, definitions, options, context), kanjiShow: ({id, elementRect, writingMode, definitions, options, context}) => this.kanjiShow(id, elementRect, writingMode, definitions, options, context), @@ -103,9 +103,9 @@ class PopupProxyHost { return popup.hide(changeFocus); } - async setVisible(id, visible) { + async setVisibleOverride(id, visible) { const popup = this.getPopup(id); - return popup.setVisible(visible); + return popup.setVisibleOverride(visible); } async containsPoint(id, x, y) { diff --git a/ext/fg/js/popup-proxy.js b/ext/fg/js/popup-proxy.js index 072cebc9..99b28549 100644 --- a/ext/fg/js/popup-proxy.js +++ b/ext/fg/js/popup-proxy.js @@ -65,9 +65,9 @@ class PopupProxy { return await this.invokeHostApi('hide', {id: this.id, changeFocus}); } - async setVisible(visible) { + async setVisibleOverride(visible) { const id = await this.getPopupId(); - return await this.invokeHostApi('setVisible', {id, visible}); + return await this.invokeHostApi('setVisibleOverride', {id, visible}); } async containsPoint(x, y) { diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 9dff6f28..5509c98c 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -238,7 +238,7 @@ class Popup { return this.isInjected && this.container.style.visibility !== 'hidden'; } - setVisible(visible) { + setVisibleOverride(visible) { if (visible) { this.container.style.setProperty('display', ''); } else { diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index dc64dbea..b70fa4b8 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -445,7 +445,7 @@ class Display { async getScreenshot() { try { - await this.setPopupVisible(false); + await this.setPopupVisibleOverride(false); await Display.delay(1); // Wait for popup to be hidden. const {format, quality} = this.options.anki.screenshot; @@ -454,7 +454,7 @@ class Display { return {dataUrl, format}; } finally { - await this.setPopupVisible(true); + await this.setPopupVisibleOverride(true); } } @@ -462,8 +462,8 @@ class Display { return this.options.general.resultOutputMode === 'merge' ? 0 : -1; } - setPopupVisible(visible) { - return apiForward('popupSetVisible', {visible}); + setPopupVisibleOverride(visible) { + return apiForward('popupSetVisibleOverride', {visible}); } setSpinnerVisible(visible) { -- cgit v1.2.3 From 113cc725c1764e5eb814a22ace23a7e7150d016f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 5 Oct 2019 22:11:05 -0400 Subject: Make the window.onresize handler not change focus --- ext/fg/js/frontend.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 3fa70357..4ad78aa7 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -145,7 +145,7 @@ class Frontend { } onResize() { - this.searchClear(true); + this.searchClear(false); } onClick(e) { -- cgit v1.2.3 From c5d6b9452d1bfa51a5fef9a19082d115ef180a9b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 8 Oct 2019 21:33:10 -0400 Subject: Create utility function for logging errors --- ext/bg/js/search.js | 2 +- ext/fg/js/float.js | 2 +- ext/fg/js/frontend.js | 2 +- ext/mixed/js/extension.js | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) (limited to 'ext/fg/js/frontend.js') diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index 52adab16..ead9ba6f 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -53,7 +53,7 @@ class DisplaySearch extends Display { } onError(error) { - window.alert(`Error: ${error.toString ? error.toString() : error}`); + logError(error, true); } onSearchClear() { diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js index 88842eef..8fdb6925 100644 --- a/ext/fg/js/float.js +++ b/ext/fg/js/float.js @@ -37,7 +37,7 @@ class DisplayFloat extends Display { if (window.yomichan_orphaned) { this.onOrphaned(); } else { - window.alert(`Error: ${error.toString ? error.toString() : error}`); + logError(error, true); } } diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 39433afb..88cb93a9 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -228,7 +228,7 @@ class Frontend { } onError(error) { - console.log(error); + logError(error, false); } setEnabled(enabled) { diff --git a/ext/mixed/js/extension.js b/ext/mixed/js/extension.js index 5e925124..861e52a5 100644 --- a/ext/mixed/js/extension.js +++ b/ext/mixed/js/extension.js @@ -68,6 +68,24 @@ function jsonToError(jsonError) { return error; } +function logError(error, alert) { + const manifest = chrome.runtime.getManifest(); + let errorMessage = `${manifest.name} v${manifest.version} has encountered an error.\n`; + errorMessage += `Originating URL: ${window.location.href}\n`; + + const errorString = `${error.toString ? error.toString() : error}`; + const stack = `${error.stack}`.trimRight(); + errorMessage += (!stack.startsWith(errorString) ? `${errorString}\n${stack}` : `${stack}`); + + errorMessage += '\n\nIssues can be reported at https://github.com/FooSoft/yomichan/issues'; + + console.error(errorMessage); + + if (alert) { + window.alert(`${errorString}\n\nCheck the developer console for more details.`); + } +} + const EXTENSION_IS_BROWSER_EDGE = ( extensionHasBrowser() && (!extensionHasChrome() || (typeof chrome.runtime === 'undefined' && typeof browser.runtime !== 'undefined')) -- cgit v1.2.3