-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarms.js
105 lines (89 loc) · 3.3 KB
/
alarms.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
// // background.js
// // Keep track of connected ports
// const connectedPorts = new Set();
// // Listen for incoming connections
// chrome.runtime.onConnect.addListener((port) => {
// // Add the port to our set of connected ports
// connectedPorts.add(port);
// // Listen for messages from the port
// port.onMessage.addListener((message) => {
// console.log(`Received message from port ${port.sender.tab.id}:`, message);
// // Schedule a notification
// chrome.alarms.create('notification', { when: Date.now() + 5000 });
// });
// // When the port is disconnected, remove it from the set of connected ports
// port.onDisconnect.addListener(() => {
// connectedPorts.delete(port);
// });
// });
// // Handle the alarm when it fires
// chrome.alarms.onAlarm.addListener((alarm) => {
// if (alarm.name === 'notification') {
// // Send a message to all connected ports
// connectedPorts.forEach((port) => {
// port.postMessage({ notification: 'Reminder: Time to take a break!' });
// });
// }
// });
// Keep track of connected ports
// const connectedPorts = new Set();
// // Listen for incoming connections
// chrome.runtime.onConnect.addListener((port) => {
// // Add the port to our set of connected ports
// connectedPorts.add(port);
// // Listen for messages from the port
// port.onMessage.addListener((message) => {
// console.log(`Received message from port ${port.sender.tab.id}:`, message);
// // Check if the message is a request to schedule a notification
// if (message.type === 'scheduleNotification') {
// // Schedule a notification
// chrome.alarms.create('notification', { when: Date.now() + 5000 });
// }
// });
// // When the port is disconnected, remove it from the set of connected ports
// port.onDisconnect.addListener(() => {
// connectedPorts.delete(port);
// });
// });
// // Handle the alarm when it fires
// chrome.alarms.onAlarm.addListener((alarm) => {
// if (alarm.name === 'notification') {
// // Send a message to all connected ports
// connectedPorts.forEach((port) => {
// port.postMessage({ notification: 'Reminder: Time to take a break!' });
// });
// }
// });
// background.js
let notificationId = avc;
// Listen for messages from the content script
chrome.runtime.onMessage.addListener((message) => {
if (message.scheduleNotification) {
// Create a new notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'logo2.png',
title: 'Reminder',
message: 'Time to take a break!',
requireInteraction: true,
}, (id) => {
// Store the notification ID so we can clear it later
notificationId = id;
});
}
});
// Listen for the user's response to the notification
chrome.notifications.onClicked.addListener((id) => {
if (id === notificationId) {
// Clear the notification and open a new tab
chrome.notifications.clear(notificationId);
chrome.tabs.create({ url: 'https://www.google.com/' });
}
});
// Listen for the user to close the notification
chrome.notifications.onClosed.addListener((id) => {
if (id === notificationId) {
// Clear the notification
chrome.notifications.clear(notificationId);
}
});