添加 Electron 应用基础结构,包括: - package.json 配置文件 - 主进程和预加载脚本 - README 文档 - 自动更新功能 - 系统托盘和菜单 - IPC 通信机制
66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
/**
|
|
* 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<string>;
|
|
getPlatform: () => Promise<string>;
|
|
checkUpdate: () => Promise<any>;
|
|
downloadUpdate: () => Promise<void>;
|
|
installUpdate: () => Promise<void>;
|
|
onUpdateAvailable: (callback: (info: any) => void) => void;
|
|
onUpdateDownloaded: (callback: (info: any) => void) => void;
|
|
openExternal: (url: string) => Promise<void>;
|
|
onNavigate: (callback: (path: string) => void) => void;
|
|
removeAllListeners: (channel: string) => void;
|
|
};
|
|
}
|
|
}
|