diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-07-03 12:20:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-03 12:20:22 -0400 |
commit | a07a8dfff667e0bba20d7199c4d7aa610e98bcdb (patch) | |
tree | 0621aa42db97b7ac39207452b54c10b528080f24 | |
parent | ce634325ce3a6ba4994916116b15b90e92666255 (diff) |
Extension unload detection (#647)
* Update how extension unload is detected
* Remove event handlers and use yomichan.isExtensionUnloaded instead
* Update terminology
-rw-r--r-- | ext/fg/float.html | 2 | ||||
-rw-r--r-- | ext/fg/js/frontend.js | 10 | ||||
-rw-r--r-- | ext/mixed/js/api.js | 3 | ||||
-rw-r--r-- | ext/mixed/js/comm.js | 2 | ||||
-rw-r--r-- | ext/mixed/js/display.js | 22 | ||||
-rw-r--r-- | ext/mixed/js/yomichan.js | 35 |
6 files changed, 43 insertions, 31 deletions
diff --git a/ext/fg/float.html b/ext/fg/float.html index 735a880a..a13244ee 100644 --- a/ext/fg/float.html +++ b/ext/fg/float.html @@ -29,7 +29,7 @@ </div> </div> - <div id="error-orphaned" hidden> + <div id="error-extension-unloaded" hidden> <div class="entry"> <h1>Yomichan Updated!</h1> <p> diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index f6b0d236..33cb0b90 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -32,7 +32,6 @@ class Frontend { this._options = null; this._pageZoomFactor = 1.0; this._contentScale = 1.0; - this._orphaned = false; this._lastShowPromise = Promise.resolve(); this._enabledEventListeners = new EventListenerCollection(); this._activeModifiers = new Set(); @@ -110,7 +109,6 @@ class Frontend { visualViewport.addEventListener('resize', this._onVisualViewportResize.bind(this)); } - yomichan.on('orphaned', this._onOrphaned.bind(this)); yomichan.on('optionsUpdated', this.updateOptions.bind(this)); yomichan.on('zoomChanged', this._onZoomChanged.bind(this)); chrome.runtime.onMessage.addListener(this._onRuntimeMessage.bind(this)); @@ -230,10 +228,6 @@ class Frontend { return false; } - _onOrphaned() { - this._orphaned = true; - } - _onZoomChanged({newZoomFactor}) { this._pageZoomFactor = newZoomFactor; this._updateContentScale(); @@ -370,9 +364,9 @@ class Frontend { } } } catch (e) { - if (this._orphaned) { + if (yomichan.isExtensionUnloaded) { if (textSource !== null && this._options.scanning.modifier !== 'none') { - this._showPopupContent(textSource, await this.getOptionsContext(), 'orphaned'); + this._showPopupContent(textSource, await this.getOptionsContext(), 'extensionUnloaded'); } } else { yomichan.logError(e); diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 1e421147..534154ef 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -322,7 +322,7 @@ const api = (() => { const data = {action, params}; return new Promise((resolve, reject) => { try { - chrome.runtime.sendMessage(data, (response) => { + yomichan.sendMessage(data, (response) => { this._checkLastError(chrome.runtime.lastError); if (response !== null && typeof response === 'object') { if (typeof response.error !== 'undefined') { @@ -337,7 +337,6 @@ const api = (() => { }); } catch (e) { reject(e); - yomichan.triggerOrphaned(e); } }); } diff --git a/ext/mixed/js/comm.js b/ext/mixed/js/comm.js index 7787616e..0d6b8695 100644 --- a/ext/mixed/js/comm.js +++ b/ext/mixed/js/comm.js @@ -268,7 +268,7 @@ class CrossFrameAPI { } _createCommPort(otherFrameId) { - const port = chrome.runtime.connect(null, {name: `background-cross-frame-communication-port-${otherFrameId}`}); + const port = yomichan.connect(null, {name: `background-cross-frame-communication-port-${otherFrameId}`}); return this._setupCommPort(otherFrameId, port); } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index c8c574f4..f02a6e5c 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -47,7 +47,6 @@ class Display { useCache: true }); this._styleNode = null; - this._orphaned = false; this._eventListeners = new EventListenerCollection(); this._persistentEventListeners = new EventListenerCollection(); @@ -169,16 +168,11 @@ class Display { this._setInteractive(true); await yomichan.ready(); await this._displayGenerator.prepare(); - yomichan.on('orphaned', this._onOrphaned.bind(this)); - } - - _onOrphaned() { - this._orphaned = true; } onError(error) { - if (this._orphaned) { - this.setContent('orphaned'); + if (yomichan.isExtensionUnloaded) { + this.setContent('extensionUnloaded'); } else { yomichan.logError(error); } @@ -494,8 +488,8 @@ class Display { case 'kanji': await this._setContentKanji(details.definitions, details.context, token); break; - case 'orphaned': - this._setContentOrphaned(); + case 'extensionUnloaded': + this._setContentExtensionUnloaded(); break; } } catch (e) { @@ -614,15 +608,15 @@ class Display { this._updateAdderButtons(states); } - _setContentOrphaned() { - const errorOrphaned = document.querySelector('#error-orphaned'); + _setContentExtensionUnloaded() { + const errorExtensionUnloaded = document.querySelector('#error-extension-unloaded'); if (this._container !== null) { this._container.hidden = true; } - if (errorOrphaned !== null) { - errorOrphaned.hidden = false; + if (errorExtensionUnloaded !== null) { + errorExtensionUnloaded.hidden = false; } this._updateNavigation(null, null); diff --git a/ext/mixed/js/yomichan.js b/ext/mixed/js/yomichan.js index 00335eba..d921a5a2 100644 --- a/ext/mixed/js/yomichan.js +++ b/ext/mixed/js/yomichan.js @@ -47,6 +47,8 @@ const yomichan = (() => { // NOP } + this._isExtensionUnloaded = false; + const {promise, resolve} = deferPromise(); this._isBackendPreparedPromise = promise; this._isBackendPreparedPromiseResolve = resolve; @@ -61,12 +63,16 @@ const yomichan = (() => { // Public + get isExtensionUnloaded() { + return this._isExtensionUnloaded; + } + prepare() { chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); } ready() { - chrome.runtime.sendMessage({action: 'yomichanCoreReady'}); + this.sendMessage({action: 'yomichanCoreReady'}); return this._isBackendPreparedPromise; } @@ -80,10 +86,6 @@ const yomichan = (() => { return id; } - triggerOrphaned(error) { - this.trigger('orphaned', {error}); - } - isExtensionUrl(url) { try { return url.startsWith(chrome.runtime.getURL('/')); @@ -190,8 +192,31 @@ const yomichan = (() => { this.trigger('log', {error, level, context}); } + sendMessage(...args) { + try { + return chrome.runtime.sendMessage(...args); + } catch (e) { + this._onExtensionUnloaded(e); + throw e; + } + } + + connect(...args) { + try { + return chrome.runtime.connect(...args); + } catch (e) { + this._onExtensionUnloaded(e); + throw e; + } + } + // Private + _onExtensionUnloaded(error) { + this._isExtensionUnloaded = true; + this.trigger('extensionUnloaded', {error}); + } + _getUrl() { return (typeof window === 'object' && window !== null ? window.location.href : ''); } |