32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
import { contextBridge, ipcRenderer, shell } from 'electron'
|
|
import { electronAPI } from '@electron-toolkit/preload'
|
|
|
|
const customAPI = {
|
|
ipcRenderer: {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
invoke: (channel: string, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
|
|
on: (channel: string, listener: (...args: any[]) => void) =>
|
|
ipcRenderer.on(channel, (_, ...args) => listener(...args)),
|
|
removeAllListeners: (channel: string) => ipcRenderer.removeAllListeners(channel)
|
|
},
|
|
openExternal: (url: string) => shell.openExternal(url)
|
|
}
|
|
|
|
// Gộp cả electronAPI và customAPI
|
|
const mergedAPI = {
|
|
...electronAPI,
|
|
...customAPI
|
|
}
|
|
|
|
if (process.contextIsolated) {
|
|
try {
|
|
contextBridge.exposeInMainWorld('electron', mergedAPI)
|
|
} catch (error) {
|
|
console.error('Failed to expose electron API:', error)
|
|
}
|
|
} else {
|
|
// @ts-ignore
|
|
window.electron = mergedAPI
|
|
}
|