export const EXPORT_PAGE_SIZE = 240
export const exportStyles = `
:root {
color-scheme: light;
--page: #edf2f0;
--panel: #fff;
--text: #1d2a25;
--muted: #68766f;
--border: #d8e2dc;
--mine: #d9f0e2;
--accent: #176b57;
--accent-soft: #e4f2ec;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
margin: 0;
background: var(--page);
color: var(--text);
font: 14px system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
}
.archive-loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-content: center;
justify-items: center;
gap: 10px;
padding: 24px;
background: var(--page);
color: var(--text);
text-align: center;
transition: opacity .2s ease, visibility .2s ease;
}
.archive-loading.complete {
visibility: hidden;
opacity: 0;
pointer-events: none;
}
.archive-loading-indicator {
width: 34px;
height: 34px;
margin-bottom: 4px;
border: 3px solid #c9d9d1;
border-top-color: var(--accent);
border-radius: 50%;
animation: archive-loading-spin .8s linear infinite;
}
.archive-loading strong { font-size: 16px; }
.archive-loading span { color: var(--muted); font-size: 13px; }
.archive-loading.error .archive-loading-indicator {
display: grid;
border: 0;
animation: none;
place-items: center;
color: #a33b32;
font-size: 28px;
}
.archive-loading.error .archive-loading-indicator::before { content: '!'; }
@keyframes archive-loading-spin { to { transform: rotate(360deg); } }
.page {
width: 100%;
max-width: 1380px;
min-width: 0;
height: 100vh;
margin: auto;
padding: 20px 24px;
display: flex;
flex-direction: column;
}
.toolbar {
display: grid;
width: 100%;
min-width: 0;
grid-template-columns: minmax(220px, 1fr) auto;
gap: 14px 24px;
align-items: center;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 18px;
padding: 16px 20px;
box-shadow: 0 8px 24px #29483b12;
}
.archive-heading {
display: flex;
align-items: center;
gap: 8px 12px;
min-width: 0;
flex-wrap: wrap;
}
.title { font-size: 18px; font-weight: 750; }
.controls { display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.controls input, .filter-button {
border: 1px solid var(--border);
border-radius: 10px;
padding: 8px 11px;
background: #fff;
color: var(--text);
font: inherit;
}
.controls input[type=search] { width: min(320px, 34vw); }
.filters {
grid-column: 1 / -1;
display: flex;
gap: 7px;
align-items: center;
flex-wrap: wrap;
}
.filter-button { cursor: pointer; }
.filter-button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.count { margin-left: auto; color: var(--muted); font-size: 13px; }
.archive-layout {
display: grid;
width: 100%;
max-width: 100%;
min-width: 0;
grid-template-columns: 168px minmax(0, 1fr);
gap: 18px;
min-height: 0;
flex: 1;
margin-top: 16px;
}
.archive-layout.single-conversation { grid-template-columns: 168px minmax(0, 1fr); }
.archive-navigation {
display: flex;
width: 100%;
max-width: 100%;
flex-direction: column;
gap: 10px;
min-width: 0;
min-height: 0;
}
.conversation-filter {
position: relative;
display: inline-flex;
flex: 0 1 auto;
min-width: 0;
max-width: 100%;
height: 42px;
border-radius: 10px;
transition: background .15s ease;
}
.conversation-filter[hidden] { display: none; }
.conversation-trigger {
display: flex;
width: auto;
max-width: 280px;
min-width: 0;
align-items: center;
gap: 9px;
border: 0;
border-radius: 10px;
padding: 5px 9px 5px 6px;
background: transparent;
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
transition: background .15s ease;
}
.conversation-trigger:hover, .conversation-trigger[aria-expanded=true] { background: #edf5f1; }
.conversation-trigger:focus-visible { outline: 2px solid #5b9785; outline-offset: 1px; }
.conversation-trigger-avatar {
position: relative;
display: grid;
width: 30px;
height: 30px;
flex: 0 0 30px;
overflow: hidden;
border-radius: 8px;
place-items: center;
background: #dcebe4;
color: var(--accent);
font-size: 12px;
font-weight: 700;
}
.conversation-trigger-avatar img { width: 100%; height: 100%; object-fit: cover; }
.conversation-trigger-avatar.multiple { overflow: visible; background: transparent; }
.conversation-trigger-avatar.multiple img {
position: absolute;
width: 21px;
height: 21px;
border: 2px solid #f7fbf9;
border-radius: 6px;
}
.conversation-trigger-avatar.multiple img:first-child { top: 0; left: 0; }
.conversation-trigger-avatar.multiple img:last-child { right: 0; bottom: 0; }
.conversation-trigger-name {
flex: 0 1 auto;
min-width: 0;
max-width: 220px;
overflow: hidden;
color: var(--text);
font-size: 15px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
.conversation-switch-icon {
width: 14px;
height: 14px;
flex: 0 0 14px;
color: #5c6963;
stroke-width: 2;
}
.conversation-menu {
position: absolute;
z-index: 12;
top: calc(100% + 7px);
left: 0;
width: max(100%, 240px);
max-width: calc(100vw - 32px);
max-height: min(360px, 55vh);
overflow-y: auto;
border: 0;
border-radius: 10px;
padding: 6px;
background: #fff;
box-shadow: 0 10px 30px #19392d2b, 0 2px 8px #19392d14;
}
.conversation-menu[hidden] { display: none; }
.conversation-option {
display: flex;
width: 100%;
min-width: 0;
align-items: center;
gap: 10px;
border: 0;
border-radius: 7px;
padding: 8px;
background: transparent;
color: var(--text);
font: inherit;
text-align: left;
cursor: pointer;
}
.conversation-option:hover, .conversation-option:focus-visible { background: #f0f6f3; outline: 0; }
.conversation-option[aria-selected=true] { background: var(--accent-soft); color: var(--accent); }
.conversation-option-avatar {
display: grid;
width: 30px;
height: 30px;
flex: 0 0 30px;
overflow: hidden;
border-radius: 8px;
place-items: center;
background: #dcebe4;
color: var(--accent);
font-size: 12px;
font-weight: 700;
}
.conversation-option-avatar img { width: 100%; height: 100%; object-fit: cover; }
.conversation-option-avatar.multiple { position: relative; overflow: visible; background: transparent; }
.conversation-option-avatar.multiple img {
position: absolute;
width: 21px;
height: 21px;
border: 2px solid #fff;
border-radius: 6px;
}
.conversation-option-avatar.multiple img:first-child { top: 0; left: 0; }
.conversation-option-avatar.multiple img:last-child { right: 0; bottom: 0; }
.conversation-option-name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.conversation-option-check { width: 16px; flex: 0 0 16px; color: var(--accent); text-align: center; }
.timeline {
flex: 1;
overflow: auto;
background: #f7faf8;
border: 1px solid var(--border);
border-radius: 16px;
padding: 12px 9px;
}
.timeline-empty { padding: 10px; color: var(--muted); font-size: 12px; }
.timeline-year-group + .timeline-year-group { margin-top: 4px; }
.timeline-year {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
margin: 0;
padding: 7px 8px;
border: 0;
border-radius: 6px;
background: transparent;
color: var(--text);
font: inherit;
font-size: 13px;
font-weight: 700;
cursor: pointer;
text-align: left;
}
.timeline-year::after {
content: '⌄';
color: var(--muted);
font-size: 16px;
font-weight: 400;
line-height: 1;
transform: rotate(-90deg);
transition: transform .15s ease;
}
.timeline-year:hover { background: #eaf2ee; }
.timeline-year[aria-expanded=true]::after { transform: rotate(0); }
.timeline-months[hidden] { display: none; }
.timeline-month {
width: 100%;
display: flex;
justify-content: space-between;
gap: 8px;
border: 0;
border-left: 3px solid transparent;
border-radius: 8px;
background: transparent;
color: var(--muted);
padding: 7px 8px;
cursor: pointer;
text-align: left;
}
.timeline-month:hover, .timeline-month.active {
border-left-color: var(--accent);
background: var(--accent-soft);
color: var(--accent);
}
.timeline-month small { color: inherit; }
.scroll { width: 100%; max-width: 100%; overflow: auto; min-width: 0; padding: 10px 8px 36px; }
.lazy-hint {
width: min(100%, 820px);
margin: 0 auto 12px;
padding: 7px 12px;
border-radius: 999px;
background: #e4ece8;
color: var(--muted);
font-size: 12px;
text-align: center;
}
.message {
display: flex;
flex-direction: column;
gap: 6px;
width: min(100%, 820px);
min-width: 0;
margin: 0 auto 22px;
}
.locate-all {
position: relative;
display: grid;
width: 28px;
height: 28px;
flex: 0 0 auto;
border: 1px solid #b8d4c7;
border-radius: 50%;
padding: 0;
place-items: center;
background: #fff;
color: var(--accent);
font: inherit;
cursor: pointer;
opacity: 0;
pointer-events: none;
box-shadow: 0 3px 10px #29483b1a;
transition: opacity .15s ease, background .15s ease;
}
.locate-icon { font-size: 16px; line-height: 1; }
.locate-label {
position: absolute;
z-index: 3;
top: 50%;
left: calc(100% + 7px);
border: 1px solid #b8d4c7;
border-radius: 7px;
padding: 5px 8px;
background: #fff;
color: var(--accent);
font-size: 11px;
font-weight: 650;
line-height: 1.4;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transform: translate(-2px, -50%);
box-shadow: 0 3px 10px #29483b1a;
transition: opacity .15s ease, transform .15s ease;
}
.message.sent .locate-label { right: calc(100% + 7px); left: auto; transform: translate(2px, -50%); }
.message:hover .locate-all, .locate-all:focus-visible {
opacity: 1;
pointer-events: auto;
}
.locate-all:hover, .locate-all:focus-visible { background: var(--accent-soft); outline: none; }
.locate-all:hover .locate-label, .locate-all:focus-visible .locate-label {
opacity: 1;
transform: translate(0, -50%);
}
@media (hover: none) {
.locate-all { opacity: 1; pointer-events: auto; }
.locate-label { display: none; }
}
.message.located .bubble { animation: locate-message 1.5s ease-out; }
@keyframes locate-message {
0%, 32% { outline: 3px solid #36a477; outline-offset: 3px; }
100% { outline: 3px solid transparent; outline-offset: 3px; }
}
@media (prefers-reduced-motion: reduce) {
.message.located .bubble { animation: none; outline: 3px solid #36a477; outline-offset: 3px; }
.archive-loading-indicator { animation: none; }
}
.message.sent { align-items: flex-end; }
.message.system { align-items: center; }
.message.system .row {
position: relative;
width: fit-content;
max-width: 92%;
justify-content: center;
}
.message.system .locate-all {
position: absolute;
top: 50%;
left: calc(100% + 10px);
transform: translateY(-50%);
}
.message.system .avatar { display: none; }
.message.system .message-stack {
width: max-content;
max-width: 100%;
}
.message.system .bubble {
width: max-content;
max-width: 100%;
padding: 5px 10px;
border: 0;
border-radius: 5px;
background: #e9eeeb;
color: var(--muted);
font-size: 11px;
text-align: center;
box-shadow: none;
}
.message.system .sender { display: none; }
.time { color: var(--muted); font-size: 11px; margin: 0 12px; }
.conversation-source {
display: inline-block;
margin-left: 8px;
padding: 2px 6px;
border-radius: 5px;
background: var(--accent-soft);
color: var(--accent);
}
.row { display: flex; width: 100%; min-width: 0; gap: 12px; align-items: flex-end; max-width: 100%; }
.sent .row { flex-direction: row-reverse; }
.avatar {
width: 38px;
height: 38px;
flex: 0 0 auto;
border-radius: 50%;
overflow: hidden;
background: #dcebe4;
display: grid;
place-items: center;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.message-stack {
min-width: 0;
max-width: min(78%, 760px);
display: grid;
gap: 6px;
}
.bubble {
min-width: 0;
max-width: 100%;
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 10px 18px 18px 18px;
background: #fff;
box-shadow: 0 4px 12px #29483b0d;
}
.sent .bubble { background: var(--mine); border-color: #c7e6d4; border-radius: 18px 10px 18px 18px; }
.sender { color: var(--muted); font-size: 12px; margin-bottom: 5px; }
.content { line-height: 1.7; word-break: break-word; white-space: pre-wrap; }
.audio-wrap { width: 380px; max-width: 100%; min-width: 0; }
.search-highlight {
border-radius: 3px;
padding: 0 1px;
background: #ffe58f;
color: inherit;
box-decoration-break: clone;
-webkit-box-decoration-break: clone;
}
.audio { display: block; width: 100%; max-width: 100%; height: 38px; }
.voice-transcript {
width: 100%;
max-width: 100%;
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid #cad8d1;
color: #3c4742;
font-size: 13px;
line-height: 1.55;
white-space: pre-wrap;
word-break: break-word;
}
.voice-transcript.error { color: #8a5a16; border-top-color: #e4c88f; }
.media-status {
margin-top: 8px;
padding: 6px 8px;
border-left: 3px solid #b27a18;
background: #fff8e8;
color: #79530f;
font-size: 12px;
line-height: 1.5;
}
.file-attachment {
display: flex;
align-items: center;
gap: 9px;
max-width: 320px;
margin: 2px 0 8px;
padding: 10px 12px;
border: 1px solid var(--border);
border-radius: 10px;
background: #f7faf8;
color: var(--accent);
font-weight: 650;
text-decoration: none;
word-break: break-all;
}
.file-attachment:hover { text-decoration: underline; }
.quote-reference {
margin-top: 10px;
padding: 8px 11px;
border-left: 3px solid #8eb4a3;
background: #f1f6f3;
color: var(--muted);
display: grid;
gap: 3px;
}
.quote-reference strong { font-weight: 650; color: var(--text); }
.quote-reference span { white-space: pre-wrap; }
.structured-content {
display: grid;
gap: 7px;
width: min(420px, 100%);
min-width: min(240px, 100%);
}
.structured-kicker {
color: var(--accent);
font-size: 11px;
font-weight: 700;
}
.structured-title {
color: var(--text);
font-size: 15px;
font-weight: 700;
line-height: 1.45;
white-space: pre-wrap;
word-break: break-word;
}
.structured-description {
color: var(--muted);
font-size: 12px;
line-height: 1.55;
white-space: pre-wrap;
word-break: break-word;
}
.structured-footer {
display: flex;
gap: 6px;
align-items: center;
padding-top: 6px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 11px;
}
.structured-link { color: inherit; text-decoration: none; }
.structured-link:hover .structured-title { color: var(--accent); }
.structured-share-articles { display: flex; flex-direction: column; }
.structured-share-article {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 10px;
align-items: center;
padding: 10px 0;
border-top: 1px solid var(--border);
color: inherit;
text-decoration: none;
}
.structured-share-article:first-child { border-top: 0; }
.structured-share-article img { width: 54px; height: 54px; border-radius: 4px; object-fit: cover; }
.location-content {
gap: 8px;
padding-top: 8px;
border-top: 3px solid #3f8f76;
}
.location-content .structured-footer { justify-content: space-between; }
.location-coordinates {
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.location-action { color: var(--accent); font-weight: 650; }
.mini-program-content { border-top: 3px solid #4d9d75; padding-top: 8px; }
.mini-program-preview {
display: block;
width: 100%;
max-height: 260px;
border-radius: 8px;
object-fit: cover;
cursor: zoom-in;
}
.mini-program-icon {
width: 20px;
height: 20px;
flex: 0 0 auto;
border-radius: 4px;
object-fit: cover;
}
.forward-content details { margin-top: 2px; }
.forward-content summary {
color: var(--accent);
cursor: pointer;
font-size: 12px;
font-weight: 650;
}
.forward-list, .forward-list ol {
display: grid;
gap: 7px;
margin: 8px 0 0;
padding-left: 18px;
}
.forward-item { color: var(--muted); font-size: 12px; line-height: 1.5; }
.forward-item strong { color: var(--text); font-weight: 650; }
.forward-item time { margin-left: 6px; font-size: 10px; }
.forward-item span { display: block; white-space: pre-wrap; word-break: break-word; }
.payment-content {
gap: 4px;
padding-left: 12px;
border-left: 4px solid #d15246;
}
.payment-content.transfer { border-left-color: var(--accent); }
.payment-content .structured-title { font-size: 16px; }
.media-image {
display: block;
max-width: 100%;
max-height: 420px;
margin-bottom: 8px;
border-radius: 12px;
object-fit: contain;
background: #eef2f5;
}
.media-image[data-preview] { cursor: zoom-in; }
.empty { display: grid; place-items: center; min-height: 260px; color: var(--muted); text-align: center; }
.lightbox {
position: fixed;
inset: 0;
display: none;
place-items: center;
background: #14231ddd;
z-index: 10;
padding: 24px;
overflow: auto;
}
.lightbox.open { display: grid; }
.lightbox img {
width: min(86vw, 980px);
max-height: 88vh;
object-fit: contain;
cursor: zoom-in;
transform: scale(var(--zoom, 1));
transform-origin: center;
transition: transform .12s ease;
}
.lightbox-close {
position: fixed;
top: 20px;
right: 20px;
z-index: 11;
width: 42px;
height: 42px;
border: 1px solid #ffffff66;
border-radius: 50%;
background: #14231dcc;
color: #fff;
font-size: 30px;
line-height: 1;
cursor: pointer;
}
@media (max-width: 760px) {
html, body { width: 100%; max-width: 100%; overflow-x: hidden; overscroll-behavior-x: none; }
.page { padding: 10px; }
.toolbar {
grid-template-columns: minmax(112px, .7fr) minmax(0, 1.3fr);
gap: 8px;
padding: 10px;
border-radius: 14px;
}
.archive-heading {
grid-column: 1;
min-height: 36px;
gap: 4px;
align-items: center;
flex-wrap: nowrap;
}
.archive-heading .title {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.conversation-filter:not([hidden]) {
display: inline-flex;
width: 100%;
min-width: 0;
max-width: 100%;
flex: 1 1 auto;
height: 36px;
}
.conversation-trigger { width: 100%; max-width: none; gap: 6px; padding: 4px 6px 4px 4px; }
.conversation-trigger-avatar { width: 26px; height: 26px; flex-basis: 26px; border-radius: 7px; }
.conversation-trigger-avatar.multiple img { width: 18px; height: 18px; border-radius: 5px; }
.conversation-trigger-name { flex: 1; max-width: none; font-size: 12px; }
.conversation-switch-icon { width: 13px; height: 13px; flex-basis: 13px; }
.conversation-menu {
width: min(250px, calc(100vw - 20px));
max-width: none;
}
.controls { grid-column: 2; min-width: 0; justify-content: flex-start; }
.controls input[type=search] {
width: 100%;
min-width: 0;
height: 36px;
padding: 0 10px;
font-size: 16px;
}
.filters {
grid-column: 1 / -1;
display: flex;
gap: 4px;
flex-wrap: wrap;
}
.filter-button {
min-height: 30px;
border-radius: 8px;
padding: 4px 6px;
font-size: 12px;
line-height: 1.2;
}
.count {
display: block;
width: 100%;
flex: 0 0 100%;
margin-left: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.archive-layout { grid-template-columns: 1fr; align-content: start; margin-top: 10px; }
.archive-layout.single-conversation { grid-template-columns: 1fr; }
.archive-navigation { align-self: start; gap: 8px; }
.timeline {
align-self: stretch;
display: flex;
width: 100%;
max-width: 100%;
gap: 6px;
overflow: auto;
padding: 8px;
}
.timeline-year-group {
flex: 0 0 auto;
display: flex;
align-items: center;
gap: 6px;
}
.timeline-year-group + .timeline-year-group { margin-top: 0; }
.timeline-year {
flex: 0 0 auto;
width: auto;
gap: 6px;
padding: 7px 8px;
white-space: nowrap;
}
.timeline-months { display: flex; gap: 6px; }
.timeline-months[hidden] { display: none; }
.timeline-month {
flex: 0 0 auto;
width: auto;
border-left: 0;
border-bottom: 3px solid transparent;
}
.timeline-month:hover, .timeline-month.active {
border-left-color: transparent;
border-bottom-color: var(--accent);
}
.scroll {
overflow-x: hidden;
overscroll-behavior-x: none;
touch-action: pan-y;
}
.message:not(.system) .message-stack { max-width: calc(100% - 50px); }
.message:not(.system) .row.has-locate .message-stack { max-width: calc(100% - 90px); }
.audio-wrap { width: min(260px, 100%); }
}
`
const safe = (value: unknown): string =>
String(value ?? '').replace(
/[&<>"']/g,
(character) =>
({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character] ||
character
)
const renderExportScript = (name: string): string => `
(() => {
'use strict'
const loadingOverlay = document.querySelector('#archive-loading')
const loadingTitle = document.querySelector('#archive-loading-title')
const loadingDetail = document.querySelector('#archive-loading-detail')
const finishLoading = () => {
if (!loadingOverlay) return
loadingOverlay.setAttribute('aria-busy', 'false')
loadingOverlay.classList.add('complete')
window.setTimeout(() => loadingOverlay.remove(), 250)
}
const failLoading = (message) => {
if (!loadingOverlay) return
loadingOverlay.setAttribute('aria-busy', 'false')
loadingOverlay.setAttribute('role', 'alert')
loadingOverlay.classList.add('error')
if (loadingTitle) loadingTitle.textContent = '无法加载聊天档案'
if (loadingDetail) loadingDetail.textContent = message
}
const initializeArchive = () => {
try {
const PAGE_SIZE = ${EXPORT_PAGE_SIZE}
const WINDOW_STEP = Math.floor(PAGE_SIZE / 2)
const archive = window.__WECHAT_EXPORT__ || { name: ${JSON.stringify(name)}, messages: [] }
const legacyConversation = archive.sourceId
? [{ id: archive.sourceId, name: archive.name || ${JSON.stringify(name)}, type: 'user', messageCount: 0 }]
: []
const conversations = Array.isArray(archive.conversations) && archive.conversations.length
? archive.conversations
: legacyConversation
const allMessages = (Array.isArray(archive.messages) ? archive.messages : []).map((message) =>
message.exportConversationId || conversations.length !== 1
? message
: Object.assign({}, message, {
exportConversationId: conversations[0].id,
exportConversationName: conversations[0].name
})
)
const list = document.querySelector('#messages')
const layout = document.querySelector('.archive-layout')
const conversationFilter = document.querySelector('#conversation-filter')
const conversationTrigger = document.querySelector('#conversation-trigger')
const conversationTriggerAvatar = document.querySelector('#conversation-trigger-avatar')
const conversationTriggerName = document.querySelector('#conversation-trigger-name')
const conversationMenu = document.querySelector('#conversation-menu')
const timeline = document.querySelector('#timeline')
const query = document.querySelector('#query')
const count = document.querySelector('#count')
const title = document.querySelector('#archive-title')
const filters = document.querySelector('#filters')
const box = document.querySelector('#lightbox')
const preview = document.querySelector('#lightbox-image')
const closeButton = document.querySelector('#lightbox-close')
let activeKind = 'all'
let activeConversation = 'all'
let filtered = []
let windowStart = 0
let windowEnd = 0
let scrollLoadPending = false
let scrollLoadSuppressed = false
let activeMonthUpdatePending = false
let expandedTimelineYear = ''
const tabPositions = new Map()
let lastScrollTop = 0
let zoom = 1
const nextFrame = (callback) => {
if (typeof window.requestAnimationFrame === 'function') {
window.requestAnimationFrame(callback)
} else {
window.setTimeout(callback, 0)
}
}
const esc = (value) => String(value ?? '').replace(
/[&<>"']/g,
(character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[character] || character
)
const decodeEntities = (value) => {
const textarea = document.createElement('textarea')
textarea.innerHTML = String(value ?? '')
return textarea.value
}
const displayText = (value) => esc(decodeEntities(value))
const externalUrl = (value) => {
const decoded = decodeEntities(value).trim()
if (!decoded) return ''
try {
const parsed = new URL(decoded)
return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? esc(decoded) : ''
} catch {
return ''
}
}
const imageUrl = (value) => {
const decoded = decodeEntities(value).trim()
if (decoded.toLowerCase().startsWith('data:image/')) return esc(decoded)
return ''
}
const urlHost = (value) => {
const decoded = decodeEntities(value).trim()
if (!decoded) return ''
try {
const parsed = new URL(decoded)
return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? parsed.hostname : ''
} catch {
return ''
}
}
const pad = (value) => String(value).padStart(2, '0')
const fullTime = (message) => {
const timestamp = Number(message.createTime || 0)
if (!timestamp) return String(message.datetime || '')
const date = new Date(timestamp * 1000)
if (Number.isNaN(date.getTime())) return String(message.datetime || '')
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) +
' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds())
}
const monthKey = (message) => {
const timestamp = Number(message.createTime || 0)
if (!timestamp) return 'unknown'
const date = new Date(timestamp * 1000)
return date.getFullYear() + '-' + pad(date.getMonth() + 1)
}
const kindOf = (message) => {
const data = message.contentData || {}
const shareType = String(data.typeVal || '')
if (
message.exportMediaType === 'file' ||
(data.type === 'share' && (shareType === '6' || shareType === '74'))
) return 'file'
if (
message.exportMediaType === 'image' || message.exportMediaType === 'video' ||
message.exportMediaType === 'sticker' || data.type === 'image' ||
data.type === 'video' || data.type === 'sticker'
) return 'media'
if (message.voiceDataUrl || data.type === 'voice' || message.type === '语音') return 'voice'
if (
data.type === 'system' || data.type === 'unknown' || data.type === 'redPacket' ||
data.type === 'voip' || data.type === 'card' || message.from === 'system' ||
(data.type === 'share' && shareType === '2000') ||
message.type === '微信红包' || message.type === '转账'
) return 'system'
if (
data.type === 'share' || data.type === 'location' ||
data.type === 'miniProgram' || data.type === 'forwardBundle'
) return 'share'
return 'text'
}
const forwardedSearchText = (items) => Array.isArray(items)
? items.flatMap((item) => [
item.sender,
item.sentAt,
item.text,
forwardedSearchText(item.nested)
]).flat().filter(Boolean)
: []
const searchText = (message) => [
message.exportConversationName,
message.name,
message.content,
message.type,
message.contentData && message.contentData.title,
message.contentData && message.contentData.des,
message.contentData && message.contentData.description,
message.contentData && message.contentData.appname,
message.contentData && message.contentData.appName,
message.contentData && message.contentData.quotedSender,
message.contentData && message.contentData.quotedContent,
message.voiceTranscript,
message.contentData && message.contentData.nickname,
message.contentData && message.contentData.username,
message.contentData && message.contentData.poiname,
message.contentData && message.contentData.label,
message.contentData && message.contentData.status,
message.contentData && forwardedSearchText(message.contentData.items),
message.exportMediaName
].filter(Boolean).join(' ').toLowerCase()
const exportedImageOrVideoFileStem = (message) => {
const mediaType = message.exportMediaType || (message.contentData && message.contentData.type)
if (mediaType !== 'image' && mediaType !== 'video') return ''
const mediaUrl = String(message.exportMediaUrl || '').split(/[?#]/, 1)[0]
const fileName = mediaUrl.slice(mediaUrl.lastIndexOf('/') + 1)
if (!fileName) return ''
let decodedFileName = fileName
try {
decodedFileName = decodeURIComponent(fileName)
} catch {}
const extensionIndex = decodedFileName.lastIndexOf('.')
return (extensionIndex > 0 ? decodedFileName.slice(0, extensionIndex) : decodedFileName).toLowerCase()
}
const shareLabel = (typeVal) => {
if (String(typeVal) === '5') return '公众号链接'
if (String(typeVal) === '51') return '视频号'
return '分享'
}
const renderShareContent = (data) => {
const label = shareLabel(data.typeVal)
const articles = Array.isArray(data.articles) ? data.articles : []
if (articles.length) {
const articleMarkup = articles.map((article) => {
const href = externalUrl(article.url)
const cover = imageUrl(article.coverUrl)
const body = '' : '')
return href
? '' + body + ''
: '