diff options
Diffstat (limited to 'ext/fg/js/popup.js')
| -rw-r--r-- | ext/fg/js/popup.js | 87 | 
1 files changed, 36 insertions, 51 deletions
| diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index d752812e..60dc16dd 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -22,11 +22,10 @@   */  class Popup { -    constructor(id, depth, frameIdPromise) { +    constructor(id, depth, frameId) {          this._id = id;          this._depth = depth; -        this._frameIdPromise = frameIdPromise; -        this._frameId = null; +        this._frameId = frameId;          this._parent = null;          this._child = null;          this._childrenSupported = true; @@ -69,6 +68,10 @@ class Popup {          return this._depth;      } +    get frameId() { +        return this._frameId; +    } +      get url() {          return window.location.href;      } @@ -193,43 +196,42 @@ class Popup {      }      async _createInjectPromise() { -        try { -            const {frameId} = await this._frameIdPromise; -            if (typeof frameId === 'number') { -                this._frameId = frameId; -            } -        } catch (e) { -            // NOP -        } -          if (this._messageToken === null) {              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', () => { -                const uniqueId = yomichan.generateId(32); -                Popup._listenForDisplayPrepareCompleted(uniqueId, resolve); - -                this._invokeApi('prepare', { -                    options: this._options, -                    popupInfo: { -                        id: this._id, -                        depth: this._depth, -                        parentFrameId -                    }, -                    url: this.url, -                    childrenSupported: this._childrenSupported, -                    scale: this._contentScale, -                    uniqueId -                }); +        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() { @@ -374,23 +376,6 @@ 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; |