-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
123 lines (116 loc) · 3.56 KB
/
options.js
File metadata and controls
123 lines (116 loc) · 3.56 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const ROOT = document.querySelector(":root");
const DIGITS = document.getElementsByClassName("digit-selector");
const STATUS = document.querySelector(".status");
const WHEELS = [0, 0, 0, 0, 0, 0];
let active_digit = 0;
const showStatusMessage = (message) => {
STATUS.innerHTML = message.repeat(10);
STATUS.classList.add("status-show");
setTimeout(() => {
STATUS.classList.remove("status-show");
}, 2000);
};
const toDateStringUS = (dateStringIN) => {
const [day, month, year] = dateStringIN.split("/");
return `${month}/${day}/${year}`;
};
const toDateStringIN = (date) => {
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let month =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
let year = date.getFullYear() % 100;
year = year < 10 ? "0" + year : year;
return `${day}/${month}/${year}`;
};
const isDateValid = (dateStringIN) => {
date = new Date(toDateStringUS(dateStringIN));
if (isNaN(date.getTime())) return false;
if (
date.getFullYear() < 1900 ||
date.getFullYear() > new Date().getFullYear()
)
return false;
if (
date.getFullYear() == new Date().getFullYear() &&
date.getMonth() > new Date().getMonth()
)
return false;
if (
date.getFullYear() == new Date().getFullYear() &&
date.getMonth() == new Date().getMonth() &&
date.getDate() > new Date().getDate()
)
return false;
return true;
};
chrome.storage.sync.get("MY_PAGE_DOB", (data) => {
const MYDOB = data.MY_PAGE_DOB;
if (MYDOB) {
let ageString = MYDOB.split("/").join("");
for (let i = 0; i < 6; i++) {
let digit = ageString[i];
setTimeout(() => {
ROOT.style.setProperty(`--digit-${i}`, digit);
}, 1);
WHEELS[i] = digit;
}
}
});
document.addEventListener("keydown", function (event) {
if (event.key === "ArrowRight") {
DIGITS[active_digit++].classList.remove("digit-selector-active");
active_digit = active_digit % 6;
DIGITS[active_digit].classList.add("digit-selector-active");
}
if (event.key === "ArrowLeft") {
DIGITS[active_digit--].classList.remove("digit-selector-active");
active_digit = active_digit < 0 ? 5 : active_digit;
DIGITS[active_digit].classList.add("digit-selector-active");
}
if (event.key === "Tab") {
event.preventDefault();
}
if (event.key === "ArrowUp") {
event.preventDefault();
let value = Number(
getComputedStyle(ROOT).getPropertyValue(`--digit-${active_digit}`)
);
let digitValue;
if (++value >= 0) digitValue = value % 10;
else digitValue = value == -10 ? 0 : (value % 10) + 10;
WHEELS[active_digit] = digitValue;
ROOT.style.setProperty(`--digit-${active_digit}`, value);
}
if (event.key === "ArrowDown") {
event.preventDefault();
let value = Number(
getComputedStyle(ROOT).getPropertyValue(`--digit-${active_digit}`)
);
let digitValue;
if (--value < 0) digitValue = value == -10 ? 0 : (value % 10) + 10;
else digitValue = value % 10;
WHEELS[active_digit] = digitValue;
ROOT.style.setProperty(`--digit-${active_digit}`, value);
}
if (event.key === "Enter") {
let dob = "";
for (let i = 0; i < 6; i++) {
dob += WHEELS[i];
if (i & 1 && i != 5) dob += "/";
}
if (!isDateValid(dob)) {
showStatusMessage("Error");
} else {
chrome.storage.sync.set({ "MY_PAGE_DOB": dob });
showStatusMessage("Saved");
setTimeout(() => {
history.back();
if (history.length <= 1) {
window.open("index.html", "_self");
}
}, 1500);
}
}
});