diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-25 04:17:17 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-04-05 19:57:37 +0300 |
commit | c171503d77bbae21ddfa2eb49c6a571440f41763 (patch) | |
tree | 00d9f928505c6d26b0fc4167caee3696273949cf /ext | |
parent | 7928c5d71324b3eebece7e2a26e9783e139066d5 (diff) |
use getTemporaryListenerResult in other places
Diffstat (limited to 'ext')
-rw-r--r-- | ext/fg/js/popup.js | 64 | ||||
-rw-r--r-- | ext/mixed/js/core.js | 15 |
2 files changed, 38 insertions, 41 deletions
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 47e32963..fb3e56b4 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -203,27 +203,38 @@ class Popup { this._messageToken = await apiGetMessageToken(); } - return new Promise((resolve) => { - const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); - this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); - this._container.addEventListener('load', () => { - this._listenForDisplayPrepareCompleted(resolve); - - this._invokeApi('prepare', { - popupInfo: { - id: this._id, - depth: this._depth, - parentFrameId - }, - url: this.url, - childrenSupported: this._childrenSupported, - scale: this._contentScale - }); + const popupPreparedPromise = yomichan.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action, params}, {resolve}) => { + if ( + action === 'popupPrepareCompleted' && + isObject(params) && + params.targetPopupId === this._id + ) { + resolve(); + } + } + ); + + const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); + this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); + this._container.addEventListener('load', () => { + this._invokeApi('prepare', { + popupInfo: { + id: this._id, + depth: this._depth, + parentFrameId + }, + url: this.url, + childrenSupported: this._childrenSupported, + scale: this._contentScale }); - this._observeFullscreen(true); - this._onFullscreenChanged(); - this._injectStyles(); }); + this._observeFullscreen(true); + this._onFullscreenChanged(); + this._injectStyles(); + + return popupPreparedPromise; } async _injectStyles() { @@ -358,21 +369,6 @@ class Popup { contentWindow.postMessage({action, params, token}, this._targetOrigin); } - _listenForDisplayPrepareCompleted(resolve) { - const runtimeMessageCallback = ({action, params}, sender, callback) => { - if ( - action === 'popupPrepareCompleted' && - isObject(params) && - params.targetPopupId === this._id - ) { - chrome.runtime.onMessage.removeListener(runtimeMessageCallback); - callback(); - resolve(); - } - }; - chrome.runtime.onMessage.addListener(runtimeMessageCallback); - } - static _getFullscreenElement() { return ( document.fullscreenElement || diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 616f48b8..ca37a26a 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -278,11 +278,16 @@ const yomichan = (() => { constructor() { super(); - this._isBackendPreparedResolve = null; - this._isBackendPreparedPromise = new Promise((resolve) => (this._isBackendPreparedResolve = resolve)); + this._isBackendPreparedPromise = this.getTemporaryListenerResult( + chrome.runtime.onMessage, + ({action}, {resolve}) => { + if (action === 'backendPrepared') { + resolve(); + } + } + ); this._messageHandlers = new Map([ - ['backendPrepared', this._onBackendPrepared.bind(this)], ['getUrl', this._onMessageGetUrl.bind(this)], ['optionsUpdated', this._onMessageOptionsUpdated.bind(this)], ['zoomChanged', this._onMessageZoomChanged.bind(this)] @@ -362,10 +367,6 @@ const yomichan = (() => { return false; } - _onBackendPrepared() { - this._isBackendPreparedResolve(); - } - _onMessageGetUrl() { return {url: window.location.href}; } |