-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
48 lines (47 loc) · 1.3 KB
/
preload.js
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
45
46
47
48
// allow IPC
const { contextBridge, ipcRenderer, BrowserWindow } = require('electron');
process.once('loaded', () => {
// make IPC communication available to renderer
// whitelist channels
let validSenderChannels = [
'scoop-list',
'scoop-status',
'scoop-update',
'scoop-update-app',
'scoop-update-all',
'scoop-uninstall-app',
'scoop-checkver',
'scoop-update-app',
'scoop-bucket-list',
];
let validReceiverChannels = [
'app-list-entry',
'app-list-entry-remove',
'bucket-list-entry',
'console-log',
];
validSenderChannels.forEach(channel => {
validReceiverChannels.push(`${channel}-started`);
validReceiverChannels.push(`${channel}-finished`);
});
contextBridge.exposeInMainWorld(
'api',
{
send: (channel, data) => {
if (validSenderChannels.includes(channel)) {
ipcRenderer.send(channel, data);
} else {
console.warn(`Invalid sender channel ${channel}`);
}
},
on: (channel, func) => {
if (validReceiverChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (_event, ...args) => func(...args));
} else {
console.warn(`Invalid receiver channel ${channel}`);
}
}
}
)
});