|
| 1 | +// Function to show the notification |
| 2 | +function showNotification(title, message) { |
| 3 | + const options = { |
| 4 | + type: "basic", |
| 5 | + iconUrl: "res/notify_icon.png", |
| 6 | + title: title, |
| 7 | + message: message || "No Contest Available", |
| 8 | + buttons: [ |
| 9 | + { title: "Turn Off Notification" }, |
| 10 | + { title: "Visit Leetcode" } |
| 11 | + ] |
| 12 | + }; |
| 13 | + |
| 14 | + // chrome.notifications.create("", options); |
| 15 | + // Generate a unique notification ID |
| 16 | + const notificationId = `cracktech_notification_${Date.now()}`; |
| 17 | + chrome.notifications.create(notificationId, options); |
| 18 | +} |
| 19 | + |
| 20 | +// Helper function to convert epoch to date string |
| 21 | +function convertEpochToDateString(epoch) { |
| 22 | + const date = new Date(epoch * 1000); |
| 23 | + const options = { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone }; |
| 24 | + const dateTimeString = date.toLocaleString(undefined, options); |
| 25 | + return dateTimeString; |
| 26 | +} |
| 27 | + |
| 28 | +// Helper function to convert duration in seconds to hours and minutes string |
| 29 | +function convertDurationToString(durationInSeconds) { |
| 30 | + const hours = Math.floor(durationInSeconds / 3600); |
| 31 | + const minutes = Math.floor((durationInSeconds % 3600) / 60); |
| 32 | + |
| 33 | + let durationString = ''; |
| 34 | + if (hours > 0) { |
| 35 | + durationString += `${hours}:`; |
| 36 | + } |
| 37 | + if (minutes > 0) { |
| 38 | + durationString += `${hours > 0 ? '' : ''}${minutes}`; |
| 39 | + } |
| 40 | + |
| 41 | + return durationString; |
| 42 | +} |
| 43 | + |
| 44 | +function fetchNotificationContent() { |
| 45 | + fetch("https://raw.githubusercontent.com/ssavi-ict/LeetCode-Which-Company/main/data/contests.json") |
| 46 | + .then(response => response.json()) |
| 47 | + .then(data => { |
| 48 | + // const message = JSON.stringify(data); |
| 49 | + // console.log(message); |
| 50 | + // 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 |
| 54 | + |
| 55 | + Object.entries(data).forEach(([key, value]) => { |
| 56 | + const start_time = value.start_time; |
| 57 | + |
| 58 | + if (start_time < currentTime) { |
| 59 | + contestCount--; |
| 60 | + } |
| 61 | + }); |
| 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 | + } |
| 70 | + }) |
| 71 | + .catch(error => { |
| 72 | + console.log("Error fetching notification content:", error); |
| 73 | + // showNotification(); // Show the notification with default values in case of an error |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +// Variable to track the first notification |
| 78 | +let firstNotificationScheduled = false; |
| 79 | + |
| 80 | +// Event listener for the browser startup |
| 81 | +chrome.runtime.onStartup.addListener(function () { |
| 82 | + scheduleFirstNotification(); |
| 83 | +}); |
| 84 | + |
| 85 | +// Event listener for the browser installation/upgrade |
| 86 | +chrome.runtime.onInstalled.addListener(function () { |
| 87 | + scheduleFirstNotification(); |
| 88 | +}); |
| 89 | + |
| 90 | +// Function to schedule the first notification |
| 91 | +function scheduleFirstNotification() { |
| 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 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +// chrome.tabs.onCreated.addListener(function(tab) { |
| 104 | +// fetchNotificationContent(); |
| 105 | +// }); |
| 106 | + |
| 107 | +// Event listener for the alarm trigger |
| 108 | +chrome.alarms.onAlarm.addListener(function (alarm) { |
| 109 | + if (alarm.name === "firstNotificationAlarm") { |
| 110 | + fetchNotificationContent(); |
| 111 | + scheduleSecondNotification(); |
| 112 | + // firstNotificationScheduled = false; // Set the flag to false after the first notification |
| 113 | + } else if (alarm.name === "secondNotificationAlarm") { |
| 114 | + fetchNotificationContent(); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +// Function to schedule the second notification |
| 119 | +function scheduleSecondNotification() { |
| 120 | + chrome.alarms.create("secondNotificationAlarm", { delayInMinutes: 15 }); |
| 121 | +} |
| 122 | + |
| 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 | +// }); |
| 129 | + |
| 130 | +// Event listener for notification button clicks |
| 131 | +chrome.notifications.onButtonClicked.addListener(function (notificationId, buttonIndex) { |
| 132 | + if (notificationId.startsWith("cracktech_notification_")) { |
| 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 | + } |
| 138 | + } |
| 139 | +}); |
0 commit comments