Files
devbox/js/cat-time2.js
T
gitea 2adf88c45d 工具数 201 → 270:时间/代码/娱乐三批
- cat-time2.js:番茄钟、倒计时、煮蛋/厨房/小睡/会议/Tabata/平板/冥想/沙漏计时、秒表、双方钟、在线时钟、世界时钟、周数、年内天数、今年进度、下班倒计时、相对时间、跨时区会议、节假日倒计时
- cat-code.js:JSON 格式化与 diff、文本/代码行级 diff、JS/CSS/HTML 压缩、CSS/HTML/XML/YAML/SQL 美化、正则测试、cron 解释、chmod、cURL→代码、JWT、SHA、HMAC、TOTP、CRC32、校验比对
- cat-fun.js:抛硬币、8 号球、答案之书、废话/夸夸/鸡汤/毒鸡汤/土味情话、签语、幸运色、随机表情/书/电影/目的地、点名、默契度、星座/生肖/元素周期表、乘法表、口算、打字练习、词云、对阵表、循环赛程、投票统计
- 新增 fun 分组;全部 270 个 mount 通过
2026-11-20 14:54:08 +08:00

135 lines
13 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* DevBox · 时间工具(实时计时 / 日期推算) */
(function () {
'use strict'
const { el, panelTitle, textTool: T } = DB
const btn = (l, f, c = '') => el('button', { class: 'btn ' + c, text: l, onclick: f })
const pane = (t, r) => { const p = el('div', { class: 'panel' }); if (t) p.append(panelTitle(t, r)); return p }
const pad = (n) => String(n).padStart(2, '0')
/** 计时器工厂:给秒数与标签 */
const timer = (id, name, desc, presets, keywords) => DB.tool({
id, name, group: 'time', icon: 'clock', desc, keywords,
mount(root) {
let total = presets[0][1] * 60, left = total, run = false, iv = 0
const face = el('div', { class: 'big-time', text: `${pad(Math.floor(total / 60))}:00` })
const info = el('div', { class: 'hint', text: '选一个时长开始' })
const paint = () => { face.textContent = `${pad(Math.floor(left / 60))}:${pad(left % 60)}`; face.classList.toggle('run', run) }
const beep = () => { try { const c = new (window.AudioContext || window.webkitAudioContext)(); const o = c.createOscillator(); const g = c.createGain(); o.frequency.value = 880; g.gain.value = .2; o.connect(g); g.connect(c.destination); o.start(); setTimeout(() => { o.stop(); c.close() }, 400) } catch (e) {} }
const tick = () => { if (!run) return; left--; if (left <= 0) { left = 0; run = false; beep(); beep(); info.textContent = '时间到!' } paint() }
const start = (sec) => { if (sec) { total = left = sec } run = true; if (!iv) iv = setInterval(tick, 1000); paint() }
root.append(pane(name, el('div', { class: 'row' }, ...presets.map(([label, min]) => btn(label, () => { left = min * 60; info.textContent = `已设为 ${min} 分钟`; paint(); start() }, 'ghost')))),
face, info, el('div', { class: 'row' }, btn('开始 / 暂停', () => { run = !run; if (run && !iv) iv = setInterval(tick, 1000); paint() }), btn('重置', () => { run = false; left = total; paint() }, 'ghost')))
},
})
timer('pomodoro', '番茄钟', '25 分钟专注 + 5 分钟休息的经典节奏', [['专注 25 分', 25], ['短休 5 分', 5], ['长休 15 分', 15]], ['pomodoro', '番茄', '专注'])
timer('countdown-timer', '倒计时', '自定义分钟数,到点响铃', [['1 分钟', 1], ['5 分钟', 5], ['10 分钟', 10], ['30 分钟', 30], ['60 分钟', 60]], ['countdown', '倒计时'])
timer('egg-timer', '煮蛋计时', '溏心 6 分、半熟 8 分、全熟 10 分', [['溏心 6 分', 6], ['半熟 8 分', 8], ['全熟 10 分', 10]], ['煮蛋', 'egg', '计时'])
timer('kitchen-timer', '厨房计时', '炒菜、炖汤常用时长', [['3 分钟', 3], ['8 分钟', 8], ['15 分钟', 15], ['45 分钟', 45]], ['kitchen', '厨房', '炖'])
timer('nap-timer', '小睡定时', '20 分钟提神,90 分钟一个完整周期', [['20 分钟', 20], ['45 分钟', 45], ['90 分钟', 90]], ['nap', '午睡', '小睡'])
timer('meeting-timer', '会议计时', '防止会议拖堂,5/10/15 分钟提醒', [['5 分钟', 5], ['10 分钟', 10], ['25 分钟', 25]], ['meeting', '会议', '计时'])
timer('tabata-timer', 'Tabata 训练', '4 分钟高强度:20 秒练 10 秒歇 ×8', [['4 分钟', 4]], ['tabata', 'hiit', '训练'])
timer('plank-timer', '平板支撑', '30 秒到 2 分钟一组', [['30 秒', 0.5], ['1 分钟', 1], ['2 分钟', 2]], ['plank', '平板支撑'])
timer('meditation-timer', '冥想计时', '5 / 10 / 20 分钟静坐', [['5 分钟', 5], ['10 分钟', 10], ['20 分钟', 20]], ['meditation', '冥想'])
timer('sand-timer', '沙漏计时', '1 / 3 / 5 分钟,随手一个', [['1 分钟', 1], ['3 分钟', 3], ['5 分钟', 5]], ['sand', '沙漏'])
DB.tool({
id: 'stopwatch', name: '秒表', group: 'time', icon: 'clock', desc: '带分段记录,精确到 0.01 秒', keywords: ['stopwatch', '秒表', '计时'],
mount(root) {
let base = 0, start = 0, run = false, raf = 0
const face = el('div', { class: 'big-time', text: '00:00.00' })
const laps = el('div', { class: 'result-list' })
const paint = () => { const ms = base + (run ? performance.now() - start : 0)
face.textContent = `${pad(Math.floor(ms / 60000))}:${pad(Math.floor(ms / 1000) % 60)}.${pad(Math.floor(ms % 1000 / 10))}` }
const loop = () => { paint(); if (run) raf = requestAnimationFrame(loop) }
root.append(pane('秒表', el('div', { class: 'row' },
btn('开始 / 停', () => { if (run) { base += performance.now() - start; run = false; cancelAnimationFrame(raf) } else { run = true; start = performance.now(); loop() } paint() }),
btn('分段', () => { laps.prepend(el('div', { class: 'row between' }, el('span', { text: '第 ' + (laps.children.length + 1) + ' 段' }), el('span', { class: 'tag', text: face.textContent }))) }),
btn('清零', () => { base = 0; run = false; cancelAnimationFrame(raf); laps.textContent = ''; paint() }, 'ghost'))), face, laps)
},
})
DB.tool({
id: 'chess-clock', name: '国际象棋钟', group: 'time', icon: 'clock', desc: '双方各自计时,落子后点一下换边', keywords: ['chess', '计时', '对局'],
mount(root) {
const secs = [300, 300]
let turn = 0, run = false, iv = 0
const faces = [el('div', { class: 'big-time' }), el('div', { class: 'big-time' })]
const paint = () => faces.forEach((f, i) => { f.textContent = `${pad(Math.floor(secs[i] / 60))}:${pad(secs[i] % 60)}`; f.style.opacity = i === turn ? '1' : '.45' })
const tick = () => { if (!run) return; secs[turn]--; if (secs[turn] <= 0) { run = false; DB.toast(`第 ${turn + 1} 方超时`, true) } paint() }
const switchTurn = () => { turn = 1 - turn }
root.append(pane('双方计时', el('div', { class: 'row' }, btn('开始 / 暂停', () => { run = !run; if (run && !iv) iv = setInterval(tick, 1000) }), btn('换手', switchTurn), btn('重置', () => { secs[0] = secs[1] = 300; turn = 0; paint() }, 'ghost'))),
el('div', { class: 'grid2' }, faces[0], faces[1]))
paint()
},
})
DB.tool({
id: 'online-clock', name: '在线时钟', group: 'time', icon: 'clock', desc: '大字号时间 + 日期 + 时区,投屏、对表都行', keywords: ['clock', '时钟', '时间'],
mount(root) {
const face = el('div', { class: 'big-time', text: '' })
const sub = el('div', { class: 'hint' })
const paint = () => { const d = new Date()
face.textContent = `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
sub.textContent = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} 星期${'日一二三四五六'[d.getDay()]} · ${Intl.DateTimeFormat().resolvedOptions().timeZone}` }
paint(); setInterval(paint, 1000)
root.append(pane('时钟'), face, sub)
},
})
DB.tool({
id: 'world-clock', name: '世界时钟', group: 'time', icon: 'globe', desc: '十几个城市当前时间,开会前看一眼', keywords: ['world clock', '时区', '城市'],
mount(root) {
const out = el('pre', { class: 'out' })
const zones = [['北京', 'Asia/Shanghai'], ['东京', 'Asia/Tokyo'], ['首尔', 'Asia/Seoul'], ['新加坡', 'Asia/Singapore'], ['迪拜', 'Asia/Dubai'], ['莫斯科', 'Europe/Moscow'], ['柏林', 'Europe/Berlin'], ['巴黎', 'Europe/Paris'], ['伦敦', 'Europe/London'], ['纽约', 'America/New_York'], ['洛杉矶', 'America/Los_Angeles'], ['圣保罗', 'America/Sao_Paulo'], ['悉尼', 'Australia/Sydney'], ['奥克兰', 'Pacific/Auckland']]
const paint = () => { out.textContent = zones.map(([n, z]) => { const t = new Date().toLocaleTimeString('zh-CN', { timeZone: z, hour12: false })
const day = new Date().toLocaleDateString('zh-CN', { timeZone: z }).slice(5)
return `${n.padEnd(4, ' ')} ${t} ${day}` }).join('\n') }
paint(); setInterval(paint, 1000)
root.append(pane('世界时钟'), out)
},
})
T({ id: 'week-number', name: '第几周', group: 'time', icon: 'calendar', desc: '今天是今年的第几周、第几天', keywords: ['week', '周数', '第几周'], ph: '2026-11-20',
fn: (s) => { const d = new Date(s.trim() || Date.now()); if (isNaN(d)) throw new Error('日期格式不对,用 2026-11-20')
const start = new Date(d.getFullYear(), 0, 1); const days = Math.floor((d - start) / 86400000) + 1
const week = Math.ceil(((d - new Date(d.getFullYear(), 0, 1)) / 86400000 + new Date(d.getFullYear(), 0, 1).getDay() + 1) / 7)
const iso = (() => { const t = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); const day = t.getUTCDay() || 7; t.setUTCDate(t.getUTCDate() + 4 - day); const y0 = new Date(Date.UTC(t.getUTCFullYear(), 0, 1)); return Math.ceil(((t - y0) / 86400000 + 1) / 7) })()
return `日期:${d.toLocaleDateString('zh-CN')}\n今年第 ${days} 天\nISO 周数:第 ${iso} 周\n粗略周数:第 ${week} 周\n今年还剩 ${(d.getFullYear() % 4 === 0 && (d.getFullYear() % 100 !== 0 || d.getFullYear() % 400 === 0)) ? 366 - days : 365 - days} 天` } })
T({ id: 'day-of-year', name: '年内第几天', group: 'time', icon: 'calendar', desc: '给日期算出年内序号与倒计时天数', keywords: ['day of year', '年内', '倒计时'], ph: '2026-12-31',
fn: (s) => { const d = new Date(s.trim() || Date.now()); const start = new Date(d.getFullYear(), 0, 0)
return `日期:${d.toLocaleDateString('zh-CN')}\n年内第 ${Math.round((d - start) / 86400000)} 天\n距年末 ${Math.round((new Date(d.getFullYear(), 11, 31) - d) / 86400000)} 天` } })
T({ id: 'year-progress', name: '今年进度', group: 'time', icon: 'calendar', desc: '今年过了百分之多少、还剩几天', keywords: ['year progress', '进度', '今年'], ph: '(直接看)',
fn: () => { const now = new Date(); const y = now.getFullYear(); const total = (y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0)) ? 366 : 365
const passed = Math.floor((now - new Date(y, 0, 1)) / 86400000) + 1; const pct = passed / total * 100
const bar = '█'.repeat(Math.round(pct / 2.5)).padEnd(40, '░')
return `${y} 年已过 ${pct.toFixed(2)}%\n${bar}\n已过 ${passed} 天,还剩 ${total - passed} 天` } })
T({ id: 'offwork-countdown', name: '下班倒计时', group: 'time', icon: 'clock', desc: '离 18:00 还有多久,周末还有几天', keywords: ['下班', '倒计时', '摸鱼'], ph: '下班时间(如 18:00)',
fn: (s) => { const m = /(\d{1,2}):(\d{2})/.exec(s) || [0, '18', '00']; const now = new Date()
const target = new Date(now); target.setHours(+m[1], +m[2], 0, 0)
const diff = target - now; const h = Math.floor(diff / 3600000), mi = Math.floor(diff % 3600000 / 60000)
const toFri = (5 - now.getDay() + 7) % 7
return diff > 0 ? `离下班还有 ${h} 小时 ${mi} 分钟\n离周末还有 ${toFri === 0 ? '就是今天' : toFri + ' 天'}` : `已经过了下班时间 ${-h} 小时 ${-mi} 分钟,早点走` } })
T({ id: 'relative-title', name: '相对时间描述', group: 'time', icon: 'clock', desc: '把日期变成「3 天前」「2 小时后」', keywords: ['relative', '多久', '相对时间'], ph: '2026-11-18 09:30',
fn: (s) => { const d = new Date(s.trim()); if (isNaN(d)) throw new Error('格式:2026-11-18 09:30')
const diff = d - Date.now(); const abs = Math.abs(diff); const unit = abs < 60000 ? ['秒', 1000] : abs < 3600000 ? ['分钟', 60000] : abs < 86400000 ? ['小时', 3600000] : abs < 2592000000 ? ['天', 86400000] : ['个月', 2592000000]
const n = Math.round(abs / unit[1]); return `${diff < 0 ? n + ' ' + unit[0] + '前' : n + ' ' + unit[0] + '后'}\n绝对时间:${d.toLocaleString('zh-CN')}` } })
T({ id: 'timezone-meeting', name: '跨时区会议时间', group: 'time', icon: 'globe', desc: '给一个北京时间,换算各城市当地时间', keywords: ['时区', '会议', '换算'], ph: '14:00 或 2026-11-21 14:00',
fn: (s) => { const t = s.trim() || '14:00'; let base
if (/^\d{1,2}:\d{2}$/.test(t)) { const [h, m] = t.split(':'); base = new Date(); base.setHours(+h, +m, 0, 0) } else { base = new Date(t); if (isNaN(base)) throw new Error('时间格式不对') }
const zones = [['北京', 'Asia/Shanghai'], ['东京', 'Asia/Tokyo'], ['新加坡', 'Asia/Singapore'], ['迪拜', 'Asia/Dubai'], ['柏林', 'Europe/Berlin'], ['伦敦', 'Europe/London'], ['纽约', 'America/New_York'], ['洛杉矶', 'America/Los_Angeles']]
return zones.map(([n, z]) => { const str = base.toLocaleString('zh-CN', { timeZone: z, hour12: false, month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' })
return `${n.padEnd(4, ' ')} ${str}` }).join('\n') } })
T({ id: 'holiday-countdown', name: '节假日倒计时', group: 'time', icon: 'calendar', desc: '元旦、春节、五一、国庆还有多少天', keywords: ['节假日', '倒计时', '放假'], ph: '(直接看)',
fn: () => { const now = new Date(); const y = now.getFullYear()
const list = [['元旦', new Date(y + 1, 0, 1)], ['春节(除夕示意 2/10 前后,以官方为准)', new Date(y, 1, 10)], ['清明(4/4 前后)', new Date(y, 3, 4)], ['五一', new Date(y, 4, 1)], ['中秋(示意 9/25)', new Date(y, 8, 25)], ['国庆', new Date(y, 9, 1)], ['圣诞', new Date(y, 11, 25)]]
return list.map(([n, d]) => { let dd = d; if (dd < now) dd = new Date(dd.getFullYear() + 1, dd.getMonth(), dd.getDate())
return `${n}:${Math.ceil((dd - now) / 86400000)} 天(${dd.toLocaleDateString('zh-CN')})` }).join('\n') + '\n\n(法定调休以官方公布为准)' } })
})()