72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
import { contextBridge, ipcRenderer } from 'electron'
|
|
import { IPC_EVENTS } from '@lib/constants';
|
|
|
|
const api: WindowApi = {
|
|
versions: process.versions,
|
|
|
|
external: {
|
|
open: (url: string) => ipcRenderer.invoke('external-open', url)
|
|
},
|
|
|
|
closeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_CLOSE),
|
|
minimizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MINIMIZE),
|
|
maximizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MAXIMIZE),
|
|
onWindowMaximized: (callback: (isMaximized: boolean) => void) => ipcRenderer.on(IPC_EVENTS.WINDOW_MAXIMIZE + 'back', (_, isMaximized) => callback(isMaximized)),
|
|
isWindowMaximized: () => ipcRenderer.invoke(IPC_EVENTS.IS_WINDOW_MAXIMIZED),
|
|
viewIsReady: () => ipcRenderer.send(IPC_EVENTS.RENDERER_IS_READY),
|
|
|
|
app: {
|
|
setFrameless: (route?: string) => ipcRenderer.invoke(IPC_EVENTS.APP_SET_FRAMELESS, route),
|
|
loadPage: (page: string) => ipcRenderer.invoke(IPC_EVENTS.APP_LOAD_PAGE, page)
|
|
},
|
|
|
|
// 通过 IPC 调用主进程
|
|
readFile: (filePath: string) => ipcRenderer.invoke(IPC_EVENTS.READ_FILE, filePath),
|
|
|
|
// 异步调用(映射为 electron 的 invoke)
|
|
invoke: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
|
|
|
|
// 异步调用(为了兼容老代码)
|
|
invokeAsync: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.invoke(channel, ...args),
|
|
|
|
// 监听主进程消息
|
|
on: (event: IPC_EVENTS, callback: (...args: any[]) => void) => {
|
|
const subscription = (_event: any, ...args: any[]) => callback(...args)
|
|
ipcRenderer.on(event, subscription)
|
|
return () => ipcRenderer.removeListener(event, subscription)
|
|
},
|
|
|
|
// 发送消息到主进程
|
|
send: (channel: IPC_EVENTS, ...args: any[]) => ipcRenderer.send(channel, ...args),
|
|
|
|
// 获取窗口ID
|
|
getCurrentWindowId: () => ipcRenderer.sendSync(IPC_EVENTS.GET_WINDOW_ID),
|
|
|
|
// 发送日志
|
|
logger: {
|
|
debug: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_DEBUG, message, ...meta),
|
|
info: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_INFO, message, ...meta),
|
|
warn: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_WARN, message, ...meta),
|
|
error: (message: string, ...meta: any[]) => ipcRenderer.send(IPC_EVENTS.LOG_ERROR, message, ...meta),
|
|
},
|
|
|
|
// 执行脚本
|
|
executeScript: (params: any) => ipcRenderer.invoke(IPC_EVENTS.EXECUTE_SCRIPT, params),
|
|
|
|
// 打开渠道
|
|
openChannel: (channels: any) => ipcRenderer.invoke(IPC_EVENTS.OPEN_CHANNEL, channels),
|
|
|
|
// 脚本管理
|
|
scriptApi: {
|
|
list: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_LIST),
|
|
get: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_GET, id),
|
|
save: (input: any) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_SAVE, input),
|
|
delete: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_DELETE, id),
|
|
toggle: (id: string, enabled: boolean) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_TOGGLE, id, enabled),
|
|
run: (id: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RUN, id),
|
|
startRecording: (url?: string) => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_START, url),
|
|
stopRecording: () => ipcRenderer.invoke(IPC_EVENTS.SCRIPT_RECORD_STOP),
|
|
},
|
|
}
|
|
|
|
contextBridge.exposeInMainWorld('api', api) |