refactor: update setWindowOpenHandler in app.ts to handle additional features

This commit is contained in:
purocean
2024-10-10 01:49:16 +08:00
parent e5e95599b1
commit c45e991c3e
+25 -2
View File
@@ -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<string, boolean | string> = {}
// 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,
}
}
})
})
}