-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground.js
More file actions
58 lines (53 loc) · 1.74 KB
/
background.js
File metadata and controls
58 lines (53 loc) · 1.74 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
// Set up the daily reset alarm at 12:00 a.m.
chrome.runtime.onInstalled.addListener(() => {
setMidnightAlarm();
});
chrome.runtime.onStartup.addListener(() => {
setMidnightAlarm();
});
// Handle alarms for resetting state
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "dailyReset") {
chrome.storage.local.set({ state: null }, () => {
console.log("State reset at 12:00 a.m.");
});
setMidnightAlarm(); // Reset the alarm for the next day
}
});
// Function to set an alarm for 12:00 a.m.
function setMidnightAlarm() {
const now = new Date();
const midnight = new Date();
midnight.setHours(24, 0, 0, 0);
const timeUntilMidnight = (midnight - now) / (1000 * 60);
chrome.alarms.create("dailyReset", { delayInMinutes: timeUntilMidnight });
}
// Handle messages from newTab.js for generating subtasks
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "generateSubtasks") {
// Call GPT wrapper
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer API_TOKEN", // tbu
},
body: JSON.stringify({
prompt: `Break down the task "${message.task}" into 5 subtasks.`,
max_tokens: 100,
}),
})
.then((response) => response.json())
.then((data) => {
const subtasks = data.choices[0].text
.trim()
.split("\n")
.filter(Boolean);
chrome.runtime.sendMessage({ action: "updateSubtasks", subtasks });
})
.catch((error) => {
console.error("Error generating subtasks:", error);
});
return true; // Keep the message channel open for async response
}
});