2025-04-10 15:11:22 +08:00

29 lines
1023 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//TBD 这个地方必须用用require不然会白屏
const { contextBridge, ipcRenderer } = require('electron')
// import { contextBridge, ipcRenderer } from 'electron'
contextBridge.exposeInMainWorld(
'electron',
{
send: (channel, args) => {
ipcRenderer.send(channel, args)
},
// 通过 channel 向主进程发送消息,并异步等待结果。主进程应该使用 ipcMain.handle() 监听 channel
invoke: (channel, args) => {
return new Promise(resolve => ipcRenderer.invoke(channel, args).then(data => resolve(data)).catch(e => console.log(e)))
},
// 监听 channel 事件
on: (channel, func) => {
ipcRenderer.on(channel, (event, ...args) => func(event, ...args))
},
// 一次性监听事件
once: (channel, func) => {
ipcRenderer.once(channel, (event, ...args) => func(event, ...args))
},
setTitle: (title) => ipcRenderer.send('win-setTitle', title)
}
)