-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-tab.js
73 lines (61 loc) · 2.12 KB
/
new-tab.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
function createWeeksVisualization(birthday, lifespan) {
const weeksContainer = document.getElementById("weeks-container");
weeksContainer.innerHTML = "";
const today = new Date();
const birthDate = new Date(birthday);
const totalWeeks = lifespan * 52;
const livedWeeks = Math.floor(
(today - birthDate) / (7 * 24 * 60 * 60 * 1000),
);
for (let i = 0; i < totalWeeks; i++) {
const weekDot = document.createElement("div");
weekDot.className = "week";
if (i < livedWeeks) {
weekDot.classList.add("lived");
}
weeksContainer.appendChild(weekDot);
}
}
function updateWeeks() {
chrome.storage.local.get(["birthday", "lifespan"], function (result) {
const birthdayInput = document.getElementById("birthday");
const lifespanInput = document.getElementById("lifespan");
if (result.birthday) {
birthdayInput.value = result.birthday;
}
if (result.lifespan) {
lifespanInput.value = result.lifespan;
}
if (result.birthday && result.lifespan) {
createWeeksVisualization(result.birthday, result.lifespan);
} else {
document.getElementById("weeks-container").innerHTML =
"<p>Please set your birthday and expected lifespan.</p>";
}
});
}
function saveSettings() {
const birthday = document.getElementById("birthday").value;
const lifespan = document.getElementById("lifespan").value;
chrome.storage.local.set({ birthday, lifespan }, function () {
console.log("Settings saved");
updateWeeks();
});
}
function toggleTheme() {
document.body.classList.toggle("dark");
const isDark = document.body.classList.contains("dark");
chrome.storage.local.set({ darkTheme: isDark });
}
document.addEventListener("DOMContentLoaded", function () {
updateWeeks();
document.getElementById("save").addEventListener("click", saveSettings);
const themeCheckbox = document.getElementById("theme-checkbox");
themeCheckbox.addEventListener("change", toggleTheme);
chrome.storage.local.get("darkTheme", function (result) {
if (result.darkTheme) {
document.body.classList.add("dark");
themeCheckbox.checked = true;
}
});
});