-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
notifications-isolated.js
48 lines (38 loc) · 1.2 KB
/
notifications-isolated.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
(window => {
const notifications = new Map();
// Handle events sent from the browser process
window.addEventListener('message', ({data: {type, data}}) => {
if (type === 'notification-callback') {
const {callbackName, id} = data;
const notification = notifications.get(id);
if (notification && notification[callbackName]) {
notification[callbackName]();
}
if (callbackName === 'onclose') {
notifications.delete(id);
}
}
if (type === 'notification-reply-callback') {
const {callbackName, id, previousConversation, reply} = data;
const notification = notifications.get(id);
if (notification && notification[callbackName]) {
notification[callbackName]();
}
notifications.delete(id);
window.postMessage({type: 'notification-reply', data: {previousConversation, reply}}, '*');
}
});
let counter = 1;
window.Notification = Object.assign(
class {
constructor(title, options) {
this.id = counter++;
notifications.set(this.id, this);
window.postMessage({type: 'notification', data: {title, id: this.id, ...options}}, '*');
}
// No-op, but Messenger expects this method to be present
close() {}
},
window.Notification
);
})(window);