extract different popup creation functions

This commit is contained in:
siikamiika 2020-04-10 15:00:23 +03:00
parent 92109bb5d2
commit 7dd2610ce8

View File

@ -24,6 +24,47 @@
* apiOptionsGet * apiOptionsGet
*/ */
async function createIframePopupProxy(url) {
const rootPopupInformationPromise = yomichan.getTemporaryListenerResult(
chrome.runtime.onMessage,
({action, params}, {resolve}) => {
if (action === 'rootPopupInformation') {
resolve(params);
}
}
);
apiBroadcastTab('rootPopupRequestInformationBroadcast');
const {popupId, frameId} = await rootPopupInformationPromise;
const frameOffsetForwarder = new FrameOffsetForwarder();
frameOffsetForwarder.start();
const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder);
const popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset);
await popup.prepare();
return popup;
}
async function getOrCreatePopup(depth) {
const frameOffsetForwarder = new FrameOffsetForwarder();
frameOffsetForwarder.start();
const popupHost = new PopupProxyHost();
await popupHost.prepare();
const popup = popupHost.getOrCreatePopup(null, null, depth);
return popup;
}
async function createPopupProxy(depth, id, parentFrameId, url) {
const popup = new PopupProxy(null, depth + 1, id, parentFrameId, url);
await popup.prepare();
return popup;
}
async function main() { async function main() {
await yomichan.prepare(); await yomichan.prepare();
@ -46,34 +87,11 @@ async function main() {
let popup; let popup;
if (!proxy && (window !== window.parent) && options.general.showIframePopupsInRootFrame) { if (!proxy && (window !== window.parent) && options.general.showIframePopupsInRootFrame) {
const rootPopupInformationPromise = yomichan.getTemporaryListenerResult( popup = await createIframePopupProxy(url);
chrome.runtime.onMessage,
({action, params}, {resolve}) => {
if (action === 'rootPopupInformation') {
resolve(params);
}
}
);
apiBroadcastTab('rootPopupRequestInformationBroadcast');
const {popupId, frameId} = await rootPopupInformationPromise;
const frameOffsetForwarder = new FrameOffsetForwarder();
frameOffsetForwarder.start();
const getFrameOffset = frameOffsetForwarder.getOffset.bind(frameOffsetForwarder);
popup = new PopupProxy(popupId, 0, null, frameId, url, getFrameOffset);
await popup.prepare();
} else if (proxy) { } else if (proxy) {
popup = new PopupProxy(null, depth + 1, id, parentFrameId, url); popup = await createPopupProxy(depth, id, parentFrameId, url);
await popup.prepare();
} else { } else {
const frameOffsetForwarder = new FrameOffsetForwarder(); popup = await getOrCreatePopup(depth);
frameOffsetForwarder.start();
const popupHost = new PopupProxyHost();
await popupHost.prepare();
popup = popupHost.getOrCreatePopup(null, null, depth);
} }
const frontend = new Frontend(popup, initEventDispatcher); const frontend = new Frontend(popup, initEventDispatcher);