forked from electron/electron-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
47 lines (42 loc) · 1.65 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
/**
* The preload script runs before `index.html` is loaded
* in the renderer. 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
*/
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// EXPORT FUNCTIONS FOR USE IN REACT
const { contextBridge, ipcRenderer } = require('electron');
console.log('PRELOAD!!! YAY!');
// not sure why this doesn't work, when it works in main.js...
// const { channels } = require('./src/constants');
// Expose protected methods that allow the renderer process (React) to use
// the ipcRenderer and fs (filesystem) without exposing the entire object
contextBridge.exposeInMainWorld('ElectronAPI', {
send: (channel, data) => {
// Whitelist channels
//let validChannels = ['toMain'];
//let validChannels = [channels.TEST_EVENT_MIKE, channels.TO_MAIN];
let validChannels = ['toMain', 'test_event_mike', 'savetodo', 'loadtodo']
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
receive: (channel, func) => {
let validChannels = ['fromMain', 'todoloaded'];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
});