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-
431// Function to show the notification
442function showNotification ( title , message ) {
453 const options = {
464 type : "basic" ,
475 iconUrl : "res/notify_icon.png" ,
486 title : title ,
49- message : message || "No Contest Available"
7+ message : message || "No Contest Available" ,
8+ buttons : [
9+ { title : "Turn Off Notification" } ,
10+ { title : "Visit Leetcode" }
11+ ]
5012 } ;
5113
5214 // chrome.notifications.create("", options);
@@ -86,25 +48,29 @@ function fetchNotificationContent() {
8648 // const message = JSON.stringify(data);
8749 // console.log(message);
8850 // showNotification(message);
51+ const title = `CrackTech Contest Notification` ;
52+ let contestCount = Object . keys ( data ) . length ; // Count the number of key-value pairs
53+ const currentTime = Date . now ( ) / 1000 ; // Current time in seconds
8954
90- const contestCount = Object . keys ( data ) . length ; // Count the number of key-value pairs
55+ Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
56+ const start_time = value . start_time ;
9157
92- let notificationMessage = '' ;
93- let title = `Upcoming Leetcode Contest - ${ contestCount } \n` ;
94-
95- Object . entries ( data ) . forEach ( ( [ key , value ] , index ) => {
96- const { title, start_time, contest_duration } = value ;
97- const startTimeString = convertEpochToDateString ( start_time ) ;
98- const durationString = convertDurationToString ( contest_duration ) ;
99-
100- notificationMessage += `${ index + 1 } . ${ title } - ${ startTimeString } for ${ durationString } hr.\n` ;
58+ if ( start_time < currentTime ) {
59+ contestCount -- ;
60+ }
10161 } ) ;
102-
103- showNotification ( title , notificationMessage ) ;
62+ if ( contestCount > 0 ) {
63+ const notificationMessage = `You have ${ contestCount } contest(s) coming up soon. Get ready!!` ;
64+ showNotification ( title , notificationMessage ) ;
65+ }
66+ else {
67+ const notificationMessage = `No Upcoming Contest(s).` ;
68+ showNotification ( title , notificationMessage ) ;
69+ }
10470 } )
10571 . catch ( error => {
106- console . error ( "Error fetching notification content:" , error ) ;
107- showNotification ( ) ; // Show the notification with default values in case of an error
72+ console . log ( "Error fetching notification content:" , error ) ;
73+ // showNotification(); // Show the notification with default values in case of an error
10874 } ) ;
10975}
11076
@@ -123,10 +89,15 @@ chrome.runtime.onInstalled.addListener(function () {
12389
12490// Function to schedule the first notification
12591function scheduleFirstNotification ( ) {
126- if ( ! firstNotificationScheduled ) {
127- chrome . alarms . create ( "firstNotificationAlarm" , { delayInMinutes : 2 } ) ;
128- firstNotificationScheduled = true ;
129- }
92+ chrome . storage . local . get ( "switchState" , function ( result ) {
93+ const switchState = result . switchState ;
94+ console . log ( 'Hello' ) ; console . log ( switchState ) ;
95+ if ( switchState ) {
96+ // fetchNotificationContent();
97+ chrome . alarms . create ( "firstNotificationAlarm" , { delayInMinutes : 1 } ) ; // Trigger every day
98+ firstNotificationScheduled = true ;
99+ }
100+ } ) ;
130101}
131102
132103// chrome.tabs.onCreated.addListener(function(tab) {
@@ -138,31 +109,31 @@ chrome.alarms.onAlarm.addListener(function (alarm) {
138109 if ( alarm . name === "firstNotificationAlarm" ) {
139110 fetchNotificationContent ( ) ;
140111 scheduleSecondNotification ( ) ;
112+ // firstNotificationScheduled = false; // Set the flag to false after the first notification
141113 } else if ( alarm . name === "secondNotificationAlarm" ) {
142114 fetchNotificationContent ( ) ;
143115 }
144116} ) ;
145117
146118// Function to schedule the second notification
147119function scheduleSecondNotification ( ) {
148- chrome . alarms . create ( "secondNotificationAlarm" , { delayInMinutes : 18 } ) ;
120+ chrome . alarms . create ( "secondNotificationAlarm" , { delayInMinutes : 15 } ) ;
149121}
150122
151- // Function to show the popup with notification ID
152- function showPopup ( notificationId ) {
153- const popupUrl = chrome . runtime . getURL ( `content.html?id=${ notificationId } ` ) ;
154- chrome . windows . create ( { url : popupUrl , type : "popup" , width : 400 , height : 200 } ) ;
155- }
123+ // // Event listener for notification click
124+ // chrome.notifications.onClicked.addListener(function (notificationId) {
125+ // if (notificationId.startsWith("cracktech_notification_")) {
126+ // chrome.tabs.create({ url: "https://leetcode.com/contest/" });
127+ // }
128+ // });
156129
157- // Event listener for notification click
158- chrome . notifications . onClicked . addListener ( function ( notificationId ) {
159- // Perform actions based on the clicked notification
130+ // Event listener for notification button clicks
131+ chrome . notifications . onButtonClicked . addListener ( function ( notificationId , buttonIndex ) {
160132 if ( notificationId . startsWith ( "cracktech_notification_" ) ) {
161- // Extract the associated notification ID
162- const notificationIdNumber = Number ( notificationId . split ( "_" ) [ 2 ] ) ;
163-
164- // Perform actions based on the notification ID
165- // For example, show a popup with the notification details
166- showPopup ( notificationIdNumber ) ;
133+ if ( buttonIndex === 0 ) {
134+ chrome . storage . local . set ( { switchState : false } ) ;
135+ } else if ( buttonIndex === 1 ) {
136+ chrome . tabs . create ( { url : "https://leetcode.com/contest/" } ) ;
137+ }
167138 }
168- } ) ;
139+ } ) ;
0 commit comments