MarseyWorld/files/assets/js/new_comments.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

const comments = JSON.parse(localStorage.getItem("comment-counts")) || {}
let pid
let pcc
for (let twoattrs of document.getElementsByClassName("twoattrs")) {
twoattrs = twoattrs.value.split(',')
pid = twoattrs[0]
pcc = twoattrs[1]
const lastCount = comments[pid]
if (lastCount) {
2023-08-30 09:08:02 +00:00
const title = document.getElementById(`${pid}-title`)
if (title) title.classList.add('visited')
const newComments = pcc - lastCount.c
if (newComments > 0) {
2023-02-18 20:55:39 +00:00
const elems = document.getElementsByClassName(`${pid}-new-comments`)
for (const elem of elems)
{
elem.textContent = ` (+${newComments})`
elem.classList.remove("d-none")
}
}
}
}
const LAST_CACHE_CLEAN_ID = "last-cache-clean"
const EXPIRE_INTERVAL_MILLIS = 5 * 24 * 60 * 60 * 1000
const CACHE_CLEAN_INTERVAL = 60 * 60 * 1000
function cleanCache() {
const lastCacheClean = JSON.parse(localStorage.getItem(LAST_CACHE_CLEAN_ID)) || Date.now()
const now = Date.now()
if (now - lastCacheClean > CACHE_CLEAN_INTERVAL) {
const comments = JSON.parse(localStorage.getItem("comment-counts")) || {}
for (let [key, value] of Object.entries(comments)) {
if (now - value.t > EXPIRE_INTERVAL_MILLIS) {
delete comments[key]
}
}
localStorage.setItem("comment-counts", JSON.stringify(comments))
}
localStorage.setItem(LAST_CACHE_CLEAN_ID, JSON.stringify(now))
}
setTimeout(cleanCache, 500)