Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
123 lines (113 loc) · 4.16 KB
/
Copy pathbackground.js
File metadata and controls
123 lines (113 loc) · 4.16 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
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
// Fetch contests from all platforms
async function fetchAllContests() {
try {
const [codeforces, leetcode, codechef] = await Promise.all([
fetch('https://codeforces.com/api/contest.list').then(res => res.json()),
fetch('https://leetcode.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
query {
allContests {
title
titleSlug
startTime
duration
isVirtual
}
}
`
})
}).then(res => res.json()),
fetch('https://www.codechef.com/api/list/contests/all').then(res => res.json())
]);
const contests = [
...codeforces.result
.filter(contest => contest.phase === 'BEFORE')
.map(contest => ({
id: `cf-${contest.id}`,
name: contest.name,
platform: 'codeforces',
startTime: new Date(contest.startTimeSeconds * 1000).toISOString(),
endTime: new Date((contest.startTimeSeconds + contest.durationSeconds) * 1000).toISOString(),
duration: contest.durationSeconds / 60,
url: `https://codeforces.com/contests/${contest.id}`,
})),
...leetcode.data.allContests
.filter(contest => !contest.isVirtual)
.map(contest => ({
id: `lc-${contest.titleSlug}`,
name: contest.title,
platform: 'leetcode',
startTime: new Date(contest.startTime * 1000).toISOString(),
endTime: new Date((contest.startTime + contest.duration) * 1000).toISOString(),
duration: contest.duration,
url: `https://leetcode.com/contest/${contest.titleSlug}`,
})),
...codechef.future_contests.map(contest => ({
id: `cc-${contest.contest_code}`,
name: contest.contest_name,
platform: 'codechef',
startTime: new Date(contest.contest_start_date).toISOString(),
endTime: new Date(contest.contest_end_date).toISOString(),
duration: Math.round((new Date(contest.contest_end_date) - new Date(contest.contest_start_date)) / (60 * 1000)),
url: `https://www.codechef.com/${contest.contest_code}`,
}))
];
// Store contests in chrome storage
await chrome.storage.local.set({ contests });
// Check for upcoming contests and set notifications
const now = new Date();
contests.forEach(contest => {
const startTime = new Date(contest.startTime);
const timeUntilStart = startTime - now;
// Set notification for contests starting in the next 24 hours
if (timeUntilStart > 0 && timeUntilStart <= 24 * 60 * 60 * 1000) {
const notificationTime = new Date(startTime.getTime() - 30 * 60 * 1000); // 30 minutes before
chrome.alarms.create(`contest-${contest.id}`, {
when: notificationTime.getTime()
});
}
});
return contests;
} catch (error) {
console.error('Error fetching contests:', error);
return [];
}
}
// Handle alarm triggers for notifications
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name.startsWith('contest-')) {
const contestId = alarm.name.replace('contest-', '');
const { contests } = await chrome.storage.local.get('contests');
const contest = contests.find(c => c.id === contestId);
if (contest) {
chrome.notifications.create(contestId, {
type: 'basic',
iconUrl: `icons/icon128.png`,
title: 'Upcoming Contest!',
message: `${contest.name} starts in 30 minutes!`
});
}
}
});
// Handle notification clicks
chrome.notifications.onClicked.addListener((notificationId) => {
const contestId = notificationId;
chrome.storage.local.get('contests', ({ contests }) => {
const contest = contests.find(c => c.id === contestId);
if (contest) {
chrome.tabs.create({ url: contest.url });
}
});
});
// Initial fetch
fetchAllContests();
// Fetch contests every hour
chrome.alarms.create('fetch-contests', { periodInMinutes: 60 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'fetch-contests') {
fetchAllContests();
}
});