Platform
- Electron -
12.0.8 - Platform – macOS
10.15.7
Description
I'm trying to display a file dialog from an Electron renderer process. I thought I could reference the dialog object in the same way I reference ipcRenderer through the contextBridge.
// preload.ts
const { contextBridge, ipcRenderer, dialog } = require('electron');
import type { IpcRendererEvent } from 'electron';
contextBridge.exposeInMainWorld(
'ipc',
{
send: (channel: string, data: string[]) => {
ipcRenderer.send(channel, data);
},
on: (channel: string, func: (evt: IpcRendererEvent, args: any) => void) => {
ipcRenderer.on(channel, func);
},
chooseFile: (title: string) => dialog.showOpenDialogSync({
title, properties: ['openFile']})
}
)
But when I invoke window.ipc.chooseFile('Some Title'), I receive an error:
Cannot read property 'showOpenDialogSync' of undefined
This seems to indicate that the dialog reference is not conveyed through
the contextBridge proxy. Yet the first two functions that convey ipcRenderer work. The exposeInMainWorld documentation warns us that not every type can be proxied.
Notes
I understand that the generality of the three functions in my example undermines the security purpose of context isolation. I'm just trying to determine what works before I invest too much time designing a detailed message passing scheme.
I understand I can implement this function in the main process and invoked it through IPC. I'm fine with that. I'm just curious why an
ipcRendererreference is successfully proxied through thecontextBridgebutdialogis not.