-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpreload.js
More file actions
66 lines (53 loc) · 2.6 KB
/
preload.js
File metadata and controls
66 lines (53 loc) · 2.6 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
63
64
65
/**
* Preload script for secure IPC communication
* This script runs in a context that has access to both DOM and Node.js APIs
* but isolates the renderer process from direct Node.js access
*/
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', {
// File system operations
readFile: (path) => ipcRenderer.invoke('read-file', path),
writeFile: (path, content) => ipcRenderer.invoke('write-file', path, content),
existsFile: (path) => ipcRenderer.invoke('exists-file', path),
mkdir: (path) => ipcRenderer.invoke('mkdir', path),
readdir: (path) => ipcRenderer.invoke('readdir', path),
unlink: (path) => ipcRenderer.invoke('unlink', path),
stat: (path) => ipcRenderer.invoke('stat', path),
// Process execution (with sanitization)
execCommand: (command, args, options) => ipcRenderer.invoke('exec-command', command, args, options),
// System information
getSystemInfo: () => ipcRenderer.invoke('get-system-info'),
// Driver operations
checkDriverStatus: () => ipcRenderer.invoke('check-driver-status'),
reloadDriver: () => ipcRenderer.invoke('reload-driver'),
// App control
quitApp: () => ipcRenderer.send('quit-app'),
updateIcon: (statusClass) => ipcRenderer.send('driver-status-changed', statusClass),
// Shell operations
openExternal: (url) => ipcRenderer.invoke('open-external', url),
openPath: (path) => ipcRenderer.invoke('open-path', path),
// Named pipe operations
sendPipeCommand: (command) => ipcRenderer.invoke('send-pipe-command', command),
// Window controls
minimizeWindow: () => ipcRenderer.send('window-minimize'),
maximizeWindow: () => ipcRenderer.send('window-maximize'),
closeWindow: () => ipcRenderer.send('window-close'),
// Event listeners
on: (channel, callback) => {
const validChannels = ['driver-status-update', 'window-maximized', 'window-unmaximized'];
if (validChannels.includes(channel)) {
ipcRenderer.on(channel, (event, ...args) => callback(...args));
}
},
removeListener: (channel, callback) => {
ipcRenderer.removeListener(channel, callback);
}
});
// Expose a safe console object for debugging
contextBridge.exposeInMainWorld('safeConsole', {
log: (...args) => console.log('[Renderer]', ...args),
error: (...args) => console.error('[Renderer]', ...args),
warn: (...args) => console.warn('[Renderer]', ...args)
});