-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
187 lines (166 loc) · 4.74 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fetchCountry from "./api/fetchCountry.js";
import fetchTimes from "./api/fetchTimes.js";
chrome.commands.onCommand.addListener((command) => {
if (command === "open_popup") {
chrome.action.openPopup();
}
});
chrome.runtime.onInstalled.addListener((details) => {
console.log("onInstalled Reason: ", details.reason);
scheduleDailyAlarm();
if (Notification.permission !== "granted") {
Notification.requestPermission((permission) => {
if (permission === "granted") {
console.log("Notification permission granted.");
} else {
console.log("Notification permission denied.");
}
});
}
});
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "dailyPrayerUpdate") {
console.log("Daily prayer time update triggered.");
const currentDate = new Date().toDateString();
updateTimings(currentDate)
.then(() => {
console.log("Prayer times updated successfully.");
})
.catch((error) => {
console.error("Error updating prayer times:", error);
});
} else if (alarm.name.startsWith("prayer-")) {
const prayerName = alarm.name.replace("prayer-", "");
showPrayerNotification(prayerName);
}
});
// if (alarm.name === "dailyPrayerUpdate") {
// console.log("Daily prayer time update triggered.");
// const currentDate = new Date().toDateString();
// updateTimings(currentDate)
// .then(() => {
// console.log("Prayer times updated successfully.");
// })
// .catch((error) => {
// console.error("Error updating prayer times:", error);
// });
// }
// });
function scheduleDailyAlarm() {
// Calculate time until midnight
const now = new Date();
const nextMidnight = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1,
0,
0,
0,
0
);
const timeUntilMidnight = nextMidnight - now;
// Create an alarm to repeat every 24 hours starting at midnight
chrome.alarms.create("dailyPrayerUpdate", {
when: Date.now() + timeUntilMidnight,
periodInMinutes: 24 * 60,
});
}
async function updateTimings(currentDate) {
try {
await fetchPrayerTimes(currentDate);
const result = await chrome.storage.local.get(["timings"]);
const prayerTimes = result.timings;
if (!prayerTimes) {
throw new Error("Prayer timings not found in storage.");
}
const prayers = ["Fajr", "Dhuhr", "Asr", "Maghrib", "Isha"];
prayers.forEach((prayer) => {
if (prayerTimes[prayer]) {
const prayerTime = new Date(`${currentDate} ${prayerTimes[prayer]}`);
if (prayerTime.getTime() > Date.now()) {
schedulePrayerAlarm(prayer, prayerTime.getTime());
} else {
console.warn(
`Skipping past prayer time for ${prayer}: ${prayerTime}`
);
}
} else {
console.warn(`Prayer time not available for ${prayer}`);
}
});
console.log("Prayer times updated and alarms set:", prayerTimes);
} catch (error) {
console.error("Failed to update prayer timings:", error);
}
}
const currentDate = new Date().toDateString();
updateTimings(currentDate);
function schedulePrayerAlarm(prayer, timestamp) {
chrome.alarms.create(`prayer-${prayer}`, { when: timestamp });
console.log(`Alarm set for ${prayer} at ${new Date(timestamp)}`);
}
function showPrayerNotification(prayerName) {
chrome.notifications.create({
type: "basic",
iconUrl: "./imgs/icon-64.png",
title: "Prayer Reminder",
message: `It's time for ${prayerName} prayer.`,
priority: 2,
});
}
function fetchPrayerTimes(currentDate) {
return new Promise((resolve, reject) => {
chrome.storage.local.get(["latitude", "longitude"], (result) => {
if (result.latitude && result.longitude) {
fetchCountry(result.latitude, result.longitude)
.then((countryCode) => {
return fetchTimes(
result.latitude,
result.longitude,
getMethodByCountry(countryCode)
);
})
.then(() => {
chrome.storage.local.set({ lastUpdated: currentDate }, resolve);
})
.catch((error) => {
console.error("Error updating timings: ", error);
reject(error);
});
} else {
console.log("No location data found");
reject(new Error("No location data found"));
}
});
});
}
function getMethodByCountry(countryCode) {
const methods = {
AE: 16,
EG: 5,
IN: 1,
IQ: 3,
IR: 7,
KW: 9,
MY: 3,
PK: 1,
QA: 10,
SA: 4,
SG: 11,
TR: 13,
US: 2,
FR: 12,
RU: 14,
};
return methods[countryCode] || 3; // Default to Muslim World League
}
function checkAndUpdateTimings() {
chrome.storage.local.get(["lastUpdated"], (result) => {
const currentDate = new Date();
const lastUpdatedDate = new Date(result.lastUpdated);
if (currentDate.getDate() !== lastUpdatedDate.getDate()) {
fetchPrayerTimes(currentDate.toDateString());
}
});
}
setInterval(checkAndUpdateTimings, 1000);