-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
32 lines (28 loc) · 988 Bytes
/
content.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
const updateTimestamps = () => {
const nodes = Array.from(document.getElementsByTagName('relative-time'));
for (const node of nodes) {
const date = new Date(node.getAttribute('datetime'));
const now = new Date();
const isWithinOneDay = ((now - date) < 1000 * 60 * 60 * 24);
if (isWithinOneDay) {
continue;
}
const datetimeStr = date.toLocaleString('ja-JP', { timeZone: 'JST' });
if (node.innerHTML !== datetimeStr) {
node.innerHTML = datetimeStr;
}
if (node.shadowRoot && node.shadowRoot.innerHTML !== datetimeStr) {
node.shadowRoot.innerHTML = datetimeStr;
}
}
};
// 初回実行
updateTimestamps();
// MutationObserverの設定
const observer = new MutationObserver((mutations) => {
const shouldUpdate = mutations.some(mutation => mutation.type === 'childList' || mutation.type === 'subtree');
if (shouldUpdate) {
updateTimestamps();
}
});
observer.observe(document.body, { childList: true, subtree: true });