-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreload.js
More file actions
44 lines (42 loc) · 1.78 KB
/
preload.js
File metadata and controls
44 lines (42 loc) · 1.78 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
34
35
36
37
38
39
40
41
42
43
44
// Preload script for Electron
const { contextBridge, ipcRenderer } = require('electron');
// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld(
'electronAPI', {
// Send messages to main process
send: (channel, data) => {
// Whitelist channels
let validChannels = ['toMain', 'check-for-update', 'restart-app'];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
// Receive messages from main process
receive: (channel, func) => {
let validChannels = ['fromMain', 'update-available', 'update-downloaded', 'update-not-available', 'update-error', 'bot-output', 'bot-finished'];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
// Invoke methods and get responses
invoke: (channel, data) => {
let validChannels = ['dialog:openFile', 'dialog:saveFile', 'force-update-check', 'launch-bot'];
if (validChannels.includes(channel)) {
return ipcRenderer.invoke(channel, data);
}
return Promise.reject(new Error(`Invalid channel: ${channel}`));
},
// Check if app is packaged
isPackaged: () => {
return ipcRenderer.invoke('is-packaged');
},
// Get resource path
getResourcePath: (filename) => {
return ipcRenderer.invoke('get-resource-path', filename);
}
}
);
// Log that preload script has loaded
console.log('Preload script loaded successfully');