Skip to content

Commit 44fd83f

Browse files
committed
[v1.0.6] Notification Feature Added
1 parent aafef5d commit 44fd83f

File tree

3 files changed

+158
-3
lines changed

3 files changed

+158
-3
lines changed

chrome/background.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// // Example event listener for when a new tab is opened or when the user visits leetcode.com
2+
// chrome.tabs.onCreated.addListener(checkNotification);
3+
// chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
4+
// if (changeInfo.status === 'complete' && tab.url.includes('leetcode.com')) {
5+
// checkNotification(tab);
6+
// }
7+
// });
8+
9+
// function checkNotification(tab) {
10+
// // Check if the last notification was sent more than 24 hours ago
11+
// chrome.storage.local.get('lastNotificationSent', result => {
12+
// let lastNotificationSent = result.lastNotificationSent;
13+
// lastNotificationSent = false;
14+
// console.log(lastNotificationSent);
15+
16+
// // Check if the last notification exists and if it is older than 24 hours
17+
// // if (!lastNotificationSent || (Date.now() - lastNotificationSent) >= 24 * 60 * 60 * 1000) {
18+
// // if (!lastNotificationSent) {
19+
// // Check if the user is on leetcode.com
20+
// // if (tab.url.includes('leetcode.com')) {
21+
// // Fetch the JSON data and send the notification
22+
// fetch('https://raw.githubusercontent.com/ssavi-ict/LeetCode-Which-Company/main/data/contests.json')
23+
// .then(response => response.json())
24+
// .then(data => {
25+
// const jsonData = JSON.stringify(data);
26+
// chrome.notifications.create('notificationId', {
27+
// type: 'basic',
28+
// title: 'Notification Title',
29+
// message: jsonData,
30+
// iconUrl: 'res/notify_icon.png'
31+
// });
32+
// chrome.storage.local.set({ lastNotificationSent: Date.now() });
33+
// })
34+
// .catch(error => {
35+
// // Handle any errors that occurred during the fetch request
36+
// console.log('Error Occured');
37+
// });
38+
// // }
39+
// // }
40+
// });
41+
// }
42+
43+
// Function to show the notification
44+
function showNotification(title, message) {
45+
const options = {
46+
type: "basic",
47+
iconUrl: "res/notify_icon.png",
48+
title: title,
49+
message: message || "No Contest Available"
50+
};
51+
52+
chrome.notifications.create("", options);
53+
}
54+
55+
// Helper function to convert epoch to date string
56+
function convertEpochToDateString(epoch) {
57+
const date = new Date(epoch * 1000);
58+
const options = { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone };
59+
const dateTimeString = date.toLocaleString(undefined, options);
60+
return dateTimeString;
61+
}
62+
63+
// Helper function to convert duration in seconds to hours and minutes string
64+
function convertDurationToString(durationInSeconds) {
65+
const hours = Math.floor(durationInSeconds / 3600);
66+
const minutes = Math.floor((durationInSeconds % 3600) / 60);
67+
68+
let durationString = '';
69+
if (hours > 0) {
70+
durationString += `${hours}:`;
71+
}
72+
if (minutes > 0) {
73+
durationString += `${hours > 0 ? '' : ''}${minutes}`;
74+
}
75+
76+
return durationString;
77+
}
78+
79+
function fetchNotificationContent() {
80+
fetch("https://raw.githubusercontent.com/ssavi-ict/LeetCode-Which-Company/main/data/contests.json")
81+
.then(response => response.json())
82+
.then(data => {
83+
// const message = JSON.stringify(data);
84+
// console.log(message);
85+
// showNotification(message);
86+
87+
const contestCount = Object.keys(data).length; // Count the number of key-value pairs
88+
89+
let notificationMessage = '';
90+
let title = `Upcoming Leetcode Contest - ${contestCount}\n`;
91+
92+
Object.entries(data).forEach(([key, value], index) => {
93+
const { title, start_time, contest_duration } = value;
94+
const startTimeString = convertEpochToDateString(start_time);
95+
const durationString = convertDurationToString(contest_duration);
96+
97+
notificationMessage += `${index + 1}. ${title} - ${startTimeString} for ${durationString} hr.\n`;
98+
});
99+
100+
showNotification(title, notificationMessage);
101+
})
102+
.catch(error => {
103+
console.error("Error fetching notification content:", error);
104+
showNotification(); // Show the notification with default values in case of an error
105+
});
106+
}
107+
108+
// Variable to track the first notification
109+
let firstNotificationScheduled = false;
110+
111+
// Event listener for the browser startup
112+
chrome.runtime.onStartup.addListener(function () {
113+
scheduleFirstNotification();
114+
});
115+
116+
// Event listener for the browser installation/upgrade
117+
chrome.runtime.onInstalled.addListener(function () {
118+
scheduleFirstNotification();
119+
});
120+
121+
// Function to schedule the first notification
122+
function scheduleFirstNotification() {
123+
if (!firstNotificationScheduled) {
124+
chrome.alarms.create("firstNotificationAlarm", { delayInMinutes: 2 });
125+
firstNotificationScheduled = true;
126+
}
127+
}
128+
129+
// chrome.tabs.onCreated.addListener(function(tab) {
130+
// fetchNotificationContent();
131+
// });
132+
133+
// Event listener for the alarm trigger
134+
chrome.alarms.onAlarm.addListener(function (alarm) {
135+
if (alarm.name === "firstNotificationAlarm") {
136+
fetchNotificationContent();
137+
scheduleSecondNotification();
138+
} else if (alarm.name === "secondNotificationAlarm") {
139+
fetchNotificationContent();
140+
}
141+
});
142+
143+
// Function to schedule the second notification
144+
function scheduleSecondNotification() {
145+
chrome.alarms.create("secondNotificationAlarm", { delayInMinutes: 18 });
146+
}

chrome/manifest.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "CrackTech - Find LeetCode Company Tags",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"homepage_url":"https://github.com/ssavi-ict/LC-Which-Company",
55
"description": "An extension that can help candidate to prepare for a Company specific DSA interview.",
66
"manifest_version": 3,
@@ -10,7 +10,16 @@
1010
"default_title": "CrackTech - Find LeetCode Company Tags"
1111
},
1212
"icons": {
13-
"32": "res/32.png"
13+
"32": "res/32.png",
14+
"16": "res/16.png",
15+
"48": "res/48.png",
16+
"128": "res/128.png"
1417
},
15-
"permissions": ["tabs"]
18+
"background": {
19+
"service_worker": "background.js"
20+
},
21+
"permissions": ["tabs", "notifications", "alarms"],
22+
"host_permissions": [
23+
"<all_urls>"
24+
]
1625
}

chrome/res/notify_icon.png

968 Bytes
Loading

0 commit comments

Comments
 (0)