diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-16 12:23:20 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-02-16 12:23:20 -0500 |
commit | b5d32c73e657c882ba6f70b79d4a5e214684f563 (patch) | |
tree | 64e63413e8307a43ef65b318dec88a3a76e4cd3f /ext/fg/js/popup.js | |
parent | 42f1c2463c8051d9cbbcacd43f06922c2f11ec71 (diff) |
Simplify process to wait for iframe prepare completion
Diffstat (limited to 'ext/fg/js/popup.js')
-rw-r--r-- | ext/fg/js/popup.js | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index 55f3e0aa..5f6a777b 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -123,10 +123,6 @@ class Popup { this._invokeApi('setContentScale', {scale}); } - setDisplayInitialized() { - throw new Error('Override me'); - } - // Popup-only public functions setParent(parent) { @@ -223,7 +219,10 @@ class Popup { return new Promise((resolve) => { const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); this._container.addEventListener('load', () => { - this._invokeApi('initialize', { + const uniqueId = yomichan.generateId(32); + Popup._listenForDisplayPrepareCompleted(uniqueId, resolve); + + this._invokeApi('prepare', { options: this._options, popupInfo: { id: this._id, @@ -232,9 +231,9 @@ class Popup { }, url: this.url, childrenSupported: this._childrenSupported, - scale: this._contentScale + scale: this._contentScale, + uniqueId }); - this.setDisplayInitialized = resolve; }); this._observeFullscreen(); this._onFullscreenChanged(); @@ -357,6 +356,23 @@ class Popup { } } + static _listenForDisplayPrepareCompleted(uniqueId, resolve) { + const runtimeMessageCallback = ({action, params}, sender, callback) => { + if ( + action === 'popupPrepareCompleted' && + typeof params === 'object' && + params !== null && + params.uniqueId === uniqueId + ) { + chrome.runtime.onMessage.removeListener(runtimeMessageCallback); + callback(); + resolve(); + return false; + } + }; + chrome.runtime.onMessage.addListener(runtimeMessageCallback); + } + static _getPositionForHorizontalText(elementRect, width, height, viewport, offsetScale, optionsGeneral) { const preferBelow = (optionsGeneral.popupHorizontalTextPosition === 'below'); const horizontalOffset = optionsGeneral.popupHorizontalOffset * offsetScale; |