-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
84 lines (71 loc) · 2.52 KB
/
background.js
File metadata and controls
84 lines (71 loc) · 2.52 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
// 2025 Security: Proper service worker implementation
chrome.runtime.onInstalled.addListener(() => {
console.log("CodeRush Extension Installed - 2025 Edition!");
});
// Listener for triggered alarms (reminders)
chrome.alarms.onAlarm.addListener((alarm) => {
// Security: Validate alarm data
if (!alarm || !alarm.name) {
console.error("Invalid alarm data received");
return;
}
chrome.notifications.create(alarm.name, {
type: "basic",
iconUrl: "/src/assets/icon128.png",
title: "CodeRush Reminder",
message: `Contest "${alarm.name}" is starting soon!`,
priority: 2,
requireInteraction: true
});
});
// 2025 Security: Proper message handling without direct fetch
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// Security: Validate request structure
if (!request || typeof request.action !== 'string') {
sendResponse({ success: false, error: "Invalid request format" });
return;
}
if (request.action === "setReminder") {
const { contestName, reminderTime } = request;
// Security: Input validation
if (!contestName || typeof contestName !== 'string' || contestName.length > 100) {
sendResponse({ success: false, error: "Invalid contest name" });
return;
}
if (!reminderTime || typeof reminderTime !== 'number' || reminderTime <= Date.now()) {
sendResponse({ success: false, error: "Invalid reminder time" });
return;
}
try {
chrome.alarms.create(contestName, { when: reminderTime });
chrome.storage.local.set({ [contestName]: reminderTime }, () => {
if (chrome.runtime.lastError) {
sendResponse({ success: false, error: chrome.runtime.lastError.message });
} else {
sendResponse({ success: true });
}
});
} catch (error) {
sendResponse({ success: false, error: "Failed to set reminder" });
}
return true;
}
if (request.action === "clearReminder") {
const { contestName } = request;
if (!contestName || typeof contestName !== 'string') {
sendResponse({ success: false, error: "Invalid contest name" });
return;
}
try {
chrome.alarms.clear(contestName);
chrome.storage.local.remove(contestName, () => {
sendResponse({ success: true });
});
} catch (error) {
sendResponse({ success: false, error: "Failed to clear reminder" });
}
return true;
}
// Default: Unknown action
sendResponse({ success: false, error: "Unknown action" });
});