From 12e8fa90e07cf44af182b2d4cc3c073aa81638da Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Sun, 11 Dec 2022 13:35:28 +0000 Subject: [PATCH] gluon: wait for page load before dispatching IPC messages --- gluon/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gluon/index.js b/gluon/index.js index 54839cd..ab0216c 100644 --- a/gluon/index.js +++ b/gluon/index.js @@ -88,7 +88,7 @@ const startChromium = async (url, { windowSize }) => { // todo: move this to it's own library const { 3: pipeWrite, 4: pipeRead } = proc.stdio; - let onReply = {}; + let onReply = {}, pageLoadCallback = () => {}; const onMessage = msg => { msg = JSON.parse(msg); @@ -99,6 +99,7 @@ const startChromium = async (url, { windowSize }) => { } if (msg.method === 'Runtime.bindingCalled' && msg.name === 'gluonSend') onWindowMessage(JSON.parse(msg.payload)); + if (msg.method === 'Page.frameStoppedLoading') pageLoadCallback(msg.params); }; let msgId = 0; @@ -266,10 +267,15 @@ delete window._gluonSend; source: windowInjectionSource }, sessionId); + const pageLoadPromise = new Promise(res => { + pageLoadCallback = res; + }); + let onIPCReply = {}, ipcListeners = {}; const sendToWindow = async (type, data, id = undefined) => { id = id ?? Math.random().toString().split('.')[1]; + await pageLoadPromise; // wait for page to load before sending, otherwise messages won't be heard evalInWindow(`window.Gluon.ipc._recieve(${JSON.stringify({ id, type,