forked from ScratchAddons/ScratchAddons
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandle-notifications.js
102 lines (94 loc) · 2.41 KB
/
handle-notifications.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
const periods = [
{
name: chrome.i18n.getMessage("15min"),
mins: 15,
},
{
name: chrome.i18n.getMessage("1hour"),
mins: 60,
},
{
name: chrome.i18n.getMessage("8hours"),
mins: 480,
},
{
name: chrome.i18n.getMessage("24hours"),
mins: 1440,
},
{
name: chrome.i18n.getMessage("untilEnabled"),
mins: Infinity,
},
];
chrome.storage.local.get("muted", (obj) => {
if (obj.muted) contextMenuMuted();
else contextMenuUnmuted();
scratchAddons.muted = obj.muted;
});
chrome.contextMenus.removeAll();
let currentMenuItem = null;
chrome.contextMenus.onClicked.addListener(({ parentMenuItemId, menuItemId }) => {
if (parentMenuItemId === "mute") {
const mins = Number(menuItemId.split("_")[1]);
contextMenuMuted();
muteForMins(mins);
} else if (menuItemId === "unmute") {
contextMenuUnmuted();
unmute();
}
});
function contextMenuUnmuted() {
if (currentMenuItem === "unmute") chrome.contextMenus.remove("unmute");
currentMenuItem = "mute";
chrome.contextMenus.create({
id: "mute",
title: chrome.i18n.getMessage("muteFor"),
contexts: ["browser_action"],
});
for (const period of periods) {
chrome.contextMenus.create({
id: `mute_${period.mins}`,
title: period.name,
parentId: "mute",
contexts: ["browser_action"],
});
}
chrome.browserAction.setIcon({
path: {
16: "../images/icon-16.png",
32: "../images/icon-32.png",
},
});
}
function contextMenuMuted() {
if (currentMenuItem === "mute") chrome.contextMenus.remove("mute");
currentMenuItem = "unmute";
chrome.contextMenus.create({
id: "unmute",
title: chrome.i18n.getMessage("unmute"),
contexts: ["browser_action"],
});
chrome.browserAction.setIcon({
path: {
16: "../images/icon-gray-16.png",
32: "../images/icon-gray-32.png",
},
});
}
function muteForMins(mins) {
if (mins !== Infinity) chrome.alarms.create("muted", { delayInMinutes: mins });
scratchAddons.muted = true;
scratchAddons.localEvents.dispatchEvent(new CustomEvent("badgeUpdateNeeded"));
chrome.storage.local.set({ muted: true });
}
function unmute() {
scratchAddons.muted = false;
scratchAddons.localEvents.dispatchEvent(new CustomEvent("badgeUpdateNeeded"));
chrome.storage.local.set({ muted: false });
}
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "muted") {
unmute();
contextMenuUnmuted();
}
});