-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreload.js
43 lines (39 loc) · 1.12 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
/**
* 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
*/
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])
}
})
// Preload (Isolated World)
// 三种方式在main与html之间通信
const {
contextBridge,
ipcRenderer
} = require('electron')
contextBridge.exposeInMainWorld(
'ipcRenderer', {
// From render to main.
send: (channel, args) => {
return ipcRenderer.sendSync(channel, 'ping')
},
// From main to render.
// listener为自定义的消息处理函数,原型在renderer中
receive: (channel, listener) => {
ipcRenderer.on(channel, (event, args) => listener(args));
},
// From render to main and back again.
invoke: (channel, args) => {
return ipcRenderer.invoke(channel, args);
}
}
)