Repository navigation
Expand file tree
/
Copy pathpreload.js
More file actions
62 lines (57 loc) · 1.66 KB
/
Copy pathpreload.js
File metadata and controls
62 lines (57 loc) · 1.66 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { contextBridge, ipcRenderer } = require('electron');
const INVOKE_CHANNELS = new Set([
'get-config',
'save-config',
'go-to-slide',
'next-slide',
'previous-slide',
'reload-current-slide',
'reload-with-new-config',
'exit-app',
'show-screensaver',
'hide-screensaver',
'is-screensaver-active',
'get-moon-phase',
'get-todays-bitcoin-fact',
'set-modal-state',
'get-autostart-status',
'set-autostart',
'get-wifi-status',
'scan-wifi',
'connect-wifi',
'disconnect-wifi'
]);
const EVENT_CHANNELS = new Set([
'config-loaded',
'view-changed',
'webview-error',
'webview-loaded',
'screensaver-shown',
'screensaver-hidden',
'screensaver-error',
'screensaver-loaded'
]);
// Expose secure API to renderer process
contextBridge.exposeInMainWorld('electronAPI', {
invoke: (channel, data) => {
if (!INVOKE_CHANNELS.has(channel)) {
throw new Error(`IPC channel '${channel}' is not allowed`);
}
return ipcRenderer.invoke(channel, data);
},
// Returns a function that removes the listener
on: (channel, func) => {
if (!EVENT_CHANNELS.has(channel)) {
throw new Error(`IPC channel '${channel}' is not allowed`);
}
const subscription = (event, ...args) => func(...args);
ipcRenderer.on(channel, subscription);
return () => ipcRenderer.removeListener(channel, subscription);
},
// Logging interface (no direct access to electron-log)
log: {
info: (message, ...args) => ipcRenderer.invoke('log-info', { message, args }),
error: (message, ...args) => ipcRenderer.invoke('log-error', { message, args }),
warn: (message, ...args) => ipcRenderer.invoke('log-warn', { message, args })
}
});