Files
devbox/js/tools-text-extra.js
T
gitea cea0c44e9d 文本提取器:结果按规则分组,逐条可单独复制
- 结果从单个 <pre> 改成 .extract-group 分组列表,每组头显示「手机号 · 2 条」与「复制全部」
- 每条一行:值 + 独立「复制」按钮,移动端自动换行
- 样式补充 .chips/.chip-btn/.extract-head/.extract-row/.extract-val
2026-11-20 14:32:45 +08:00

72 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
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 · 文本补齐:从一大段文本里抽手机号 / 邮箱 / 链接 / 身份证 / IP / 银行卡 */
(function () {
'use strict'
const { el, panelTitle, copy, toast, onInput } = 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 }
const ta = (ph, rows) => el('textarea', { class: 'ta', placeholder: ph, rows: rows || 10, spellcheck: 'false' })
const RULES = {
phone: { name: '手机号', re: /(?<!\d)1[3-9]\d{9}(?!\d)/g, hint: '中国大陆 11 位' },
email: { name: '邮箱', re: /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g, hint: '含常见 TLD' },
url: { name: '链接', re: /https?:\/\/[^\s<>"'))】\]]+/g, hint: 'http / https' },
idcard: { name: '身份证', re: /(?<!\d)\d{17}[\dXx](?!\d)/g, hint: '18 位,含 X 结尾' },
ip: { name: 'IP 地址', re: /(?<![\d.])(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}(?![\d.])/g, hint: 'IPv4' },
bank: { name: '银行卡号', re: /(?<!\d)(?:62|4\d|5[1-5])\d{13,17}(?!\d)/g, hint: '16-19 位' },
}
DB.tool({
id: 'text-extract',
name: '文本提取器',
group: 'text',
icon: 'filter',
desc: '从聊天记录、网页源码里批量抓手机号 / 邮箱 / 链接 / 身份证 / IP / 卡号,自动去重',
keywords: ['extract', 'regex', 'email', 'phone', 'url', 'ip', '提取', '手机号', '邮箱'],
mount(root) {
const src = ta('把一整段文本粘进来…', 12)
const picks = new Set(['phone', 'email', 'url'])
const chips = el('div', { class: 'chips' })
const out = el('div', { class: 'extract-out' })
const counts = el('div', { class: 'hint' })
for (const [k, r] of Object.entries(RULES)) {
const chip = el('button', { class: 'chip-btn' + (picks.has(k) ? ' on' : ''), text: r.name + ' · ' + r.hint })
chip.addEventListener('click', () => {
picks.has(k) ? picks.delete(k) : picks.add(k)
chip.classList.toggle('on', picks.has(k))
run()
})
chips.append(chip)
}
const run = () => {
const text = src.value
out.textContent = ''
if (!text.trim()) { counts.textContent = ''; return }
let total = 0
for (const k of Object.keys(RULES)) {
if (!picks.has(k)) continue
const found = [...new Set(text.match(RULES[k].re) || [])]
if (!found.length) continue
total += found.length
const box = el('div', { class: 'extract-group' })
box.append(el('div', { class: 'row between extract-head' },
el('b', { text: RULES[k].name + ' · ' + found.length + ' 条' }),
el('button', { class: 'btn tiny ghost', text: '复制全部', onclick: () => copy(found.join('\n')) })))
for (const item of found) {
box.append(el('div', { class: 'extract-row' },
el('code', { class: 'extract-val', text: item }),
el('button', { class: 'btn tiny', text: '复制', onclick: () => copy(item) })))
}
out.append(box)
}
if (!total) out.append(el('div', { class: 'hint', text: '这些规则都没有匹配到内容' }))
counts.textContent = total ? `共 ${total} 条,已去重` : ''
}
root.append(
pane('要抓什么', el('div', { class: 'row' }, btn('全选', () => { Object.keys(RULES).forEach((k) => picks.add(k)); root.querySelectorAll('.chip-btn').forEach((c) => c.classList.add('on')); run() }, 'ghost'), btn('只留链接', () => { picks.clear(); picks.add('url'); root.querySelectorAll('.chip-btn').forEach((c, i) => c.classList.toggle('on', Object.keys(RULES)[i] === 'url')); run() }, 'ghost'))),
chips, src, counts, out
)
onInput(src, run, 250)
},
})
})()