-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
94 lines (80 loc) · 2.73 KB
/
popup.js
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
85
86
87
88
89
90
91
92
93
94
document.addEventListener('DOMContentLoaded', () => {
const addGoalBtn = document.getElementById('addGoal');
const goalsList = document.getElementById('goals');
// Load existing goals
loadGoals();
addGoalBtn.addEventListener('click', () => {
const title = document.getElementById('goalTitle').value;
const description = document.getElementById('goalDescription').value;
const deadline = document.getElementById('goalDeadline').value;
if (!title || !deadline) {
alert('Please fill in all required fields');
return;
}
const goal = {
id: Date.now(),
title,
description,
deadline,
completed: false
};
saveGoal(goal);
loadGoals();
resetForm();
});
function saveGoal(goal) {
chrome.storage.sync.get(['goals'], (result) => {
const goals = result.goals || [];
goals.push(goal);
chrome.storage.sync.set({ goals });
// Set alarm for reminder
chrome.alarms.create(`goal-${goal.id}`, {
when: new Date(goal.deadline).getTime()
});
});
}
function loadGoals() {
chrome.storage.sync.get(['goals'], (result) => {
const goals = result.goals || [];
displayGoals(goals);
});
}
function displayGoals(goals) {
goalsList.innerHTML = '';
goals.forEach(goal => {
const goalElement = document.createElement('div');
goalElement.className = 'goal-item';
goalElement.innerHTML = `
<div>
<strong>${goal.title}</strong>
<p>${goal.description}</p>
<small>Due: ${new Date(goal.deadline).toLocaleString()}</small>
</div>
<span class="delete-btn" data-id="${goal.id}">×</span>
`;
goalsList.appendChild(goalElement);
});
// Add delete handlers
document.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
const goalId = parseInt(e.target.dataset.id);
deleteGoal(goalId);
});
});
}
function deleteGoal(goalId) {
chrome.storage.sync.get(['goals'], (result) => {
const goals = result.goals || [];
const updatedGoals = goals.filter(goal => goal.id !== goalId);
chrome.storage.sync.set({ goals: updatedGoals });
// Remove alarm
chrome.alarms.clear(`goal-${goalId}`);
loadGoals();
});
}
function resetForm() {
document.getElementById('goalTitle').value = '';
document.getElementById('goalDescription').value = '';
document.getElementById('goalDeadline').value = '';
}
});