-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
144 lines (129 loc) · 4.05 KB
/
index.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const memories = require('./src/memories');
async function main() {
const container = document.querySelector('body');
const timelist = await getTimelist(container);
return timelist;
}
function getTimelist(c) {
return c.querySelectorAll('time');
}
// Mengatur waktu standar menjadi waktu moment
function getMemories(options = {}, prefix = String, format = String) {
const moment = new memories();
moment.set('isDebug', false);
moment.set('datetime', options.datetime);
moment.set('language', options.language);
moment.set('locale', options.locale);
if (prefix != null) {
return moment.timeAgo(
prefix,
format !== null && format == 'id-ID'
? {
y: 'tahun',
m: 'bulan',
d: 'hari',
h: 'jam',
i: 'menit',
s: 'detik',
n: 'baru saja lahir',
}
: ''
);
}
return moment.timeAgo(
'timeAgo',
format !== null && format == 'id-ID'
? {
y: 'tahun yang lalu',
m: 'bulan yang lalu',
d: 'hari yang lalu',
h: 'jam yang lalu',
i: 'menit yang lalu',
s: 'detik yang lalu',
n: 'baru saja',
}
: ''
);
}
// Expired
function getExpired(options = {}) {
const moment = new memories(options);
moment.set('isDebug', false);
moment.set('datetime', options.datetime);
moment.set('language', options.language);
moment.set('locale', options.locale);
return moment.expired('year|month|day|hour|minute|second');
}
// Schedule
function getSchedule(options = {}) {
const moment = new memories(options);
moment.set('isDebug', false);
moment.set('datetime', options.datetime);
moment.set('language', options.language);
moment.set('locale', options.locale);
return moment.schedule('year|month|day|hour|minute|second');
}
// Countdown
function getCountdown(options) {
const moment = new memories(options);
moment.set('isDebug', false);
moment.set('datetime', options.datetime);
moment.set('language', options.language);
moment.set('locale', options.locale);
return moment.countdown();
}
let refresh = setInterval(() => {
main().then((timelist) => {
if (typeof timelist == 'object') {
timelist.forEach((time) => {
const prefix = time.getAttribute('prefix');
const language = time.getAttribute('lang');
const format = time.getAttribute('format');
const options = {
datetime: time.getAttribute('datetime').toString().replace(',', ''),
language: language !== null ? language : 'id-ID',
locale: {
timeZone:
language !== null && language == 'en-US'
? 'Europe/London'
: 'Asia/Jakarta',
hour12: false,
},
};
if (prefix == 'expired') {
let statusExpired = getExpired(options);
if (statusExpired) {
return (time.innerHTML = '<b>sudah</b>');
} else {
return (time.innerHTML = '<b>belum</b>');
}
} else if (prefix == 'schedule') {
let status = getSchedule(options);
// if (status == undefined) clearInterval(refresh);
if (status) {
return (time.innerHTML = '<b>sudah dimulai</b>');
} else {
return (time.innerHTML = '<b>belum dimulai</b>');
}
} else if (prefix == 'countdown') {
let status = getCountdown(options);
if (status.distance < 0) {
// clearInterval(refresh)
return (time.innerHTML = `<b>sudah berakhir</b>`);
} else {
return (time.innerHTML = `<b>${status.day}, ${status.hour} : ${status.minute} : ${status.second}</b>`);
}
} else if (prefix == 'birthday') {
time.innerHTML = getMemories(options, 'birthday', format);
} else {
time.innerHTML = getMemories(options, prefix, format);
}
});
}
});
}, 20);
module.exports = {
ref: (prop = Object | Array | undefined, isDebug = false) => {
return new memories(prop, isDebug);
},
};