mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 17:25:15 +08:00
feat: 扩展中心支持多模态消息、HTML 渲染、一级导航与应用内通知
This commit is contained in:
1 parent
b20e514c90
commit
4ea73c3414
63 files changed
+8360
-644
No files matched your search
@@ -0,0 +1,251 @@
|
||||
'use strict'
|
||||
|
||||
const EXTENSION_ID = 'com.example.morning-digest'
|
||||
const PAGE_SOURCE = 'tracememo-extension-page'
|
||||
const HOST_SOURCE = 'tracememo-extension-host'
|
||||
const CONFIG_KEY = 'config'
|
||||
const LAST_DIGEST_KEY = 'lastDigest'
|
||||
|
||||
const pending = new Map()
|
||||
let seq = 0
|
||||
let groups = []
|
||||
let lastDigest = null
|
||||
let config = {
|
||||
groupIds: [],
|
||||
groupNames: {},
|
||||
windowHours: 24,
|
||||
enableImageOcr: true,
|
||||
enableImageVision: false,
|
||||
enableVoiceTranscribe: true,
|
||||
maxMediaPerGroup: 6,
|
||||
renderWidth: 460,
|
||||
autoSendWechat: false,
|
||||
wechatTargetGroupId: ''
|
||||
}
|
||||
|
||||
function callHost(method, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const requestId = `req-${++seq}`
|
||||
pending.set(requestId, { resolve, reject })
|
||||
window.parent.postMessage(
|
||||
{ source: PAGE_SOURCE, extensionId: EXTENSION_ID, requestId, method, params: params || {} },
|
||||
'*'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
window.addEventListener('message', (event) => {
|
||||
const data = event.data
|
||||
if (!data || data.source !== HOST_SOURCE) return
|
||||
const item = pending.get(data.requestId)
|
||||
if (!item) return
|
||||
pending.delete(data.requestId)
|
||||
if (data.ok) item.resolve(data.result)
|
||||
else item.reject(new Error((data.error && data.error.message) || '请求失败'))
|
||||
})
|
||||
|
||||
function setBanner(text, tone) {
|
||||
const el = document.getElementById('banner')
|
||||
el.textContent = text
|
||||
el.dataset.tone = tone || ''
|
||||
}
|
||||
|
||||
function renderGroupList() {
|
||||
const keyword = document.getElementById('group-search').value.trim().toLowerCase()
|
||||
const container = document.getElementById('group-list')
|
||||
container.textContent = ''
|
||||
const filtered = groups.filter((g) => !keyword || g.name.toLowerCase().includes(keyword))
|
||||
if (filtered.length === 0) {
|
||||
const empty = document.createElement('div')
|
||||
empty.className = 'group-item'
|
||||
empty.textContent = groups.length === 0 ? '暂无可选群聊(请先连接微信数据库)' : '没有匹配的群聊'
|
||||
container.appendChild(empty)
|
||||
return
|
||||
}
|
||||
const selectedSet = new Set(config.groupIds)
|
||||
for (const group of filtered) {
|
||||
const label = document.createElement('label')
|
||||
label.className = 'group-item'
|
||||
const input = document.createElement('input')
|
||||
input.type = 'checkbox'
|
||||
input.checked = selectedSet.has(group.id)
|
||||
input.addEventListener('change', () => {
|
||||
if (input.checked) {
|
||||
if (!config.groupIds.includes(group.id) && config.groupIds.length < 10) {
|
||||
config.groupIds.push(group.id)
|
||||
config.groupNames[group.id] = group.name
|
||||
} else {
|
||||
input.checked = false
|
||||
}
|
||||
} else {
|
||||
config.groupIds = config.groupIds.filter((id) => id !== group.id)
|
||||
}
|
||||
})
|
||||
const span = document.createElement('span')
|
||||
span.textContent = group.name
|
||||
label.appendChild(input)
|
||||
label.appendChild(span)
|
||||
container.appendChild(label)
|
||||
}
|
||||
}
|
||||
|
||||
function renderTargetSelect() {
|
||||
const select = document.getElementById('wechat-target')
|
||||
select.textContent = ''
|
||||
const none = document.createElement('option')
|
||||
none.value = ''
|
||||
none.textContent = '不自动发送'
|
||||
select.appendChild(none)
|
||||
for (const group of groups) {
|
||||
const opt = document.createElement('option')
|
||||
opt.value = group.id
|
||||
opt.textContent = group.name
|
||||
select.appendChild(opt)
|
||||
}
|
||||
select.value = config.wechatTargetGroupId || ''
|
||||
}
|
||||
|
||||
function syncForm() {
|
||||
document.getElementById('window-hours').value = String(config.windowHours || 24)
|
||||
document.getElementById('enable-ocr').checked = config.enableImageOcr !== false
|
||||
document.getElementById('enable-vision').checked = config.enableImageVision === true
|
||||
document.getElementById('enable-voice').checked = config.enableVoiceTranscribe !== false
|
||||
document.getElementById('max-media').value = String(config.maxMediaPerGroup ?? 6)
|
||||
document.getElementById('render-width').value = String(config.renderWidth ?? 460)
|
||||
document.getElementById('auto-send').checked = config.autoSendWechat === true
|
||||
renderGroupList()
|
||||
renderTargetSelect()
|
||||
}
|
||||
|
||||
function collectForm() {
|
||||
config.windowHours = Number(document.getElementById('window-hours').value) || 24
|
||||
config.enableImageOcr = document.getElementById('enable-ocr').checked
|
||||
config.enableImageVision = document.getElementById('enable-vision').checked
|
||||
config.enableVoiceTranscribe = document.getElementById('enable-voice').checked
|
||||
config.maxMediaPerGroup = Number(document.getElementById('max-media').value) || 0
|
||||
config.renderWidth = Number(document.getElementById('render-width').value) || 460
|
||||
config.wechatTargetGroupId = document.getElementById('wechat-target').value
|
||||
config.autoSendWechat = document.getElementById('auto-send').checked
|
||||
}
|
||||
|
||||
function renderDigestResult(record) {
|
||||
const el = document.getElementById('digest-result')
|
||||
const sendBtn = document.getElementById('send-last-btn')
|
||||
const previewWrap = document.getElementById('digest-preview-wrap')
|
||||
const previewImg = document.getElementById('digest-preview-img')
|
||||
const previewCaption = document.getElementById('digest-preview-caption')
|
||||
if (!record || !record.headline) {
|
||||
el.className = 'result-body empty'
|
||||
el.textContent = '尚未生成晨报,勾选群聊后点击右上角「立即生成今日晨报」。'
|
||||
previewWrap.hidden = true
|
||||
previewImg.removeAttribute('src')
|
||||
sendBtn.disabled = true
|
||||
return
|
||||
}
|
||||
el.className = 'result-body'
|
||||
const pixelMeta =
|
||||
record.pixelWidth && record.pixelHeight
|
||||
? `(实际像素 ${record.pixelWidth}×${record.pixelHeight}px,${record.deviceScaleFactor || 1}x)`
|
||||
: ''
|
||||
const lines = [
|
||||
`【标题】${record.headline}(${record.dateLabel})`,
|
||||
`【统计】覆盖 ${record.groupCount} 个群 · ${record.totalMessages} 条消息 · 解析 ${record.totalMediaResolved} 条媒体`,
|
||||
`【长图】逻辑 ${record.width}×${record.height}px${pixelMeta} · ${(Number(record.bytes || 0) / 1024).toFixed(1)}KB`,
|
||||
`【输出文件】${record.imagePath}`
|
||||
]
|
||||
if (Array.isArray(record.highlights) && record.highlights.length > 0) {
|
||||
lines.push('', '【今日重点】', ...record.highlights.map((item, idx) => `${idx + 1}. ${item}`))
|
||||
}
|
||||
if (Array.isArray(record.actionItems) && record.actionItems.length > 0) {
|
||||
lines.push('', '【待办与约定】', ...record.actionItems.map((item, idx) => `${idx + 1}. ${item}`))
|
||||
}
|
||||
el.textContent = lines.join('\n')
|
||||
if (record.previewUrl) {
|
||||
previewWrap.hidden = false
|
||||
previewCaption.textContent = `长图实时预览(受控扩展私有媒体协议):${record.previewUrl}`
|
||||
previewImg.src = `${record.previewUrl}?t=${record.generatedAt || Date.now()}`
|
||||
} else {
|
||||
previewWrap.hidden = true
|
||||
previewImg.removeAttribute('src')
|
||||
}
|
||||
sendBtn.disabled = false
|
||||
}
|
||||
|
||||
document.getElementById('group-search').addEventListener('input', renderGroupList)
|
||||
|
||||
document.getElementById('save-btn').addEventListener('click', async () => {
|
||||
collectForm()
|
||||
try {
|
||||
await callHost('storage.set', { key: CONFIG_KEY, value: config })
|
||||
setBanner('配置已保存。', 'ok')
|
||||
} catch (error) {
|
||||
setBanner(`保存失败:${error.message}`, 'error')
|
||||
}
|
||||
})
|
||||
|
||||
document.getElementById('generate-btn').addEventListener('click', async () => {
|
||||
collectForm()
|
||||
if (config.groupIds.length === 0) {
|
||||
setBanner('请先勾选至少一个群聊。', 'error')
|
||||
return
|
||||
}
|
||||
const btn = document.getElementById('generate-btn')
|
||||
btn.disabled = true
|
||||
setBanner('正在分页拉取消息、按需解析图片/语音并离线渲染长图…')
|
||||
try {
|
||||
await callHost('storage.set', { key: CONFIG_KEY, value: config })
|
||||
const result = await callHost('extension.request', {
|
||||
method: 'generateDigest',
|
||||
params: config
|
||||
})
|
||||
lastDigest = result
|
||||
renderDigestResult(result)
|
||||
setBanner(
|
||||
result.sentToWechat
|
||||
? '今日晨报已生成、写入通知中心并已发送至目标群聊。'
|
||||
: '今日晨报已生成并写入扩展通知中心。',
|
||||
'ok'
|
||||
)
|
||||
} catch (error) {
|
||||
setBanner(`生成失败:${error.message}`, 'error')
|
||||
} finally {
|
||||
btn.disabled = false
|
||||
}
|
||||
})
|
||||
|
||||
document.getElementById('send-last-btn').addEventListener('click', async () => {
|
||||
collectForm()
|
||||
if (!config.wechatTargetGroupId) {
|
||||
setBanner('请先在「发送目标群聊」下拉框中选择一个群聊。', 'error')
|
||||
return
|
||||
}
|
||||
try {
|
||||
await callHost('extension.request', {
|
||||
method: 'sendLastDigest',
|
||||
params: { to: config.wechatTargetGroupId }
|
||||
})
|
||||
setBanner('已通过 TraceMemo Gateway 发送晨报长图。', 'ok')
|
||||
} catch (error) {
|
||||
setBanner(`发送失败:${error.message}`, 'error')
|
||||
}
|
||||
})
|
||||
|
||||
async function init() {
|
||||
const [savedConfig, savedDigest, roster] = await Promise.all([
|
||||
callHost('storage.get', { key: CONFIG_KEY }).catch(() => null),
|
||||
callHost('storage.get', { key: LAST_DIGEST_KEY }).catch(() => null),
|
||||
callHost('contacts.listGroups', {}).catch(() => ({ groups: [] }))
|
||||
])
|
||||
if (savedConfig && typeof savedConfig === 'object') {
|
||||
config = { ...config, ...savedConfig }
|
||||
}
|
||||
groups = Array.isArray(roster && roster.groups) ? roster.groups : []
|
||||
lastDigest = savedDigest
|
||||
syncForm()
|
||||
renderDigestResult(lastDigest)
|
||||
setBanner('AI 晨报已就绪。建议先点击「立即生成今日晨报」离线预览。')
|
||||
}
|
||||
|
||||
init().catch((error) => {
|
||||
setBanner(`初始化失败:${error.message}`, 'error')
|
||||
})
|
||||
@@ -0,0 +1,96 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>AI 晨报</title>
|
||||
<link rel="stylesheet" href="./style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="page">
|
||||
<header class="hero">
|
||||
<div>
|
||||
<div class="kicker">TraceMemo 官方模板 · com.example.morning-digest</div>
|
||||
<h1>AI 晨报</h1>
|
||||
<p class="subtitle">分页拉取群聊记录、按需解析图片与语音、AI 总结并离线生成 PNG 长图,支持通知提醒与可选微信发送。</p>
|
||||
</div>
|
||||
<div class="hero-actions">
|
||||
<button id="generate-btn" class="btn primary" type="button">立即生成今日晨报</button>
|
||||
<button id="save-btn" class="btn" type="button">保存配置</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div id="banner" class="banner" role="status">正在加载配置与群聊列表…</div>
|
||||
|
||||
<div class="grid">
|
||||
<section class="card">
|
||||
<h2>1. 选择采集群聊与时间窗</h2>
|
||||
<div class="field">
|
||||
<label for="window-hours">采集范围(小时)</label>
|
||||
<select id="window-hours">
|
||||
<option value="24">最近 24 小时</option>
|
||||
<option value="48">最近 48 小时</option>
|
||||
<option value="72">最近 72 小时</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="group-search">搜索并勾选目标群聊(最多 10 个)</label>
|
||||
<input id="group-search" type="search" placeholder="按群名筛选…">
|
||||
<div id="group-list" class="group-list"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h2>2. 按需媒体解析与长图渲染</h2>
|
||||
<label class="check-row">
|
||||
<input id="enable-ocr" type="checkbox" checked>
|
||||
<span>图片本地 OCR 文字识别(纯本地,优先复用索引缓存)</span>
|
||||
</label>
|
||||
<label class="check-row">
|
||||
<input id="enable-vision" type="checkbox">
|
||||
<span>图片 AI 视觉理解(需配置视觉模型)</span>
|
||||
</label>
|
||||
<label class="check-row">
|
||||
<input id="enable-voice" type="checkbox" checked>
|
||||
<span>语音本地转文字(复用本机语音转写服务)</span>
|
||||
</label>
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label for="max-media">每群最多解析媒体数</label>
|
||||
<input id="max-media" type="number" min="0" max="15" value="6">
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="render-width">长图宽度(px)</label>
|
||||
<input id="render-width" type="number" min="320" max="960" value="460">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3>3. 可选微信发送(默认关闭)</h3>
|
||||
<div class="field">
|
||||
<label for="wechat-target">发送目标群聊</label>
|
||||
<select id="wechat-target">
|
||||
<option value="">不自动发送</option>
|
||||
</select>
|
||||
</div>
|
||||
<label class="check-row">
|
||||
<input id="auto-send" type="checkbox">
|
||||
<span>生成后自动发送晨报长图到所选目标群(默认关闭,建议先预览)</span>
|
||||
</label>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<section class="card result-card">
|
||||
<div class="result-head">
|
||||
<h2>最近一次晨报结果</h2>
|
||||
<button id="send-last-btn" class="btn" type="button" disabled>手动发送长图到目标群</button>
|
||||
</div>
|
||||
<div id="digest-result" class="result-body empty">尚未生成晨报,勾选群聊后点击右上角「立即生成今日晨报」。</div>
|
||||
<div id="digest-preview-wrap" class="preview-wrap" hidden>
|
||||
<div class="preview-caption" id="digest-preview-caption"></div>
|
||||
<img id="digest-preview-img" class="preview-img" alt="晨报长图预览">
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<script src="./app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,225 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 24px;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
background: #f5f7fa;
|
||||
color: #18212f;
|
||||
}
|
||||
|
||||
.page {
|
||||
max-width: 960px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.hero {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 16px;
|
||||
background: #ffffff;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-radius: 14px;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.kicker {
|
||||
font-size: 12px;
|
||||
color: #2563eb;
|
||||
font-weight: 600;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.hero h1 {
|
||||
margin: 0 0 6px;
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.hero-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 8px 14px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #cbd5e1;
|
||||
background: #ffffff;
|
||||
color: #1e293b;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn.primary {
|
||||
background: #2563eb;
|
||||
border-color: #2563eb;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.banner {
|
||||
padding: 10px 14px;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
background: #eff6ff;
|
||||
color: #1d4ed8;
|
||||
border: 1px solid #bfdbfe;
|
||||
}
|
||||
|
||||
.banner[data-tone="error"] {
|
||||
background: #fef2f2;
|
||||
color: #b91c1c;
|
||||
border-color: #fecaca;
|
||||
}
|
||||
|
||||
.banner[data-tone="ok"] {
|
||||
background: #f0fdf4;
|
||||
color: #15803d;
|
||||
border-color: #bbf7d0;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: #ffffff;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-radius: 14px;
|
||||
padding: 18px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.card h2 {
|
||||
margin: 0;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.card h3 {
|
||||
margin: 8px 0 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.field input,
|
||||
.field select {
|
||||
padding: 8px 10px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #cbd5e1;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.field-row {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.check-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.group-list {
|
||||
max-height: 220px;
|
||||
overflow: auto;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 8px;
|
||||
padding: 6px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.group-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 8px;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.group-item:hover {
|
||||
background: #f8fafc;
|
||||
}
|
||||
|
||||
.result-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.result-body {
|
||||
font-size: 13px;
|
||||
line-height: 1.65;
|
||||
white-space: pre-wrap;
|
||||
background: #f8fafc;
|
||||
border-radius: 10px;
|
||||
padding: 14px;
|
||||
}
|
||||
|
||||
.result-body.empty {
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.preview-wrap {
|
||||
margin-top: 14px;
|
||||
padding: 12px;
|
||||
border-radius: 12px;
|
||||
background: #f8fafc;
|
||||
border: 1px solid #e2e8f0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.preview-caption {
|
||||
font-size: 12px;
|
||||
color: #64748b;
|
||||
word-break: break-all;
|
||||
align-self: flex-start;
|
||||
}
|
||||
|
||||
.preview-img {
|
||||
max-width: 100%;
|
||||
width: 460px;
|
||||
height: auto;
|
||||
border-radius: 10px;
|
||||
border: 1px solid #cbd5e1;
|
||||
box-shadow: 0 6px 20px rgba(15, 23, 42, 0.08);
|
||||
background: #ffffff;
|
||||
}
|
||||
Reference in new issue
Block a user