|
| 1 | +/** |
| 2 | + * WebInboxPanel — web 扩展收件服务(v0.20.4 / REQ-304)。 |
| 3 | + * |
| 4 | + * @ai-context: 只绑 127.0.0.1 随机端口 + 随机 token(首启生成持久化);扩展在 |
| 5 | + * 已登录 DOM 抽取(公众号/知乎正解)→ POST /ingest 单向投递; |
| 6 | + * 本面板=启动/停止/参数展示(端口+token 复制给扩展安装配置)。 |
| 7 | + */ |
| 8 | +import { useCallback, useEffect, useState } from "react"; |
| 9 | +import { invoke } from "@tauri-apps/api/core"; |
| 10 | + |
| 11 | +interface InboxView { |
| 12 | + running: boolean; |
| 13 | + port: number | null; |
| 14 | + token: string | null; |
| 15 | + inbox_url: string | null; |
| 16 | +} |
| 17 | + |
| 18 | +const btn: React.CSSProperties = { padding: "5px 10px", cursor: "pointer", fontSize: 12, borderRadius: 6 }; |
| 19 | +const okBtn: React.CSSProperties = { ...btn, background: "#0d9488", color: "#fff", border: "none" }; |
| 20 | +const ghostBtn: React.CSSProperties = { ...btn, background: "#fff", border: "1px solid #e5e7eb", color: "#374151" }; |
| 21 | + |
| 22 | +export function WebInboxPanel() { |
| 23 | + const [view, setView] = useState<InboxView | null>(null); |
| 24 | + const [msg, setMsg] = useState(""); |
| 25 | + const [err, setErr] = useState(""); |
| 26 | + |
| 27 | + const reload = useCallback(async () => { |
| 28 | + try { |
| 29 | + setErr(""); |
| 30 | + setView(await invoke<InboxView>("web_inbox_status")); |
| 31 | + } catch (e) { |
| 32 | + setErr(String(e)); |
| 33 | + } |
| 34 | + }, []); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + void reload(); |
| 38 | + }, [reload]); |
| 39 | + |
| 40 | + const start = async () => { |
| 41 | + try { |
| 42 | + setView(await invoke<InboxView>("web_inbox_start")); |
| 43 | + setMsg("✓ 收件服务已启动——仅本机回环可达(随机端口 + token 鉴权)"); |
| 44 | + } catch (e) { |
| 45 | + setErr(String(e)); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const stop = async () => { |
| 50 | + try { |
| 51 | + await invoke("web_inbox_stop"); |
| 52 | + setMsg("已停止(token 保留——下次启动同 token,扩展零重配)"); |
| 53 | + await reload(); |
| 54 | + } catch (e) { |
| 55 | + setErr(String(e)); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const copy = async () => { |
| 60 | + if (!view?.token) return; |
| 61 | + try { |
| 62 | + await navigator.clipboard.writeText( |
| 63 | + `端口:${view.port}\nToken:${view.token}`, |
| 64 | + ); |
| 65 | + setMsg("已复制端口与 token——粘贴到浏览器扩展弹窗(app/extension-web-clipper 手动加载)"); |
| 66 | + } catch (e) { |
| 67 | + setErr(`复制失败: ${e}`); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <div> |
| 73 | + <div style={{ fontSize: 12, color: "#374151", marginBottom: 6 }}> |
| 74 | + <b>🛬 本地收件(浏览器扩展投递)</b>{" "} |
| 75 | + <span style={{ fontSize: 11, color: "#9ca3af" }}> |
| 76 | + 登录墙正解:扩展读已登录 DOM → 本服务(127.0.0.1 + token 单向投递)→ kind=web 会话 |
| 77 | + </span> |
| 78 | + </div> |
| 79 | + {msg && <div style={{ fontSize: 11, color: "#047857", marginBottom: 4 }}>{msg}</div>} |
| 80 | + {err && <div style={{ fontSize: 11, color: "#dc2626", marginBottom: 4 }}>{err}</div>} |
| 81 | + <div style={{ display: "flex", gap: 6, alignItems: "center", flexWrap: "wrap" }}> |
| 82 | + {!view?.running ? ( |
| 83 | + <button style={okBtn} onClick={() => void start()}>▶ 启动收件服务</button> |
| 84 | + ) : ( |
| 85 | + <> |
| 86 | + <span style={{ fontSize: 12 }}>端口 <b>{view.port}</b></span> |
| 87 | + <code style={{ fontSize: 11, background: "#f9fafb", padding: "2px 6px", borderRadius: 4 }}>{view.token}</code> |
| 88 | + <button style={ghostBtn} onClick={() => void copy()}>复制参数</button> |
| 89 | + <button style={{ ...ghostBtn, color: "#dc2626" }} onClick={() => void stop()}>⏹ 停止</button> |
| 90 | + </> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + <div style={{ fontSize: 11, color: "#9ca3af", marginTop: 4 }}> |
| 94 | + 安装扩展:Edge/Chrome 扩展管理页开启开发者模式 → 「加载已解压的扩展程序」→ 选 app/extension-web-clipper(投递契约见 docs/Foresight/web-capture-extension-protocol.md) |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +export default WebInboxPanel; |
0 commit comments