-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
52 lines (47 loc) · 1.67 KB
/
preload.js
File metadata and controls
52 lines (47 loc) · 1.67 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
'use strict';
/**
* The preload script runs before. It has access to web APIs
* as well as Electron's renderer process modules and some
* polyfilled Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
**/
/**
* Expose electron methods to set up api bridge
* contextBridge - https://www.electronjs.org/docs/latest/api/context-bridge
* ipcRenderer - https://www.electronjs.org/docs/latest/api/ipc-renderer
**/
const { contextBridge, ipcRenderer } = require('electron')
let BridgeMethods = {};
/**
* Expose bridge methods to the renderer process these act as a secure bridge
* between the renderer process and the main process.
* https://www.electronjs.org/docs/latest/tutorial/context-isolation
**/
BridgeMethods.EventToMain = () => {
ipcRenderer.send('event:to:electron:main', {
"optional": "data to pass to main process"
});
}
BridgeMethods.HandleMainResponse = (event) => {
ipcRenderer.on('event:from:electron:main', (event, response) => {
/**
* Our ipcRenderer call back function provides two parameters
* event: an instance of a electron ipcRenderer event.
* response: the response from the main process (optional) often JSON, Object, String, Array etc.
**/
});
}
/**
* Expose electron bridge methods for interacting with the node backend securely.
**/
contextBridge.exposeInMainWorld(
'bridgeMethods', // API namespace attached to the window object window.api.someMethod().
BridgeMethods // API methods to expose to the renderer process.
);
/**
* DOMContent loaded event handler
**/
window.addEventListener('DOMContentLoaded', () => {
console.log("Preload.js loaded");
});