/** * Anonymous fallback for public Telegram channels: the t.me/s/ web * preview. Only used while the logged-in session is not live. */ const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' const decode = (s) => String(s || '') .replace(/"/g, '"') .replace(/'/g, "'") .replace(/>/g, '>') .replace(/</g, '<') .replace(/&/g, '&') const strip = (html) => decode(String(html || '').replace(//gi, '\n').replace(/<[^>]+>/g, '')) function parseViews(text) { const m = /^([\d.]+)([KkMm]?)/.exec(String(text || '').replace(/[\s, ]/g, '')) if (!m) return 0 const unit = m[2].toUpperCase() return Math.round(Number(m[1]) * (unit === 'K' ? 1e3 : unit === 'M' ? 1e6 : 1)) || 0 } /** Newest ~20 posts of a public channel as raw { id, text, links, date, views }. */ async function readChannel(username) { const res = await fetch(`https://t.me/s/${encodeURIComponent(username)}`, { headers: { 'user-agent': UA, accept: 'text/html' }, signal: AbortSignal.timeout(20000), }) if (!res.ok) throw new Error(`t.me 预览 HTTP ${res.status}`) const html = await res.text() const title = decode(/ ({ index: m.index, post: m[1] })) const out = [] for (let i = 0; i < anchors.length; i += 1) { const block = html.slice(anchors[i].index, anchors[i + 1]?.index ?? html.length) const id = Number(String(anchors[i].post).split('/')[1]) if (!id) continue const body = /
]*>([\s\S]*?)<\/div>/.exec(block)?.[1] || '' const links = [...block.matchAll(/]+href="(https?:\/\/[^"]+)"/g)].map((m) => decode(m[1])) const iso = /