From fc9150191824e9261997c7213e14da02cd560a90 Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Tue, 13 Dec 2022 18:50:32 +0000 Subject: [PATCH] gluon: inject IPC on new execution contexts to improve reliability --- gluon/browser/chromium.js | 3 ++- gluon/browser/firefox.js | 2 +- gluon/browser/ipc.js | 5 ++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gluon/browser/chromium.js b/gluon/browser/chromium.js index 3a5ae36..117f348 100644 --- a/gluon/browser/chromium.js +++ b/gluon/browser/chromium.js @@ -39,6 +39,7 @@ export default async ({ browserName, browserPath, dataPath }, { url, windowSize if (msg.method === 'Runtime.bindingCalled' && msg.name === 'gluonSend') onWindowMessage(JSON.parse(msg.payload)); if (msg.method === 'Page.frameStoppedLoading') pageLoadCallback(msg.params); + if (msg.method === 'Runtime.executionContextCreated') injectIPC(); // ensure IPC injection again }; let msgId = 0; @@ -117,7 +118,7 @@ export default async ({ browserName, browserPath, dataPath }, { url, windowSize }; - const [ ipcMessageCallback, IPCApi ] = await makeIPCApi({ + const [ ipcMessageCallback, injectIPC, IPCApi ] = await makeIPCApi({ browserName, browserInfo }, { diff --git a/gluon/browser/firefox.js b/gluon/browser/firefox.js index 09b72d1..7ca28d7 100644 --- a/gluon/browser/firefox.js +++ b/gluon/browser/firefox.js @@ -111,7 +111,7 @@ user_pref('privacy.window.maxInnerHeight', ${windowSize[1]}); */ name: '_gluonSend' }); */ - const [ ipcMessageCallback, IPCApi ] = await makeIPCApi({ + const [ ipcMessageCallback, injectIPC, IPCApi ] = await makeIPCApi({ browserName, browserInfo }, { diff --git a/gluon/browser/ipc.js b/gluon/browser/ipc.js index 48925d3..105e267 100644 --- a/gluon/browser/ipc.js +++ b/gluon/browser/ipc.js @@ -1,5 +1,6 @@ export default ({ browserName, browserInfo }, { evaluate, addScriptToEvaluateOnNewDocument, pageLoadPromise }) => { const injection = `(() => { +if (window.Gluon) return; let onIPCReply = {}, ipcListeners = {}; window.Gluon = { versions: { @@ -129,7 +130,9 @@ delete window._gluonSend; sendToWindow('pong', {}, id); // send simple pong to confirm }; - return [ onWindowMessage, { + return [ onWindowMessage, () => evaluate({ + expression: injection + }), { on: (type, cb) => { if (!ipcListeners[type]) ipcListeners[type] = []; ipcListeners[type].push(cb);