From 5eff203741ff2adc0edd6bb6581ef37b6657008d Mon Sep 17 00:00:00 2001 From: dsh-go Date: Fri, 20 Nov 2026 15:20:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=20devbox=20=E6=BA=90?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由 dsh-go 自动上传 · 2026/11/20 15:20:41 --- js/cat-check2.js | 264 +++++++++++++++++++++++++++++++++++++++++++++++ js/cat-file.js | 188 +++++++++++++++++++++++++++++++++ 2 files changed, 452 insertions(+) create mode 100644 js/cat-check2.js create mode 100644 js/cat-file.js diff --git a/js/cat-check2.js b/js/cat-check2.js new file mode 100644 index 0000000..c3461af --- /dev/null +++ b/js/cat-check2.js @@ -0,0 +1,264 @@ +(function () { + const { el, panelTitle, copy, toast } = DB + const pane = (t, ...k) => { const p = el('div', { class: 'panel' }); if (t) p.append(panelTitle(t)); p.append(...k.filter(Boolean)); return p } + const btn = (l, f, c = '') => el('button', { class: 'btn ' + c, text: l, onclick: f }) + const stage = (h) => { const d = el('div', { class: 'fullscreen-stage' }); d.style.height = (h || 340) + 'px'; return d } + let ac = null + const audio = () => { ac = ac || new (window.AudioContext || window.webkitAudioContext)(); return ac } + + /* 屏幕测试(纯色轮播) */ + DB.tool({ id: 'screen-test', name: '屏幕测试', group: 'device', icon: 'monitor', desc: '纯色轮播,检查亮点、暗点、漏光', keywords: ['屏幕', '坏点', '漏光'], + mount(root) { + const colors = ['#ffffff', '#000000', '#ff0000', '#00ff00', '#0000ff', '#ffff00', '#00ffff', '#ff00ff'] + const s = stage(); let i = 0 + const paint = () => { s.style.background = colors[i]; s.textContent = '' ; s.append(el('span', { class: 'stage-hint', text: colors[i] + ' · 点一下换下一个' })) } + s.addEventListener('click', () => { i = (i + 1) % colors.length; paint() }) + paint(); root.append(pane('屏幕测试', el('p', { class: 'hint', text: '手机调最亮,一个个看有没有异色点、边缘漏光。' })), s) + } }) + + DB.tool({ id: 'solid-color-screen', name: '纯色补光', group: 'device', icon: 'monitor', desc: '全屏纯白/纯色当补光灯', keywords: ['补光', '纯色', '白光'], + mount(root) { + const col = el('input', { type: 'color', value: '#ffffff' }); const s = stage(360) + const paint = () => { s.style.background = col.value } + col.addEventListener('input', paint); paint() + root.append(pane('纯色补光'), el('div', { class: 'row' }, col, btn('全屏', () => { s.requestFullscreen?.() }, 'ghost')), s) + } }) + + DB.tool({ id: 'monitor-calibration', name: '显示器校准', group: 'device', icon: 'monitor', desc: '灰阶、黑位、白位一张图调', keywords: ['校准', '灰阶', '对比度'], + mount(root) { + const box = el('div', { class: 'calib' }) + for (let i = 0; i < 21; i++) { const d = el('div', { class: 'calib-cell' }); d.style.background = `rgb(${Math.round(i * 12.75)},${Math.round(i * 12.75)},${Math.round(i * 12.75)})`; box.append(d) } + const bar = el('div', { class: 'calib' }) + ;['#000', '#111', '#222', '#fff', '#eee', '#ddd', '#f00', '#0f0', '#00f', '#ff0', '#0ff', '#f0f'].forEach((c) => { const d = el('div', { class: 'calib-cell' }); d.style.background = c; bar.append(d) }) + root.append(pane('显示器校准'), el('p', { class: 'hint', text: '第一排应能分辨每一格;最左黑块不该和背景融为一体,最右白块不该糊成一片。' }), box, bar) + } }) + + DB.tool({ id: 'color-banding-test', name: '色带测试', group: 'device', icon: 'monitor', desc: '看渐变有没有明显条纹', keywords: ['色带', '渐变', '色深'], + mount(root) { + const s = stage(300) + s.style.background = 'linear-gradient(90deg,#000 0%,#000 10%,#fff 90%,#fff 100%)' + const c = stage(300) + const g = c.getContext && c.getContext('2d') + if (g) { const grd = g.createLinearGradient(0, 0, c.clientWidth || 600, 0); grd.addColorStop(0, '#000'); grd.addColorStop(1, '#fff'); g.fillStyle = grd; g.fillRect(0, 0, c.clientWidth || 600, 300) } + root.append(pane('色带测试'), el('p', { class: 'hint', text: '平滑过渡为正常;出现明显竖条说明色深受限(8bit 屏常见)。' }), s, c) + } }) + + DB.tool({ id: 'color-arrangement-test', name: '颜色排序测试', group: 'device', icon: 'monitor', desc: '按色相排列小方块,练色感', keywords: ['色感', '排序', '颜色'], + mount(root) { + const wrap = el('div', { class: 'chips' }); const st = el('p', { class: 'hint', text: '点两个方块交换位置,排成彩虹顺序' }) + let arr = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330].sort(() => Math.random() - 0.5) + let sel = -1 + const paint = () => { wrap.textContent = ''; arr.forEach((h, i) => { const d = el('button', { class: 'swatch' + (sel === i ? ' on' : '') }); d.style.background = `hsl(${h} 70% 55%)`; d.addEventListener('click', () => { if (sel < 0) sel = i; else { [arr[sel], arr[i]] = [arr[i], arr[sel]]; sel = -1; paint() } }); wrap.append(d) }) + const ok = arr.every((v, i) => i === 0 || v > arr[i - 1]); st.textContent = ok ? '排对了 ✓' : '点两个方块交换位置,排成彩虹顺序' } + paint(); root.append(pane('颜色排序'), st, wrap, btn('重来', () => { arr = arr.sort(() => Math.random() - 0.5); paint() }, 'ghost')) + } }) + + DB.tool({ id: 'color-blind-test', name: '色觉自测', group: 'device', icon: 'monitor', desc: '简易色盲筛查图(红绿/蓝黄混淆)', keywords: ['色盲', '色弱', '色觉'], + mount(root) { + const s = stage(300); s.style.display = 'grid'; s.style.placeItems = 'center' + const plates = [['#e35d5d', '#7bb661', '8'], ['#5d7be3', '#d9c45b', '6'], ['#e3a95d', '#66b3a3', '3'], ['#8f5de3', '#5db67f', '5']] + let i = 0 + const paint = () => { s.style.background = plates[i][0]; s.textContent = ''; const t = el('div', { text: plates[i][2] }); t.style.color = plates[i][1]; t.style.fontSize = '120px'; t.style.fontWeight = '800'; s.append(t) } + s.addEventListener('click', () => { i = (i + 1) % plates.length; paint() }); paint() + root.append(pane('色觉自测'), el('p', { class: 'hint', text: '能看到数字说明红绿色觉问题不大;看不清不代表一定有问题,只是筛查。点一下换一张。' }), s) + } }) + + DB.tool({ id: 'color-converter', name: '颜色转换', group: 'web', icon: 'palette', desc: 'HEX / RGB / HSL / HSV 互转,带对比度', keywords: ['颜色', 'hex', 'rgb', 'hsl'], + mount(root) { + const inp = el('input', { class: 'inp', value: '#3b82f6' }) + const out = el('div', { class: 'kv-out', text: '' }); const sw = el('div', { class: 'swatch big' }) + const parse = (s) => { + s = s.trim() + let m = s.match(/^#?([0-9a-f]{6}|[0-9a-f]{3})$/i) + if (m) { let h = m[1]; if (h.length === 3) h = [...h].map((c) => c + c).join(''); return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)] } + m = s.match(/rgba?\(([^)]+)\)/i) + if (m) return m[1].split(',').slice(0, 3).map((x) => +x.trim()) + return null + } + const go = () => { + const rgb = parse(inp.value); if (!rgb) { out.textContent = '支持 #rrggbb 或 rgb(r,g,b)'; return } + const [r, g, b] = rgb.map((x) => Math.max(0, Math.min(255, Math.round(x)))) + const hexv = '#' + [r, g, b].map((x) => x.toString(16).padStart(2, '0')).join('') + const max = Math.max(r, g, b) / 255, min = Math.min(r, g, b) / 255, l = (max + min) / 2 + let h = 0, s2 = 0 + if (max !== min) { const d = max - min; s2 = l > .5 ? d / (2 - max - min) : d / (max + min) + h = max === r / 255 ? ((g - b) / 255 / d + (g < b ? 6 : 0)) : max === g / 255 ? ((b - r) / 255 / d + 2) : ((r - g) / 255 / d + 4); h *= 60 } + const lum = [r, g, b].map((v) => { const c = v / 255; return c <= .03928 ? c / 12.92 : Math.pow((c + .055) / 1.055, 2.4) }).reduce((a, v, i) => a + v * [0.2126, 0.7152, 0.0722][i], 0) + sw.style.background = hexv + out.textContent = `HEX ${hexv}\nRGB rgb(${r}, ${g}, ${b})\nHSL hsl(${Math.round(h)}, ${Math.round(s2 * 100)}%, ${Math.round(l * 100)}%)\n相对亮度 ${lum.toFixed(3)} 对白字对比 ${((1.05) / (lum + 0.05)).toFixed(2)}:1 对黑字 ${((lum + 0.05) / 0.05).toFixed(2)}:1` + } + inp.addEventListener('input', go); go() + root.append(pane('颜色转换'), inp, el('div', { class: 'row' }, sw), out) + } }) + + DB.tool({ id: 'dominant-eye-test', name: '主视眼测试', group: 'device', icon: 'box', desc: '双手圈成三角看物体,判断主导眼', keywords: ['主视眼', '眼睛'], + mount(root) { root.append(pane('主视眼测试'), el('p', { class: 'hint', text: '1. 双手张开、虎口交叉成三角孔;2. 手臂伸直,透过孔看远处一个小物体;3. 保持不动分别闭左眼、闭右眼。还能看见物体的那只眼就是主视眼。' }), el('div', { class: 'kv-out', text: '射击、台球、摄影取景都可以用主视眼。' })) } }) + + DB.tool({ id: 'astigmatism-test', name: '散光自测', group: 'device', icon: 'box', desc: '放射状线条图,看是否粗细不一', keywords: ['散光', '视力'], + mount(root) { + const s = stage(340); const g = s.getContext && s.getContext('2d') + if (g) { const w = 600, h = 340; s.width = w; s.height = h; g.fillStyle = '#fff'; g.fillRect(0, 0, w, h); g.strokeStyle = '#111'; g.lineWidth = 2 + for (let i = 0; i < 24; i++) { const a = (i / 24) * Math.PI * 2; g.beginPath(); g.moveTo(w / 2, h / 2); g.lineTo(w / 2 + Math.cos(a) * 150, h / 2 + Math.sin(a) * 150); g.stroke() } } + root.append(pane('散光自测'), el('p', { class: 'hint', text: '离屏 40cm 看,若某些方向的线明显更黑更粗,可能有散光(需去医院验光确认)。' }), s) + } }) + + DB.tool({ id: 'vision-test', name: '视力自测', group: 'device', icon: 'box', desc: 'E 字方向自测,粗略估视力', keywords: ['视力', '近视', '远视'], + mount(root) { + const s = stage(320), out = el('div', { class: 'kv-out', text: '' }), ans = el('div', { class: 'chips' }) + let level = 0, correct = 0 + const sizes = [120, 90, 64, 46, 32, 22, 14, 10] + const dirs = ['up', 'down', 'left', 'right'] + let cur = 'up' + const paint = () => { s.textContent = ''; const t = el('div', { text: 'E' }); t.style.fontSize = sizes[level] + 'px'; t.style.fontWeight = '800'; t.style.transform = `rotate(${dirs.indexOf(cur) * 90}deg)`; s.style.display = 'grid'; s.style.placeItems = 'center'; s.append(t) } + const answer = (d) => { if (d === cur) correct++; level = Math.min(level + 1, sizes.length - 1); cur = dirs[Math.floor(Math.random() * 4)]; paint(); out.textContent = `已答对 ${correct} 题,当前字号 ${sizes[level]}px` } + answer('') + ;['上', '下', '左', '右'].forEach((n, i) => ans.append(btn(n, () => answer(dirs[i])))) + root.append(pane('视力自测'), el('p', { class: 'hint', text: '手臂伸直(约 40cm)看屏幕,E 开口朝哪就点哪。' }), s, ans, out) + } }) + + DB.tool({ id: 'pwm-flicker-test', name: 'PWM 频闪检测', group: 'device', icon: 'monitor', desc: '转动的条纹看屏幕是否低频闪', keywords: ['频闪', 'pwm', '护眼'], + mount(root) { + const s = stage(220); const g = s.getContext && s.getContext('2d'); let x = 0, run = true + const loop = () => { if (!g) return; const w = s.width || 600, h = 220; s.width = w; s.height = h + g.fillStyle = '#fff'; g.fillRect(0, 0, w, h); g.fillStyle = '#000' + for (let i = -h; i < w + h; i += 24) { g.save(); g.translate(i + x, 0); g.rotate(0.35); g.fillRect(0, -h, 12, h * 3); g.restore() } + x = (x + 6) % 24; if (run) requestAnimationFrame(loop) } + loop(); s.addEventListener('click', () => { run = !run; if (run) loop() }) + root.append(pane('PWM 频闪检测'), el('p', { class: 'hint', text: '滚动条纹出现明显分段/闪动,说明屏幕低频调光;点一下暂停。' }), s) + } }) + + DB.tool({ id: 'camera-test', name: '摄像头测试', group: 'device', icon: 'box', desc: '调用摄像头看画面是否正常', keywords: ['摄像头', '相机', 'camera'], + mount(root) { + const v = el('video', { class: 'cam', autoplay: true, playsinline: true, muted: true }) + const st = el('p', { class: 'hint', text: '点开始授权摄像头' }) + const start = async () => { try { const s = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } }); v.srcObject = s; st.textContent = '已开启,点停止释放' } catch (e) { st.textContent = '打不开摄像头:' + e.message } } + root.append(pane('摄像头测试'), st, v, el('div', { class: 'row' }, btn('开始', start), btn('停止', () => { v.srcObject?.getTracks().forEach((t) => t.stop()); v.srcObject = null; st.textContent = '已停止' }, 'ghost'))) + } }) + + DB.tool({ id: 'gps-test', name: 'GPS 测试', group: 'device', icon: 'box', desc: '看定位精度、坐标与速度', keywords: ['gps', '定位', '坐标'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '点开始授权定位' }) + const start = () => { if (!navigator.geolocation) { out.textContent = '这个浏览器不支持定位'; return } + navigator.geolocation.watchPosition((p) => { out.textContent = `纬度 ${p.coords.latitude.toFixed(6)}\n经度 ${p.coords.longitude.toFixed(6)}\n精度 ±${Math.round(p.coords.accuracy)} m\n海拔 ${p.coords.altitude ? Math.round(p.coords.altitude) + ' m' : '—'}\n速度 ${p.coords.speed ? (p.coords.speed * 3.6).toFixed(1) + ' km/h' : '—'}` }, + (e) => { out.textContent = '定位失败:' + e.message }, { enableHighAccuracy: true }) } + root.append(pane('GPS 测试'), out, btn('开始', start)) + } }) + + DB.tool({ id: 'sensor-test', name: '传感器测试', group: 'device', icon: 'box', desc: '看重力感应/陀螺仪读数', keywords: ['传感器', '陀螺仪', '加速计'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '点开始读取设备方向' }) + const on = (e) => { out.textContent = `α ${e.alpha?.toFixed(1) ?? '—'}° β ${e.beta?.toFixed(1) ?? '—'}° γ ${e.gamma?.toFixed(1) ?? '—'}°\n更新于 ${new Date().toLocaleTimeString()}` } + root.append(pane('传感器测试'), out, btn('开始', () => { window.addEventListener('deviceorientation', on); out.textContent = '已开始,转动手机看看读数' }), btn('停止', () => window.removeEventListener('deviceorientation', on), 'ghost')) + } }) + + DB.tool({ id: 'nkro-test', name: '按键同时识别测试', group: 'device', icon: 'keyboard', desc: '看键盘能同时识别几个键', keywords: ['键盘', 'nkro', '同时按键'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '按住多个键试试' }); const set = new Set() + const dn = (e) => { set.add(e.code); out.textContent = `当前 ${set.size} 键:${[...set].join(' + ')} 最多 ${Math.max(set.size, +(out.dataset.max || 0))}`; out.dataset.max = Math.max(set.size, +(out.dataset.max || 0)) } + const up = (e) => { set.delete(e.code) } + window.addEventListener('keydown', dn); window.addEventListener('keyup', up) + root.append(pane('同时按键测试'), out, el('p', { class: 'hint', text: '普通键盘常见 6 键上限;机械键盘支持全键无冲。' })) + } }) + + DB.tool({ id: 'double-click-test', name: '双击速度测试', group: 'device', icon: 'mouse', desc: '测两次点击的间隔,判断鼠标微动是否双击', keywords: ['双击', '鼠标', '微动'], + mount(root) { + const pad = el('div', { class: 'clickpad', text: '在这里连续点' }); const out = el('div', { class: 'kv-out', text: '' }); let last = 0; const gaps = [] + pad.addEventListener('click', () => { const t = performance.now(); if (last) { const g = Math.round(t - last); gaps.push(g); out.textContent = `间隔 ${g} ms 最近:${gaps.slice(-6).join(' / ')} ms\n${g < 40 ? '⚠ 间隔过短,可能是误触或双击' : ''}` } last = t }) + root.append(pane('双击速度测试'), pad, out) + } }) + + DB.tool({ id: 'polling-rate-test', name: '鼠标轮询率', group: 'device', icon: 'mouse', desc: '快速移动鼠标,估算 1000Hz 还是 125Hz', keywords: ['轮询率', '鼠标', 'polling'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '把鼠标在下面这块快速画圈' }); let times = [] + const move = (e) => { times.push(performance.now()); if (times.length > 400) times.shift() + if (times.length > 20) { const span = times[times.length - 1] - times[0]; const hz = Math.round((times.length - 1) / span * 1000); out.textContent = `约 ${hz} Hz(样本 ${times.length})` } } + const pad = el('div', { class: 'clickpad', text: '在这里画圈' }); pad.addEventListener('pointermove', move) + root.append(pane('鼠标轮询率'), pad, out) + } }) + + /* 音频:声道 / 低频 / 耳鸣匹配 / 白噪音 / 雨声 / 专注音 */ + DB.tool({ id: 'headphone-test', name: '耳机左右声道', group: 'device', icon: 'music', desc: '分别响左、右,检查耳机是否接反', keywords: ['耳机', '声道', '左右'], + mount(root) { + const play = (side) => { const ctx = audio(); const o = ctx.createOscillator(); const p = ctx.createStereoPanner?.() ; const g = ctx.createGain() + o.frequency.value = 440; g.gain.value = .2; o.connect(g); if (p) { p.pan.value = side === 'L' ? -1 : 1; g.connect(p); p.connect(ctx.destination) } else g.connect(ctx.destination) + o.start(); setTimeout(() => { o.stop() }, 900) } + root.append(pane('耳机左右声道'), el('div', { class: 'row' }, btn('左声道', () => play('L')), btn('右声道', () => play('R')), btn('居中', () => play('C'), 'ghost')), el('p', { class: 'hint', text: '左右相反说明耳机插头或系统设置被反相了。' })) + } }) + + DB.tool({ id: 'bass-test', name: '低频测试', group: 'device', icon: 'music', desc: '从 20Hz 扫到 200Hz,看下潜能到多低', keywords: ['低频', '低音', 'bass'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }); let cur = null + const play = (f) => { stop(); const ctx = audio(); const o = ctx.createOscillator(); const g = ctx.createGain(); o.type = 'sine'; o.frequency.value = f; g.gain.value = .25; o.connect(g); g.connect(ctx.destination); o.start(); cur = { o, ctx } + out.textContent = `正在播放 ${f} Hz`; o.onended = () => { } } + const stop = () => { try { cur?.o.stop(); cur?.ctx.close() } catch (e) {} cur = null } + const row = el('div', { class: 'chips' }); [20, 30, 40, 50, 60, 80, 100, 120, 150, 200].forEach((f) => row.append(btn(f + 'Hz', () => play(f), 'ghost'))) + root.append(pane('低频测试'), row, out, btn('停止', stop, 'ghost'), el('p', { class: 'hint', text: '20–40Hz 听不到很正常,耳机下潜一般到 40Hz。' })) + } }) + + DB.tool({ id: 'tinnitus-match', name: '耳鸣匹配', group: 'device', icon: 'music', desc: '找出跟你耳鸣最接近的音高', keywords: ['耳鸣', '音高', '匹配'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }); let f = 4000, cur = null + const play = () => { try { cur?.o.stop(); cur?.ctx.close() } catch (e) {} const ctx = audio(); const o = ctx.createOscillator(), g = ctx.createGain(); o.frequency.value = f; g.gain.value = .08; o.connect(g); g.connect(ctx.destination); o.start(); cur = { o, ctx }; out.textContent = `当前 ${f} Hz` } + const slider = el('input', { type: 'range', min: 500, max: 12000, step: 50, value: f }) + slider.addEventListener('input', () => { f = +slider.value; if (cur) play(); out.textContent = `当前 ${f} Hz` }) + root.append(pane('耳鸣匹配'), slider, out, el('div', { class: 'row' }, btn('播放', play), btn('停止', () => { try { cur?.o.stop(); cur?.ctx.close() } catch (e) {} }, 'ghost')), el('p', { class: 'hint', text: '找到最接近的音高后,把这个数值带给耳鼻喉科医生参考。音量开小一点。' })) + } }) + + function noiseTool(id, name, desc, kind, keywords) { + DB.tool({ id, name, group: 'device', icon: 'music', desc, keywords, mount(root) { + const st = el('p', { class: 'hint', text: '未播放' }); let node = null, ctx = null + const start = () => { stop(); ctx = audio(); const len = ctx.sampleRate * 4; const buf = ctx.createBuffer(2, len, ctx.sampleRate) + for (let ch = 0; ch < 2; ch++) { const d = buf.getChannelData(ch); let b0 = 0, b1 = 0, b2 = 0 + for (let i = 0; i < len; i++) { const w = Math.random() * 2 - 1 + if (kind === 'white') d[i] = w * .18 + else if (kind === 'pink') { b0 = .99765 * b0 + w * .099046; b1 = .963 * b1 + w * .2965164; b2 = .57 * b2 + w * 1.0526913; d[i] = (b0 + b1 + b2 + w * .1848) * .05 } + else d[i] = (Math.random() - .5) * Math.pow(Math.random(), 3) * 1.4 } } + node = ctx.createBufferSource(); node.buffer = buf; node.loop = true + const g = ctx.createGain(); g.gain.value = .5; node.connect(g); g.connect(ctx.destination); node.start(); st.textContent = '正在播放,音量可调小' } + const stop = () => { try { node?.stop(); ctx?.close() } catch (e) {} node = null; st.textContent = '已停止' } + root.append(pane(name), st, el('div', { class: 'row' }, btn('播放', start), btn('停止', stop, 'ghost'))) + } }) + } + noiseTool('white-noise', '白噪音', '屏蔽环境噪音,帮助入睡、专注', 'white', ['白噪音', '专注', '睡眠']) + noiseTool('rain-sounds', '雨声', '模拟雨滴声(合成)', 'rain', ['雨声', '助眠']) + noiseTool('focus-sounds', '粉噪音', '比白噪音更柔,适合长时间听', 'pink', ['粉噪', '专注']) + + /* 呼吸 / 拉伸 / 眼保健 / 健身 */ + DB.tool({ id: 'breathing-exercise', name: '呼吸引导', group: 'life', icon: 'sun', desc: '4-7-8 呼吸法,跟着圆呼吸', keywords: ['呼吸', '放松', '冥想'], + mount(root) { + const c = el('div', { class: 'breath-circle' }), st = el('div', { class: 'big-time', text: '准备' }); let iv = 0 + const seq = [['吸气', 4], ['屏住', 7], ['呼气', 8]] + const run = () => { clearInterval(iv); let i = 0, left = seq[0][1] + const tick = () => { const [n, d] = seq[i]; st.textContent = n + ' ' + left; c.style.transform = `scale(${n === '吸气' ? 1.25 : n === '屏住' ? 1.25 : .85})` + left--; if (left <= 0) { i = (i + 1) % seq.length; left = seq[i][1] } } + tick(); iv = setInterval(tick, 1000) } + root.append(pane('4-7-8 呼吸'), st, c, el('div', { class: 'row' }, btn('开始', run), btn('停止', () => { clearInterval(iv); st.textContent = '已停止' }, 'ghost'))) + } }) + + DB.tool({ id: 'stretch-routine', name: '久坐拉伸', group: 'life', icon: 'sun', desc: '一套 6 分钟办公室拉伸', keywords: ['拉伸', '久坐', '肩颈'], + mount(root) { + const list = [['颈部侧倾', '左右各 20 秒,耳朵靠近肩膀,不耸肩'], ['肩部环绕', '前后各 10 圈,动作慢'], ['胸椎伸展', '双手抱头,向后靠椅背 20 秒'], ['体侧伸展', '一手叉腰,另一手向对侧延伸,左右各 20 秒'], ['髋屈肌拉伸', '弓步前压,左右各 30 秒'], ['腿后侧拉伸', '伸直一条腿勾脚,身体前倾 30 秒'], ['手腕拉伸', '手指向下、向上各 15 秒'], ['眼球转动', '上下左右各 5 次,慢慢来']] + const box = el('div', { class: 'kv-out' }); box.textContent = list.map((a, i) => `${i + 1}. ${a[0]}:${a[1]}`).join('\n') + root.append(pane('久坐拉伸'), box) + } }) + + DB.tool({ id: 'eye-exercise', name: '眼保健操', group: 'life', icon: 'sun', desc: '20-20-20 之外的眼部放松动作', keywords: ['眼保健操', '护眼'], + mount(root) { + const s = [['远眺', '看 6 米外固定物体 20 秒'], ['眨眼', '快速眨眼 15 次,再闭眼 10 秒'], ['眼球画圈', '顺时针、逆时针各 5 圈'], ['掌心捂眼', '双手搓热捂眼 30 秒,不要压眼球'], ['交替聚焦', '手指 20cm 与远处交替看 10 次']] + root.append(pane('眼保健操'), el('div', { class: 'kv-out', text: s.map((a, i) => `${i + 1}. ${a[0]}:${a[1]}`).join('\n') })) + } }) + + DB.tool({ id: 'workout-random', name: '随机训练', group: 'life', icon: 'sun', desc: '随机抽一个居家动作组合', keywords: ['健身', '训练', '随机'], + mount(root) { + const moves = ['深蹲 15 次', '俯卧撑 10 次', '平板支撑 30 秒', '开合跳 30 次', '登山跑 20 次', '臀桥 15 次', '靠墙静蹲 45 秒', '弓步蹲 12 次/侧', '卷腹 15 次', '波比跳 8 次'] + const out = el('div', { class: 'kv-out big', text: '' }) + const go = () => { out.textContent = [...moves].sort(() => Math.random() - .5).slice(0, 4).map((m, i) => `${i + 1}. ${m}`).join('\n') + '\n\n完成 3 轮,组间休息 60 秒' } + go(); root.append(pane('随机训练'), out, btn('换一组', go)) + } }) + + DB.tool({ id: 'first-aid-guide', name: '急救速查', group: 'life', icon: 'info', desc: '常见意外的第一步处理(不含诊断)', keywords: ['急救', '烫伤', '扭伤'], + mount(root) { + const items = [['烫伤', '立刻用流动凉水冲 15–20 分钟,不要涂牙膏酱油,起泡不要挑破'], ['割伤', '清水冲洗,干净纱布压迫止血 10 分钟,深的伤口去医院'], ['扭伤', '前 48 小时冰敷每次 15 分钟,抬高患处,别热敷别揉'], ['异物卡喉', '成人海姆立克:背后抱住,拳头顶脐上快速向上冲击'], ['中暑', '移到阴凉处,解开衣物,凉水擦身,意识不清立刻急救'], ['低血糖', '意识清醒时吃 15g 糖(半罐可乐/3 块方糖),15 分钟复测']] + root.append(pane('急救速查'), el('div', { class: 'kv-out', text: items.map(([a, b]) => `· ${a}:${b}`).join('\n') }), el('p', { class: 'hint', text: '以上只是常识提示,严重情况请立刻拨打 120。' })) + } }) +})() diff --git a/js/cat-file.js b/js/cat-file.js new file mode 100644 index 0000000..7561cbe --- /dev/null +++ b/js/cat-file.js @@ -0,0 +1,188 @@ +(function () { + const { el, panelTitle, copy, toast, bytes, download } = DB + const pane = (t, ...k) => { const p = el('div', { class: 'panel' }); if (t) p.append(panelTitle(t)); p.append(...k.filter(Boolean)); return p } + const btn = (l, f, c = '') => el('button', { class: 'btn ' + c, text: l, onclick: f }) + const readFile = (f, how = 'buf') => new Promise((res, rej) => { const r = new FileReader(); r.onload = () => res(r.result); r.onerror = rej; how === 'buf' ? r.readAsArrayBuffer(f) : r.readAsText(f) }) + const hex = (buf) => [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, '0')).join('') + const picker = (multi, cb, accept) => { const i = el('input', { type: 'file', class: 'inp', multiple: multi || false }); if (accept) i.accept = accept; i.addEventListener('change', () => cb([...i.files])); return i } + + /* MD5(自带实现,避免没 subtle 的环境) */ + function md5(buf) { + const s = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21] + const K = new Int32Array(64); for (let i = 0; i < 64; i++) K[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296) | 0 + const bytes = new Uint8Array(buf); const len = bytes.length + const total = (((len + 8) >> 6) + 1) * 64 + const m = new Uint8Array(total); m.set(bytes); m[len] = 0x80 + const dv = new DataView(m.buffer); dv.setUint32(total - 8, (len * 8) >>> 0, true); dv.setUint32(total - 4, Math.floor(len / 536870912), true) + let a0 = 0x67452301, b0 = 0xefcdab89 | 0, c0 = 0x98badcfe | 0, d0 = 0x10325476 + for (let off = 0; off < total; off += 64) { + const M = new Int32Array(16); for (let i = 0; i < 16; i++) M[i] = dv.getInt32(off + i * 4, true) + let A = a0, B = b0, C = c0, D = d0 + for (let i = 0; i < 64; i++) { + let F, g + if (i < 16) { F = (B & C) | (~B & D); g = i } else if (i < 32) { F = (D & B) | (~D & C); g = (5 * i + 1) % 16 } else if (i < 48) { F = B ^ C ^ D; g = (3 * i + 5) % 16 } else { F = C ^ (B | ~D); g = (7 * i) % 16 } + const tmp = D; D = C; C = B; B = (B + ((A + F + K[i] + M[g]) | 0) << 0) | 0 + A = tmp + const rot = s[i]; B = ((B << rot) | (B >>> (32 - rot))) | 0 + } + a0 = (a0 + A) | 0; b0 = (b0 + B) | 0; c0 = (c0 + C) | 0; d0 = (d0 + D) | 0 + } + const out = new Uint8Array(16); const o = new DataView(out.buffer) + ;[a0, b0, c0, d0].forEach((v, i) => o.setInt32(i * 4, v, true)) + return [...out].map((b) => b.toString(16).padStart(2, '0')).join('') + } + const sha = async (buf, alg) => hex(await crypto.subtle.digest(alg, buf)) + + /* 文件哈希 */ + DB.tool({ id: 'file-hash', name: '文件哈希', group: 'crypto', icon: 'hash', desc: '算 MD5 / SHA-1 / SHA-256 / SHA-512', keywords: ['哈希', 'md5', 'sha', '校验'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { if (!files[0]) return; const f = files[0]; out.textContent = '计算中…' + const buf = await readFile(f) + const r = { MD5: md5(buf), 'SHA-1': await sha(buf, 'SHA-1'), 'SHA-256': await sha(buf, 'SHA-256'), 'SHA-512': await sha(buf, 'SHA-512') } + out.textContent = `${f.name}(${bytes(f.size)})\n` + Object.entries(r).map(([k, v]) => `${k}: ${v}`).join('\n') + } + root.append(pane('文件哈希'), picker(false, run), out) + } }) + + /* 文件信息 */ + DB.tool({ id: 'file-info', name: '文件信息', group: 'web', icon: 'info', desc: '大小、类型、文件头、修改时间', keywords: ['文件', '信息', 'magic'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { if (!files[0]) return; const f = files[0]; const buf = await readFile(f) + const head = hex(buf.slice(0, 16)) + const magic = head.startsWith('89504e47') ? 'PNG 图片' : head.startsWith('ffd8ff') ? 'JPEG 图片' : head.startsWith('47494638') ? 'GIF 图片' : head.startsWith('25504446') ? 'PDF 文档' : head.startsWith('504b0304') ? 'ZIP / Office 文档' : head.startsWith('7f454c46') ? 'ELF 可执行' : /^4d5a|^5a4d/.test(head) ? 'PE 可执行' : head.startsWith('1f8b') ? 'GZIP 压缩' : head.startsWith('52494646') ? 'RIFF(WAV/WebP/AVI)' : head.startsWith('00000018') ? 'MP4 / MOV' : '未知' + out.textContent = [`名称:${f.name}`, `大小:${bytes(f.size)}(${f.size} 字节)`, `类型:${f.type || '未知'}`, `识别:${magic}`, `头部:${head}`, `修改:${new Date(f.lastModified).toLocaleString()}`].join('\n') } + root.append(pane('文件信息'), picker(false, run), out) + } }) + + /* 文件比对 */ + DB.tool({ id: 'file-compare', name: '文件比对', group: 'text', icon: 'diff', desc: '两个文件逐字节比,或按行比文本', keywords: ['比对', '对比', 'diff', '文件'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { + if (files.length < 2) { out.textContent = '选两个文件'; return } + const [a, b] = await Promise.all(files.slice(0, 2).map((f) => readFile(f))) + const A = new Uint8Array(a), B = new Uint8Array(b) + if (A.length !== B.length) out.textContent = `大小不同:${A.length} vs ${B.length} 字节` + else { let i = A.findIndex((v, k) => v !== B[k]); out.textContent = i < 0 ? '完全一致 ✓' : `第 ${i} 字节开始不同:${A[i].toString(16)} vs ${B[i].toString(16)}` } + } + root.append(pane('文件比对'), picker(true, run), out) + } }) + + /* 批量重命名 */ + DB.tool({ id: 'file-rename', name: '批量重命名', group: 'text', icon: 'edit', desc: '按规则生成新文件名清单,再手动改', keywords: ['重命名', '批量', '改名'], + mount(root) { + const list = el('textarea', { class: 'ta', rows: 5, placeholder: '每行一个原文件名' }) + const pat = el('input', { class: 'inp', value: '前缀_{n}{ext}', placeholder: '模板:{n} 序号,{name} 原名,{ext} 扩展名' }) + const out = el('textarea', { class: 'ta', rows: 8, readonly: true }) + const go = () => { + const pad = (n, w) => String(n).padStart(w, '0') + out.value = list.value.split('\n').filter((x) => x.trim()).map((line, i) => { + const name = line.trim(); const m = name.match(/\.[^.]+$/) + const ext = m ? m[0] : '', base = m ? name.slice(0, -m[0].length) : name + return pat.value.replace(/\{n\}/g, pad(i + 1, 3)).replace(/\{name\}/g, base).replace(/\{ext\}/g, ext) + }).join('\n') + } + list.addEventListener('input', go); pat.addEventListener('input', go) + root.append(pane('批量重命名'), list, pat, out, btn('复制新名字', () => copy(out.value))) + } }) + + /* 编码转换 */ + DB.tool({ id: 'file-encoding-convert', name: '文本编码转换', group: 'encode', icon: 'abc', desc: 'UTF-8 / UTF-16 / Base64 / Hex 互转,自动识别 BOM', keywords: ['编码', 'utf8', 'utf16', 'bom'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { if (!files[0]) return; const f = files[0]; const buf = await readFile(f); const u8 = new Uint8Array(buf) + const bom = u8[0] === 0xef && u8[1] === 0xbb && u8[2] === 0xbf ? 'UTF-8 BOM' : u8[0] === 0xff && u8[1] === 0xfe ? 'UTF-16LE' : u8[0] === 0xfe && u8[1] === 0xff ? 'UTF-16BE' : '无 BOM' + let text = '' + try { text = new TextDecoder(bom === 'UTF-16LE' ? 'utf-16le' : bom === 'UTF-16BE' ? 'utf-16be' : 'utf-8', { fatal: false }).decode(u8) } catch (e) { text = new TextDecoder().decode(u8) } + const ctrl = [...text.slice(0, 2000)].filter((c) => c.charCodeAt(0) < 9).length + out.textContent = `BOM:${bom}\n疑似二进制/乱码字符:${ctrl}\n\n前 500 字:\n${text.slice(0, 500)}` + } + root.append(pane('文本编码转换'), picker(false, run), out) + } }) + + /* 文件加密 / 解密(AES-GCM) */ + const enc = new TextEncoder(), dec = new TextDecoder() + async function aesKey(pass) { + const base = await crypto.subtle.importKey('raw', enc.encode(pass), 'PBKDF2', false, ['deriveKey']) + return crypto.subtle.deriveKey({ name: 'PBKDF2', salt: enc.encode('devbox-salt'), iterations: 120000, hash: 'SHA-256' }, base, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']) + } + DB.tool({ id: 'file-encrypt', name: '文件加密', group: 'crypto', icon: 'lock', desc: 'AES-GCM 加密任意文件,口令自己记住', keywords: ['加密', 'aes', '文件'], + mount(root) { + const pass = el('input', { class: 'inp', type: 'password', placeholder: '口令(至少 4 位)' }) + const st = el('div', { class: 'kv-out', text: '选文件后开始加密' }) + let cur = null + const run = async (files) => { if (!files[0]) return; cur = files[0]; st.textContent = `${cur.name}(${bytes(cur.size)})已选择,点下面加密` } + const go = async () => { + if (!cur) return + if (pass.value.length < 4) { toast('口令太短', true); return } + const iv = crypto.getRandomValues(new Uint8Array(12)) + const data = await readFile(cur) + const ct = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, await aesKey(pass.value), data) + const blob = new Blob([iv, new Uint8Array(ct)]) + download(blob, cur.name + '.enc') + st.textContent = '已加密下载:' + cur.name + '.enc' + } + root.append(pane('文件加密(AES-GCM)'), pass, picker(false, run), st, btn('加密并下载', go)) + } }) + DB.tool({ id: 'secret-note', name: '加密便签', group: 'crypto', icon: 'lock', desc: '把一段文字用口令加密成乱码,可再解回来', keywords: ['便签', '加密', '文字'], + mount(root) { + const pass = el('input', { class: 'inp', type: 'password', placeholder: '口令' }) + const inp = el('textarea', { class: 'ta', rows: 5, placeholder: '要加密的文字' }) + const out = el('textarea', { class: 'ta', rows: 5, readonly: true }) + const doEnc = async () => { if (!pass.value) return toast('先填口令', true) + const iv = crypto.getRandomValues(new Uint8Array(12)); const ct = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, await aesKey(pass.value), enc.encode(inp.value)) + out.value = btoa(String.fromCharCode(...iv)) + '.' + btoa(String.fromCharCode(...new Uint8Array(ct))) } + const doDec = async () => { try { + const [i6, c6] = out.value.trim().split('.'); const iv = Uint8Array.from(atob(i6), (c) => c.charCodeAt(0)) + const pt = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, await aesKey(pass.value), Uint8Array.from(atob(c6), (c) => c.charCodeAt(0))) + inp.value = dec.decode(pt); toast('解密成功') } catch (e) { toast('口令不对或内容损坏', true) } } + root.append(pane('加密便签'), pass, inp, out, el('div', { class: 'row' }, btn('加密', doEnc), btn('解密', doDec, 'ghost'), btn('复制', () => copy(out.value), 'ghost'))) + } }) + + /* 查重 */ + DB.tool({ id: 'duplicate-file-finder', name: '重复文件检测', group: 'web', icon: 'search', desc: '多选文件,按内容哈希找出重复', keywords: ['查重', '重复文件', 'duplicate'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { + if (!files.length) return + out.textContent = '计算中…' + const map = {} + for (const f of files) { const h = md5(await readFile(f)); (map[h] = map[h] || []).push(f.name + '(' + bytes(f.size) + ')') } + const dup = Object.values(map).filter((v) => v.length > 1) + out.textContent = dup.length ? '重复组:\n' + dup.map((g, i) => `第 ${i + 1} 组:` + g.join('、')).join('\n') : '没有发现重复文件 ✓' + } + root.append(pane('重复文件检测'), picker(true, run), out) + } }) + + /* 文档字数 */ + DB.tool({ id: 'doc-word-count', name: '文档字数统计', group: 'text', icon: 'edit', desc: '丢文件进来统计字数、行数、段落', keywords: ['字数', '统计', '文档'], + mount(root) { + const out = el('div', { class: 'kv-out', text: '' }) + const run = async (files) => { if (!files[0]) return; const t = await readFile(files[0], 'text') + const han = (t.match(/[\u4e00-\u9fff]/g) || []).length + const words = (t.match(/[A-Za-z][A-Za-z'-]*/g) || []).length + const nums = (t.match(/\d+/g) || []).length + const paras = t.split(/\n\s*\n/).filter((x) => x.trim()).length + out.textContent = `字符 ${t.length} 汉字 ${han} 英文词 ${words} 数字 ${nums}\n行数 ${t.split('\n').length} 段落 ${paras} 预计朗读 ${Math.ceil((han + words) / 4)} 秒` } + root.append(pane('文档字数统计'), picker(false, run), out) + } }) + + /* 错别字检查(常见混淆) */ + const TYPO = [['的得地'], ['在再'], ['做作'], ['帐账'], ['Secret', '']] + DB.tool({ id: 'typo-check', name: '常见错字提示', group: 'text', icon: 'edit', desc: '标出「的地得」「在再」「做作」这类易错用法', keywords: ['错别字', '校对'], + mount(root) { + const inp = el('textarea', { class: 'ta', rows: 6, placeholder: '粘贴文章…' }) + const out = el('div', { class: 'kv-out', text: '' }) + const rules = [[/的(good|well)/g, ''], [/在(次|见)/g, '「在」多指位置,「再」指重复'], [/[帐]/g, '与钱有关多用「账」'], [/地(说道)/g, ''], [/(做)(好|完)/g, '「做」偏具体动作,「作」偏抽象']] + const go = () => { const t = inp.value; const hits = [] + if (/[的地得]/.test(t)) hits.push('出现「的/地/得」:动词前用「地」,形容词后修饰名词用「的」,补充说明用「得」') + if (/再[次]/.test(t)) hits.push('「再次」正确;「在次」是错字') + if (/帐/.test(t)) hits.push('「帐」多用于帐篷,账目用「账」') + if (/即[使]/.test(t) && /既使/.test(t)) hits.push('「即使」不是「既使」') + out.textContent = hits.length ? hits.map((x, i) => `${i + 1}. ${x}`).join('\n') : '没发现问题(只能查常见混淆)' } + inp.addEventListener('input', go) + root.append(pane('常见错字提示'), inp, out) + } }) +})()