【发布时间】:2021-06-06 12:22:12
【问题描述】:
我正在使用 Electron 12.0.0 构建一个多窗口应用程序并尝试使用最新的默认值(nodeIntegration = false,contextIsolation = true)。我试图弄清楚每个渲染器进程应该如何告诉主进程哪个窗口正在发送 IPC 消息。当我尝试通过执行以下操作将 window 对象作为参数传递给 ipcRenderer.send() 时,出现错误(如下所示):
window.api.receive('save', () => {
window.api.send('save-file', window, markdownView.value)
})
window.api 在preload.js 中定义如下:
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld(
"api",
{
send: (channel, ...args) => {
const validChannels = [
'save-file'
]
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, ...args)
}
},
receive: (channel, func) => {
const validChannels = [
'save',
'file-opened',
'file-saved'
]
if (validChannels.includes(channel)) {
// Strip off the event since it includes the sender
ipcRenderer.on(channel, (event, ...args) => func(...args))
}
}
}
)
一切正常,直到我尝试调用 Save 菜单项(它将save 消息发送到渲染器)并且我在(开发者工具的)控制台中收到以下错误:
[Deprecation] 'window.webkitStorageInfo' is deprecated. renderer.js:9
Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead.
(anonymous) @ renderer.js:9
(anonymous) @ VM76 preload.js:22
emit @ VM14 events.js:315
onMessage @ VM75 renderer_init.js:93
Uncaught Error: Uncaught Error: Uncaught Error: An object could not be cloned.
at EventEmitter.<anonymous> (VM76 preload.js:22)
at EventEmitter.emit (VM14 events.js:315)
at Object.onMessage (VM75 renderer_init.js:93)
(anonymous) @ VM76 preload.js:22
emit @ VM14 events.js:315
onMessage @ VM75 renderer_init.js:93
很明显,我做错了什么。我该如何做到这一点,最好不要打开 nodeIntegration 或关闭 contextIsolation?
【问题讨论】:
标签: javascript electron