diff options
Diffstat (limited to 'site/assets/site.js')
-rw-r--r-- | site/assets/site.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/site/assets/site.js b/site/assets/site.js new file mode 100644 index 0000000..4e72511 --- /dev/null +++ b/site/assets/site.js | |||
@@ -0,0 +1,32 @@ | |||
1 | function settime() { | ||
2 | const timestamp = document.querySelector('[data-timestamp-text]') | ||
3 | if (!timestamp || !('Intl' in window)) return | ||
4 | |||
5 | const options = { | ||
6 | timeZone: "Asia/Taipei", | ||
7 | timeStyle: "short", | ||
8 | hour12: false | ||
9 | } | ||
10 | |||
11 | // https://gist.github.com/muan/e7414b6241f088090acd916ed965540e | ||
12 | let time = new Intl.DateTimeFormat(navigator.language || "zh-TW", options).format(new Date()) | ||
13 | |||
14 | // https://bugs.chromium.org/p/chromium/issues/detail?id=1262801 | ||
15 | if (time.match(/^24:/)) time = time.replace('24:', '00:') | ||
16 | |||
17 | // Setting interpolated string instead of just the time because | ||
18 | // if there's no JS there should be no mentions of current time | ||
19 | const text = timestamp.getAttribute('data-timestamp-text').replace('{time}', time) | ||
20 | timestamp.innerHTML = text.replace(':', '<span class="timestamp-colon" data-colon>:</span>') | ||
21 | |||
22 | const now = new Date() | ||
23 | const sec = now.getSeconds() | ||
24 | const secondIsEven = sec % 2 === 0 | ||
25 | const colon = document.querySelector('[data-colon]') | ||
26 | if (colon) colon.style.animationDelay = `${(secondIsEven ? 0 : 1000) - now.getMilliseconds()}ms` | ||
27 | |||
28 | const delay = 60000 - ((sec * 1000) + now.getMilliseconds()) | ||
29 | setTimeout(settime, delay) | ||
30 | } | ||
31 | |||
32 | settime() | ||