/** * Electron Preload 脚本 * * 功能描述: * 在渲染进程中安全地暴露主进程 API。 * 使用 contextBridge 确保安全性。 * * @author Timeline Team * @date 2024 */ import { contextBridge, ipcRenderer } from 'electron'; /** * 暴露给渲染进程的 API */ contextBridge.exposeInMainWorld('electronAPI', { // 应用信息 getVersion: () => ipcRenderer.invoke('get-version'), getPlatform: () => ipcRenderer.invoke('get-platform'), // 更新相关 checkUpdate: () => ipcRenderer.invoke('check-update'), downloadUpdate: () => ipcRenderer.invoke('download-update'), installUpdate: () => ipcRenderer.invoke('install-update'), // 更新事件监听 onUpdateAvailable: (callback: (info: any) => void) => { ipcRenderer.on('update-available', (_, info) => callback(info)); }, onUpdateDownloaded: (callback: (info: any) => void) => { ipcRenderer.on('update-downloaded', (_, info) => callback(info)); }, // 外部链接 openExternal: (url: string) => ipcRenderer.invoke('open-external', url), // 导航事件 onNavigate: (callback: (path: string) => void) => { ipcRenderer.on('navigate', (_, path) => callback(path)); }, // 移除监听器 removeAllListeners: (channel: string) => { ipcRenderer.removeAllListeners(channel); } }); // 类型声明 declare global { interface Window { electronAPI: { getVersion: () => Promise; getPlatform: () => Promise; checkUpdate: () => Promise; downloadUpdate: () => Promise; installUpdate: () => Promise; onUpdateAvailable: (callback: (info: any) => void) => void; onUpdateDownloaded: (callback: (info: any) => void) => void; openExternal: (url: string) => Promise; onNavigate: (callback: (path: string) => void) => void; removeAllListeners: (channel: string) => void; }; } }