From c45e991c3ee9c25714cf9696451d894e59caae97 Mon Sep 17 00:00:00 2001 From: purocean Date: Thu, 10 Oct 2024 01:49:16 +0800 Subject: [PATCH] refactor: update setWindowOpenHandler in app.ts to handle additional features --- src/main/app.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/app.ts b/src/main/app.ts index 2a5e0b7f..a8f23f67 100644 --- a/src/main/app.ts +++ b/src/main/app.ts @@ -634,7 +634,7 @@ if (!gotTheLock) { }) }) - webContents.setWindowOpenHandler(({ url }) => { + webContents.setWindowOpenHandler(({ url, features }) => { if (url.includes('__allow-open-window__')) { return { action: 'allow' } } @@ -652,7 +652,30 @@ if (!gotTheLock) { return { action: 'deny' } } - return { action: 'allow' } + const webPreferences: Record = {} + + // electron not auto parse features below. https://www.electronjs.org/docs/latest/api/window-open + const extraFeatureKeys = [ + 'experimentalFeatures', + 'nodeIntegrationInSubFrames', + 'webSecurity', + ] + + extraFeatureKeys.forEach(key => { + const match = features.match(new RegExp(`${key}=([^,]+)`)) + if (match) { + webPreferences[key] = match[1] === 'true' ? true : match[1] === 'false' ? false : match[1] + } + }) + + debugger + + return { + action: 'allow', + overrideBrowserWindowOptions: { + webPreferences: Object.keys(webPreferences).length > 0 ? webPreferences : undefined, + } + } }) }) }