- 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 通过
108 lines
5.4 KiB
JavaScript
108 lines
5.4 KiB
JavaScript
/* DevBox · 通用工具工厂:一个输入 → 输出的轻量工具都从这儿生成 */
|
||
(function () {
|
||
'use strict'
|
||
const { el, panelTitle, copy, toast } = DB
|
||
const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn })
|
||
const pane = (title, right) => { const p = el('div', { class: 'panel' }); if (title) p.append(panelTitle(title, right)); return p }
|
||
|
||
DB.ta = (ph, rows) => el('textarea', { class: 'ta', placeholder: ph, rows: rows || 10, spellcheck: 'false' })
|
||
DB.btn = btn
|
||
DB.pane = pane
|
||
|
||
/** 单框文本工具:输入即时转换,输出带复制 */
|
||
DB.textTool = function ({ id, name, group, icon, desc, keywords, ph, rows, fn, actions, live = true }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'type', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const src = DB.ta(ph || '在这里输入…', rows || 10)
|
||
const out = el('pre', { class: 'out' })
|
||
const stat = el('div', { class: 'hint' })
|
||
const run = async () => {
|
||
try {
|
||
const r = await fn(src.value)
|
||
out.textContent = typeof r === 'string' ? r : (r && r.text) || ''
|
||
if (r && r.hint) { stat.textContent = r.hint; stat.className = 'hint good' }
|
||
else stat.textContent = ''
|
||
} catch (e) { out.textContent = ''; stat.textContent = '出错了:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
const bar = el('div', { class: 'row' })
|
||
for (const [label, f] of Object.entries(actions || {})) bar.append(btn(label, () => { src.value = f(src.value); run() }))
|
||
bar.append(btn('清空', () => { src.value = ''; run() }, 'ghost'))
|
||
bar.append(btn('复制结果', () => copy(out.textContent), 'ghost'))
|
||
root.append(pane(name, bar), src, stat, out)
|
||
if (live) src.addEventListener('input', run)
|
||
run()
|
||
},
|
||
})
|
||
}
|
||
|
||
/** 文本 ↔ 文本 双向转换 */
|
||
DB.convTool = function ({ id, name, group, icon, desc, keywords, left = 'A', right = 'B', a2b, b2a, phA, phB }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'swap', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const src = DB.ta(phA || left + '…', 9)
|
||
const dst = DB.ta(phB || right + '…', 9)
|
||
const stat = el('div', { class: 'hint' })
|
||
const go = (dir) => {
|
||
try {
|
||
dst.value = dir === 'a' ? a2b(src.value) : b2a(src.value)
|
||
stat.textContent = dir === 'a' ? left + ' → ' + right : right + ' → ' + left
|
||
stat.className = 'hint good'
|
||
} catch (e) { stat.textContent = '转换失败:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
root.append(pane(name, el('div', { class: 'row' },
|
||
btn(left + ' → ' + right, () => go('a')), btn(right + ' → ' + left, () => go('b')),
|
||
btn('交换', () => { const t = src.value; src.value = dst.value; dst.value = t }, 'ghost'),
|
||
btn('复制结果', () => copy(dst.value), 'ghost'))),
|
||
el('div', { class: 'grid2' }, src, dst), stat)
|
||
},
|
||
})
|
||
}
|
||
|
||
/** 生成器:参数(select / number / text)+ 一键生成 */
|
||
DB.genTool = function ({ id, name, group, icon, desc, keywords, fields, run, outputLabel = '结果' }) {
|
||
DB.tool({
|
||
id, name, group, icon: icon || 'bolt', desc, keywords: keywords || [],
|
||
mount(root) {
|
||
const refs = {}
|
||
const row = el('div', { class: 'row' })
|
||
for (const f of fields || []) {
|
||
if (f.type === 'select') {
|
||
const s = el('select', { class: 'input' }, ...f.options.map(([v, t]) => el('option', { value: v, text: t || v })))
|
||
if (f.value) s.value = f.value
|
||
refs[f.key] = () => s.value
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), s))
|
||
} else if (f.type === 'number') {
|
||
const n = el('input', { type: 'number', value: f.value ?? 1, min: f.min ?? 0, max: f.max ?? 1000000, step: f.step || 1, style: { width: '110px' } })
|
||
refs[f.key] = () => Number(n.value)
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), n))
|
||
} else if (f.type === 'text') {
|
||
const i = el('input', { type: 'text', value: f.value || '', placeholder: f.ph || '' })
|
||
refs[f.key] = () => i.value
|
||
row.append(el('label', { class: 'inline-field' }, el('span', { text: f.label }), i))
|
||
} else if (f.type === 'check') {
|
||
const c = el('input', { type: 'checkbox' })
|
||
c.checked = !!f.value
|
||
refs[f.key] = () => c.checked
|
||
row.append(el('label', { class: 'inline-field row' }, c, el('span', { text: f.label })))
|
||
}
|
||
}
|
||
const out = el('pre', { class: 'out' })
|
||
const stat = el('div', { class: 'hint' })
|
||
const go = async () => {
|
||
try {
|
||
const args = Object.fromEntries(Object.entries(refs).map(([k, f]) => [k, f()]))
|
||
const r = await run(args)
|
||
out.textContent = typeof r === 'string' ? r : (r && r.text) || ''
|
||
if (r && r.hint) { stat.textContent = r.hint; stat.className = 'hint good' }
|
||
} catch (e) { stat.textContent = '生成失败:' + e.message; stat.className = 'hint bad' }
|
||
}
|
||
const bar = el('div', { class: 'row' }, btn('生成', go), btn('复制', () => copy(out.textContent), 'ghost'))
|
||
root.append(pane(name, bar), (fields || []).length ? row : null, stat, out)
|
||
go()
|
||
},
|
||
})
|
||
}
|
||
})()
|