-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbackground.js
161 lines (145 loc) · 3.85 KB
/
background.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* global messenger */
// Defining a onButtonClicked listener
messenger.NotificationBox.onButtonClicked.addListener((windowId, notificationId, buttonId) => {
console.log(`Listener #1 sees: button ${buttonId} clicked in notification ${notificationId} in window ${windowId}.`);
if (["btn-keep"].includes(buttonId)) {
console.log("Box will not close, as long as one listener returns {close:false}.");
return { close: false };
}
return { close: true };
});
// Defining another onButtonClicked listener
messenger.NotificationBox.onButtonClicked.addListener((windowId, notificationId, buttonId) => {
console.log(`Listener #2 sees: button ${buttonId} clicked in notification ${notificationId} in window ${windowId}.`);
if (["btn-direct"].includes(buttonId)) {
console.log("Box will close as long no listener returns {close:false}.");
}
});
// Defining a onDismissed listener
messenger.NotificationBox.onDismissed.addListener((windowId, notificationId) => {
console.log(`notification ${notificationId} in window ${windowId} was dismissed`);
});
// Defining a onClosed listener
messenger.NotificationBox.onClosed.addListener((windowId, notificationId, closedByUser) => {
console.log(`notification ${notificationId} in window ${windowId} was closed by user: ${closedByUser}`);
});
messenger.menus.create({
contexts: ["all"],
icons: {
16: "icon.png",
32: "icon.png",
},
id: "message",
title: "Show Notification (message top)",
visible: true
}, () => {
console.log("MENU ADDED");
});
messenger.menus.create({
contexts: ["all"],
icons: {
16: "icon.png",
32: "icon.png",
},
id: "top",
title: "Show Notification (top)",
visible: true
}, () => {
console.log("MENU ADDED");
});
messenger.menus.create({
contexts: ["all"],
icons: {
16: "icon.png",
32: "icon.png",
},
id: "bottom",
title: "Show Notification (bottom)",
visible: true
}, () => {
console.log("MENU ADDED");
});
messenger.menus.onClicked.addListener(async (info, tab) => {
console.log("MENU CLICKED", tab, info);
await messenger.NotificationBox.create({
windowId: tab.windowId,
tabId: tab.id,
priority: 9,
label: "NOTIFICATION from MENU",
icon: "icon.png",
placement: info.menuItemId,
style: {
"color": "blue",
"font-weight": "bold",
"font-style": "italic",
"background-color": "green",
},
buttons: [
{
id: "button1",
label: "Button 1"
}
]
});
});
async function addBoxes(window) {
// adding a top box
await messenger.NotificationBox.create({
windowId: window.id,
label: "Sample notification top 1",
placement: "top",
priority: messenger.NotificationBox.PRIORITY_WARNING_HIGH,
style: {
"color": "blue",
"font-weight": "bold",
"font-style": "italic",
"background-color": "green",
},
buttons: [
{
id: "btn-direct",
label: "Close",
accesskey: "o",
},
{
id: "btn-keep",
label: "Stay!"
}
]
});
// adding a default box
await messenger.NotificationBox.create({
windowId: window.id,
priority: messenger.NotificationBox.PRIORITY_CRITICAL_HIGH,
label: "Sample notification default 2",
buttons: [
{
id: "btn",
label: "Close",
accesskey: "d",
}
]
});
// a bottom box
await messenger.NotificationBox.create({
windowId: window.id,
label: "Sample notification bottom 1",
icon: "icon.png",
placement: "bottom",
});
}
// add boxes to all future windows
messenger.windows.onCreated.addListener(addBoxes);
// add boxes to all existing windows
messenger.windows.getAll()
.then(windows => {
for (const window of windows) {
addBoxes(window);
}
});
// open a custom window to see notification bars there as well
messenger.windows.create({
height: 200,
width: 510,
type: "popup"
});