-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg-script.js
97 lines (84 loc) · 3.21 KB
/
bg-script.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
let notificationClicked = false;
browser.notifications.onClicked.addListener(function (notificationId) {
notificationClicked = true;
console.log('notificantion is clicekd by user.')
});
// browser.notifications.onClosed.addListener(function (notificationId) {
// if (notificationClicked) {
// console.log("Notification closed by user");
// } else {
// console.log("Notification closed by operating system");
// }
// });
async function createNotification(task) {
await browser.notifications.create(
{
type: 'basic',
title: 'Remainder!!!',
message: task,
// eventtime: Date.now()+1000*5
// buttons: [{
// title: 'Close',
// iconUrl: 'icons/close-48.png',
// }]
}
)
}
browser.alarms.onAlarm.addListener(async function (alarmInfo) {
console.log("Alarm fired: ", alarmInfo.name);
let obj = await browser.storage.local.get(alarmInfo.name)
let task = obj[alarmInfo.name].rem;
let ID = obj[alarmInfo.name].id;
// createnotification returns a promise
// await createNotification(task);
let taskURL = browser.runtime.getURL("task-extension-page/task.html") + "?task=" + task + "&id=" + ID;
browser.tabs.create({ url: taskURL })
});
browser.runtime.onMessage.addListener(async function (data, sender) {
if (data.case === 'doneBtn') {
// handle clicking doneBtn on extension page
// i.e. delete that remainder
const currObj = await browser.storage.local.get(null);
if (currObj.hasOwnProperty(data.id)) {
browser.storage.local.remove(data.id)
let obj = await browser.storage.local.get(null)
const { count, ...newObj } = obj;
await browser.storage.local.set({ ...newObj, count: count - 1 })
console.log('removed')
}
} else if (data.case === 'snoozeBtn') {
await browser.alarms.clear(data.id)
let minutes = 10;
let timeSinceEpochMs = Date.now() + minutes * 60 * 1000;
await browser.alarms.create(`${data.id}`, {
when: timeSinceEpochMs,
})
const obj = await browser.storage.local.get(null)
console.log('++++++++')
console.log(obj)
delete obj[data.id];
console.log(obj)
console.log('++++++++')
await browser.storage.local.set({ ...obj, [data.id]: { rem: data.task, time: new Date(timeSinceEpochMs), id: data.id } })
const newObj = await browser.storage.local.get(null)
console.log(newObj)
console.log('++++++++')
}
})
// browser.notifications.onButtonClicked.addListener(function (notifID, buttonIndex) {
// console.log('Closed by clicking')
// // if (buttonIndex == 1) {
// // // clear that notification.
// // browser.notifications.clear(notifID)
// // }
// })
// browser.notifications.onClicked.addListener(function(notifId){
// console.log('clicked by user')
// console.log(notifId)
// // browser.notifications.clear(notifId);
// })
// browser.notifications.onClosed.addListener(function (notifId,byUser) {
// console.log(notifId)
// console.log('Closed on own')
// console.log(byUser)
// })