-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
48 lines (40 loc) · 1.28 KB
/
background.js
File metadata and controls
48 lines (40 loc) · 1.28 KB
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
// background.js
// Function to create the hourly alarm
function createAlarm() {
chrome.alarms.create("hourlyLogger", {
when: Date.now(), // Start immediately
periodInMinutes: 60 // Repeat every hour
});
}
// Listen for alarm triggers
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "hourlyLogger") {
sendNotification();
}
});
// Function to send a notification
function sendNotification() {
const now = new Date();
const hour = now.getHours();
if (hour >= 10 && hour <= 18) { // Only notify between 10 AM - 6 PM
chrome.notifications.create({
type: "basic",
iconUrl: "icon.png",
title: "Hourly Check-In",
message: "What did you do this past hour?",
priority: 2,
buttons: [{ title: "Log Activity" }]
});
}
}
// Handle notification button clicks
chrome.notifications.onButtonClicked.addListener((notificationId) => {
if (notificationId) {
chrome.tabs.create({ url: "log.html" }); // Open log page
}
});
chrome.action.onClicked.addListener(() => {
chrome.tabs.create({ url: chrome.runtime.getURL("index.html") });
});
// Create alarm when extension is installed/updated
chrome.runtime.onInstalled.addListener(createAlarm);