main: add context bridge communication example via preload script, set nodeIntegration to false by default, improve security
This commit is contained in:
1 parent
a540caf9d3
commit
8057a3521a
9 files changed
+64
-12
No files matched your search
+18
-4
@@ -1,5 +1,19 @@
|
||||
const main = (): void => {
|
||||
console.log('[Preload] Script loaded.')
|
||||
}
|
||||
import { contextBridge, ipcRenderer, shell } from 'electron'
|
||||
|
||||
main()
|
||||
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
|
||||
const mainAvailChannels = ['msgRequestGetVersion']
|
||||
const rendererAvailChannels = ['msgReceivedVersion']
|
||||
|
||||
contextBridge.exposeInMainWorld('mainApi', {
|
||||
openExternal: async (url) => await shell.openExternal(url),
|
||||
send: (channel, data) => {
|
||||
if (mainAvailChannels.includes(channel)) {
|
||||
ipcRenderer.send(channel, data)
|
||||
}
|
||||
},
|
||||
receive: (channel, cbFunc) => {
|
||||
if (rendererAvailChannels.includes(channel)) {
|
||||
ipcRenderer.on(channel, (event, ...args) => cbFunc(...args))
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user