-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
70 lines (57 loc) · 2.41 KB
/
popup.js
File metadata and controls
70 lines (57 loc) · 2.41 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
59
60
61
62
63
64
65
66
67
68
69
70
// Fetch and display an affirmation quote without try-catch
function fetchAffirmation() {
fetch("https://www.affirmations.dev/")
.then(response => response.json())
.then(data => {
document.getElementById("quote").textContent = data.affirmation;
})
.catch(error => {
document.getElementById("quote").textContent = "Could not load affirmation.";
console.error("Error fetching affirmation:", error);
});
}
// Save today's mood with date and time to Chrome storage
document.getElementById("saveMood").addEventListener("click", () => {
const mood = document.getElementById("mood").value;
const timestamp = new Date().toLocaleString();
// Retrieve the current mood history array
chrome.storage.sync.get("moodHistory", (data) => {
const moodHistory = data.moodHistory || []; // Initialize to empty array if undefined
moodHistory.push({ mood, timestamp }); // Append the new entry
// Save updated moodHistory back to storage
chrome.storage.sync.set({ moodHistory }, () => {
alert(`Mood saved: ${mood}`);
});
});
});
// View mood history
document.getElementById("viewHistory").addEventListener("click", () => {
chrome.storage.sync.get("moodHistory", (data) => {
const moodHistory = data.moodHistory || [];
const historyList = document.getElementById("historyList");
historyList.innerHTML = ''; // Clear previous list
if (moodHistory.length === 0) {
historyList.innerHTML = "<li>No moods saved yet.</li>";
} else {
moodHistory.forEach(entry => {
const listItem = document.createElement("li");
listItem.textContent = `${entry.timestamp}: ${entry.mood}`;
historyList.appendChild(listItem);
});
}
// Toggle visibility of mood history
document.getElementById("moodHistory").classList.toggle("hidden");
});
});
// Clear Mood History
document.getElementById("clearHistory").addEventListener("click", ()=> {
chrome.storage.sync.remove("moodHistory", ()=>{
const historyList = document.getElementById("historyList");
historyList.innerHTML = "<li> No moods saved yet! </li>"
alert("Mood history cleared");
})
})
// Initialize features when popup opens
document.addEventListener("DOMContentLoaded", () => {
fetchAffirmation();
});