-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (50 loc) · 1.87 KB
/
Copy pathscript.js
File metadata and controls
60 lines (50 loc) · 1.87 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
(() => {
// Cache DOM references once
const setupDiv = document.getElementById("setup");
const containerDiv = document.getElementById("container");
const ageElem = document.getElementById("age");
const dateInput = document.getElementById("dobDate");
const timeInput = document.getElementById("dobTime");
const saveBtn = document.getElementById("save");
// Start the age calculation loop
const startAgeCalculation = (birthday) => {
containerDiv.style.display = "block";
setupDiv.style.display = "none";
const birthdayMs = birthday.getTime(); // cache timestamp
const msPerYear = 1000 * 60 * 60 * 24 * 365.2425;
const updateAge = () => {
const ageYears = (Date.now() - birthdayMs) / msPerYear;
ageElem.textContent = ageYears.toFixed(9);
requestAnimationFrame(updateAge);
};
updateAge();
};
// Load DOB from storage safely
const loadDOB = () => {
if (chrome?.storage?.sync) {
chrome.storage.sync.get(["dobDate", "dobTime"], (result) => {
const { dobDate, dobTime } = result || {};
if (!dobDate || !dobTime) {
setupDiv.style.display = "block";
containerDiv.style.display = "none";
saveBtn.addEventListener("click", () => {
const date = dateInput.value;
const time = timeInput.value;
if (!date || !time) return alert("Please fill both fields.");
chrome.storage.sync.set({ dobDate: date, dobTime: time }, () => {
startAgeCalculation(new Date(`${date}T${time}Z`));
});
});
} else {
startAgeCalculation(new Date(`${dobDate}T${dobTime}Z`));
}
});
} else {
// fallback if storage is unavailable
containerDiv.style.display = "block";
setupDiv.style.display = "none";
ageElem.textContent = "Storage unavailable";
}
};
loadDOB();
})();