diff --git a/electron/preload/index.ts b/electron/preload/index.ts index bcfd20e..3d4d872 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -1,26 +1,27 @@ import { ipcRenderer, contextBridge } from 'electron' // --------- Expose some API to the Renderer process --------- -contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer)) +contextBridge.exposeInMainWorld('ipcRenderer', { + on(...args: Parameters) { + const [channel, listener] = args + ipcRenderer.on(channel, (event, ...args) => listener(event, ...args)) + }, + off(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.off(channel, ...omit) + }, + send(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.send(channel, ...omit) + }, + invoke(...args: Parameters) { + const [channel, ...omit] = args + ipcRenderer.invoke(channel, ...omit) + }, -// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it. -function withPrototype(obj: Record) { - const protos = Object.getPrototypeOf(obj) - - for (const [key, value] of Object.entries(protos)) { - if (Object.prototype.hasOwnProperty.call(obj, key)) continue - - if (typeof value === 'function') { - // Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function. - obj[key] = function (...args: any) { - return value.call(obj, ...args) - } - } else { - obj[key] = value - } - } - return obj -} + // You can expose other APTs you need here. + // ... +}) // --------- Preload scripts loading --------- function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {