-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
52 lines (48 loc) · 1.51 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
48
49
50
51
52
const { contextBridge } = require("electron");
const ipc_renderer = require('electron').ipcRenderer;
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]);
}
});
contextBridge.exposeInMainWorld(
// Allowed 'ipcRenderer' methods.
'ipcRender', {
// From render to main.
send: (channel, args) => {
let valid_channels = ipc.render.send;
if (valid_channels.includes(channel)) {
ipc_renderer.send(channel, args);
}
},
// From main to render.
receive: (channel, listener) => {
let valid_channels = ipc.render.receive;
if (valid_channels.includes(channel)) {
// Deliberately strip event as it includes `sender`.
ipc_renderer.on(channel, (event, ...args) => listener(...args));
}
},
// From render to main and back again.
invoke: (channel, args) => {
let valid_channels = ipc.render.sendReceive;
if (valid_channels.includes(channel)) {
return ipc_renderer.invoke(channel, args);
}
}
}
);
const preload = {
open_dialog: (options)=>void(async() => {
var path = await ipc_renderer.invoke("open_dialog", options)
.then((path)=>{
return path.filePaths[0];
})
return path;
})(),
}
contextBridge.exposeInMainWorld('electron', preload);