forked from plotly/falcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreload.js
54 lines (45 loc) · 1.37 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
53
54
// this file is preloaded by electron in the renderer process
// (it has access to electron's API, even when nodeIntegration has been disabled)
const {ipcRenderer, remote} = require('electron');
const propertyOptions = {
configurable: false,
enumerable: false,
writable: false
};
process.once('loaded', () => {
const $falcon = {};
Object.defineProperty(window, '$falcon', {
value: $falcon,
...propertyOptions
});
Object.defineProperty($falcon, 'showOpenDialog', {
value: remote.dialog.showOpenDialog,
...propertyOptions
});
let onUsername;
let receivedUsernameBeforeCallback;
let username;
function sendUsername(event, user) {
try {
if (onUsername) {
receivedUsernameBeforeCallback = false;
onUsername(event, user);
} else {
receivedUsernameBeforeCallback = true;
username = user;
}
} catch (error) {
console.error(error); // eslint-disable-line
}
}
ipcRenderer.on('username', sendUsername);
Object.defineProperty($falcon, 'setUsernameListener', {
value: (value) => {
onUsername = value;
if (receivedUsernameBeforeCallback) {
sendUsername(null, username);
}
},
...propertyOptions
});
});