-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
79 lines (63 loc) · 1.75 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
import fetchGfg from "./api/fetchGfg.js";
import fetchLeet from "./api/fetchLeet.js";
const ALARM_NAME = 'FetchData'
chrome.runtime.onMessage.addListener(async data => {
const { event, prefs } = data
console.log('events-->', event)
switch (event) {
case 'leetCode':
handleOnSubmit(prefs)
break;
case 'startAlarm':
createAlarm();
break;
case 'stopAlarm':
stopAlarm();
break;
default:
break;
}
})
const handleOnSubmit = (prefs) => {
console.log("dataSent from USER hit bJs");
console.log("prefs=>", prefs);
chrome.storage.local.set(prefs);
fetchLeet(prefs.leetUser);
fetchGfg(prefs.gfgUser)
// createAlarm()
setRunningStatus(true);
// setTimeout(() => {
// stopAlarm()
// console.log("stop alarm triggered")
// }, 1000);
}
const setRunningStatus = (isRunning) => {
chrome.storage.local.set({ isRunning })
}
const createAlarm = () => {
chrome.alarms.get(ALARM_NAME, existing => {
if (!existing) {
chrome.alarms.create(ALARM_NAME, {
delayInMinutes: 60.0, // first trigger after 30 minutes
periodInMinutes: 360.0
})
}
})
}
const stopAlarm = () => {
chrome.alarms.clearAll();
}
chrome.alarms.onAlarm.addListener(() => {
console.log("onAlarm scheduled running")
chrome.storage.local.get(['leetUser', 'gfgUser'], (data) => {
const { leetUser, gfgUser } = data;
// Check if the user data exists before fetching
if (leetUser) {
fetchLeet(leetUser);
}
if (gfgUser) {
fetchGfg(gfgUser);
}
});
})
// chrome.storage.sync.clear();