-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
33 lines (29 loc) · 1.04 KB
/
Copy pathpreload.js
File metadata and controls
33 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Preload 脚本 — 安全地暴露 IPC 接口给渲染进程
*/
const { contextBridge, ipcRenderer, webUtils } = require('electron');
contextBridge.exposeInMainWorld('electronAPI', {
// 获取文件真实路径(contextIsolation 下 File.path 不可用)
getFilePath: (file) => {
try {
return webUtils.getPathForFile(file);
} catch {
return null;
}
},
// 替换图标(传入文件路径,避免大文件 IPC 传输)
replaceIcon: (exePath, icoPath) =>
ipcRenderer.invoke('replace-icon', { exePath, icoPath }),
// 提取图标(传入文件路径,避免大文件 IPC 传输)
extractIcon: (exePath) =>
ipcRenderer.invoke('extract-icon', { exePath }),
// 监听菜单事件
onMenuOpenExe: (callback) => {
ipcRenderer.on('menu-open-exe', callback);
return () => ipcRenderer.removeListener('menu-open-exe', callback);
},
onMenuOpenIco: (callback) => {
ipcRenderer.on('menu-open-ico', callback);
return () => ipcRenderer.removeListener('menu-open-ico', callback);
},
});