-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
44 lines (35 loc) · 1.19 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
// Run when the service worker is initialized
chrome.storage.sync.get(['urls'], function(result) {
setupAlarms(result.urls || []);
});
// Listen for changes in storage and update alarms
chrome.storage.onChanged.addListener(function(changes, area) {
if (area === 'sync' && changes.urls) {
setupAlarms(changes.urls.newValue || []);
}
});
function setupAlarms(urls) {
// Clear all existing alarms
chrome.alarms.clearAll(function() {
// Set new alarms
urls.forEach(({ url, time }) => {
if (!/^https:\/\//i.test(url)) {
url = 'http://' + url;
}
const [hours, minutes] = time.split(':');
const now = new Date();
const alarmTime = new Date();
alarmTime.setHours(hours);
alarmTime.setMinutes(minutes);
if (alarmTime > now) {
const delayInMinutes = (alarmTime - now) / 1000 / 60;
chrome.alarms.create(url, { delayInMinutes });
} else {
console.log(`Scheduled time ${alarmTime} has already passed for ${url}`);
}
});
});
}
chrome.alarms.onAlarm.addListener(function(alarm) {
chrome.tabs.create({ url: alarm.name });
});