Files

62 lines
2.2 KiB
JavaScript

const el = id => document.getElementById(id)
const highlight = (text, terms) => {
if (!terms || terms.length === 0) return text
let s = text
terms.forEach((t, i) => {
const cls = `kw-${i % 6}`
const re = new RegExp(`(${t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi')
s = s.replace(re, `<mark class="${cls}">$1</mark>`)
})
return s
}
const fmtCNDateTime = (d) => {
const date = d instanceof Date ? d : new Date()
try {
const s = new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }).format(date)
return s.replace(/\//g, '-').replace(/\u200E/g, '')
} catch {
const pad = n => String(n).padStart(2, '0')
const y = date.getFullYear(), m = pad(date.getMonth()+1), day = pad(date.getDate()), hh = pad(date.getHours()), mm = pad(date.getMinutes()), ss = pad(date.getSeconds())
return `${y}-${m}-${day} ${hh}:${mm}:${ss}`
}
}
const fmtCNSimpleTime = (d) => {
const date = d instanceof Date ? d : new Date()
try {
const s = new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', hour: '2-digit', minute: '2-digit', hour12: false }).format(date)
return s.replace(/\u200E/g, '')
} catch {
const pad = n => String(n).padStart(2, '0')
return `${pad(date.getHours())}:${pad(date.getMinutes())}`
}
}
const cnDateStr = (d) => {
const date = d instanceof Date ? d : new Date()
try {
const s = new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', year: 'numeric', month: '2-digit', day: '2-digit' }).format(date)
return s.replace(/\//g, '-').replace(/\u200E/g, '')
} catch {
const pad = n => String(n).padStart(2, '0')
return `${date.getFullYear()}-${pad(date.getMonth()+1)}-${pad(date.getDate())}`
}
}
window.fmtCNDateTime = fmtCNDateTime
window.fmtCNSimpleTime = fmtCNSimpleTime
window.cnDateStr = cnDateStr
;(function handleSetToken() {
try {
const u = new URL(window.location.href)
const token = u.searchParams.get('set_token')
if (token) {
localStorage.setItem('toolbox_creds_token', token)
u.searchParams.delete('set_token')
window.history.replaceState(null, '', u.toString())
}
} catch {}
})()