mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-04-26 22:32:42 +08:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { contextBridge, ipcRenderer } from 'electron'
|
|
import { electronAPI } from '@electron-toolkit/preload'
|
|
|
|
// Custom APIs for renderer
|
|
const api = {
|
|
send: (channel, data) => {
|
|
// whitelist channels
|
|
const validChannels = [
|
|
'show-message',
|
|
'check-update',
|
|
'get-gpu-acceleration',
|
|
'set-gpu-acceleration',
|
|
'save-gpu-acceleration',
|
|
]
|
|
if (validChannels.includes(channel)) {
|
|
ipcRenderer.send(channel, data)
|
|
}
|
|
},
|
|
receive: (channel, func) => {
|
|
const validChannels = ['show-message']
|
|
if (validChannels.includes(channel)) {
|
|
// Deliberately strip event as it includes `sender`
|
|
ipcRenderer.on(channel, (event, ...args) => func(...args))
|
|
}
|
|
},
|
|
}
|
|
|
|
// Use `contextBridge` APIs to expose Electron APIs to
|
|
// renderer only if context isolation is enabled, otherwise
|
|
// just add to the DOM global.
|
|
if (process.contextIsolated) {
|
|
try {
|
|
contextBridge.exposeInMainWorld('electron', electronAPI)
|
|
contextBridge.exposeInMainWorld('api', api)
|
|
} catch (error) {
|
|
console.error(error)
|
|
}
|
|
} else {
|
|
// @ts-ignore (define in dts)
|
|
window.electron = electronAPI
|
|
// @ts-ignore (define in dts)
|
|
window.api = api
|
|
}
|