use getTemporaryListenerResult in other places

This commit is contained in:
siikamiika 2020-03-25 04:17:17 +02:00
parent 7928c5d713
commit c171503d77
2 changed files with 37 additions and 40 deletions

View File

@ -203,27 +203,38 @@ class Popup {
this._messageToken = await apiGetMessageToken(); this._messageToken = await apiGetMessageToken();
} }
return new Promise((resolve) => { const popupPreparedPromise = yomichan.getTemporaryListenerResult(
const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null); chrome.runtime.onMessage,
this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html')); ({action, params}, {resolve}) => {
this._container.addEventListener('load', () => { if (
this._listenForDisplayPrepareCompleted(resolve); action === 'popupPrepareCompleted' &&
isObject(params) &&
params.targetPopupId === this._id
) {
resolve();
}
}
);
this._invokeApi('prepare', { const parentFrameId = (typeof this._frameId === 'number' ? this._frameId : null);
popupInfo: { this._container.setAttribute('src', chrome.runtime.getURL('/fg/float.html'));
id: this._id, this._container.addEventListener('load', () => {
depth: this._depth, this._invokeApi('prepare', {
parentFrameId popupInfo: {
}, id: this._id,
url: this.url, depth: this._depth,
childrenSupported: this._childrenSupported, parentFrameId
scale: this._contentScale },
}); 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() { async _injectStyles() {
@ -358,21 +369,6 @@ class Popup {
contentWindow.postMessage({action, params, token}, this._targetOrigin); 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() { static _getFullscreenElement() {
return ( return (
document.fullscreenElement || document.fullscreenElement ||

View File

@ -278,11 +278,16 @@ const yomichan = (() => {
constructor() { constructor() {
super(); super();
this._isBackendPreparedResolve = null; this._isBackendPreparedPromise = this.getTemporaryListenerResult(
this._isBackendPreparedPromise = new Promise((resolve) => (this._isBackendPreparedResolve = resolve)); chrome.runtime.onMessage,
({action}, {resolve}) => {
if (action === 'backendPrepared') {
resolve();
}
}
);
this._messageHandlers = new Map([ this._messageHandlers = new Map([
['backendPrepared', this._onBackendPrepared.bind(this)],
['getUrl', this._onMessageGetUrl.bind(this)], ['getUrl', this._onMessageGetUrl.bind(this)],
['optionsUpdated', this._onMessageOptionsUpdated.bind(this)], ['optionsUpdated', this._onMessageOptionsUpdated.bind(this)],
['zoomChanged', this._onMessageZoomChanged.bind(this)] ['zoomChanged', this._onMessageZoomChanged.bind(this)]
@ -362,10 +367,6 @@ const yomichan = (() => {
return false; return false;
} }
_onBackendPrepared() {
this._isBackendPreparedResolve();
}
_onMessageGetUrl() { _onMessageGetUrl() {
return {url: window.location.href}; return {url: window.location.href};
} }