| 1 | import React, { useEffect, useRef, useState } from "react"; |
| 2 | import { ArrowLeft, RefreshCw, Trash2 } from "lucide-react"; |
| 3 | import { api } from "./api"; |
| 4 | import { InlineSelect } from "./inline-select"; |
| 5 | |
| 6 | export function BrowserCookies({ tab, onClose }) { |
| 7 | const [scope, setScope] = useState(/^https?:/.test(tab.url) ? "site" : "all"); |
| 8 | const [query, setQuery] = useState(""); |
| 9 | const [page, setPage] = useState(0); |
| 10 | const [revision, setRevision] = useState(0); |
| 11 | const [data, setData] = useState(null); |
| 12 | const [value, setValue] = useState(null); |
| 13 | const [error, setError] = useState(""); |
| 14 | const [loading, setLoading] = useState(true); |
| 15 | const [busy, setBusy] = useState(false); |
| 16 | const [confirmClear, setConfirmClear] = useState(false); |
| 17 | const generation = useRef(0), search = useRef(null), operation = useRef(false); |
| 18 | useEffect(() => { search.current?.focus(); }, []); |
| 19 | useEffect(() => { |
| 20 | const version = ++generation.current; |
| 21 | setLoading(true); setData(null); setValue(null); setError(""); setConfirmClear(false); |
| 22 | api("/browser/cookies", { id: tab.id, url: tab.url, mode: "list", scope, query, page }) |
| 23 | .then(result => { if (version === generation.current) setData(result); }) |
| 24 | .catch(error => { if (version === generation.current) setError(error.message); }) |
| 25 | .finally(() => { if (version === generation.current) setLoading(false); }); |
| 26 | return () => { generation.current++; }; |
| 27 | }, [tab.id, scope, query, page, revision]); |
| 28 | const run = async (mode, cookieKey) => { |
| 29 | if (operation.current || loading) return; |
| 30 | operation.current = true; setBusy(true); setError(""); |
| 31 | const version = generation.current; |
| 32 | try { |
| 33 | const result = await api("/browser/cookies", { id: tab.id, url: tab.url, mode, scope, query, page, cookieKey }); |
| 34 | if (version !== generation.current) return; |
| 35 | if (mode === "details") setValue({ key: cookieKey, value: result.value }); |
| 36 | else { setData(result); setValue(null); setConfirmClear(false); } |
| 37 | } catch (error) { |
| 38 | if (version === generation.current) setError(error.message); |
| 39 | } finally { operation.current = false; setBusy(false); } |
| 40 | }; |
| 41 | return <section className="browser-cookies" aria-label="Cookie 管理" onKeyDown={event => { |
| 42 | if (event.key === "Escape") { event.stopPropagation(); onClose(); } |
| 43 | }}> |
| 44 | <div className="cookie-heading"> |
| 45 | <button className="icon-button" aria-label="关闭 Cookie 管理" onClick={onClose}><ArrowLeft size={15} /></button> |
| 46 | <strong>Cookie 管理</strong><span>{tab.incognito ? "无痕会话" : "普通会话"}</span> |
| 47 | <button className="icon-button" aria-label="刷新 Cookie" disabled={busy || loading} |
| 48 | onClick={() => setRevision(revision + 1)}><RefreshCw size={15} /></button> |
| 49 | </div> |
| 50 | <div className="cookie-toolbar"> |
| 51 | <InlineSelect aria-label="Cookie 网站范围" value={scope} disabled={busy} onValueChange={next => { setScope(next); setPage(0); }}> |
| 52 | <option value="site" disabled={!/^https?:/.test(tab.url)}>当前网站</option><option value="all">所有网站</option> |
| 53 | </InlineSelect> |
| 54 | <input ref={search} aria-label="搜索 Cookie" placeholder="搜索网站或 Cookie 名称" maxLength={200} |
| 55 | value={query} disabled={busy} onChange={event => { setQuery(event.target.value); setPage(0); }} /> |
| 56 | <button disabled={busy || loading || !data} onClick={() => setConfirmClear(!confirmClear)}> |
| 57 | <Trash2 size={14} />清空{scope === "site" ? "当前网站" : "所有网站"} |
| 58 | </button> |
| 59 | </div> |
| 60 | {confirmClear && <div className="cookie-confirm" role="alert"> |
| 61 | <span>清空{scope === "site" ? data?.hostname : "此" + (tab.incognito ? "无痕" : "普通") + "会话所有网站"}的 Cookie?搜索条件不限制清空范围,网站可能需要重新登录。</span> |
| 62 | <button disabled={busy} onClick={() => run("clear")}>确认清空</button> |
| 63 | <button disabled={busy} onClick={() => setConfirmClear(false)}>取消</button> |
| 64 | </div>} |
| 65 | {error && <p className="cookie-error" role="alert">{error}</p>} |
| 66 | <div className="cookie-list" aria-busy={busy || loading}> |
| 67 | {loading ? <p>正在读取 Cookie…</p> : data?.cookies.length === 0 ? <p>没有匹配的 Cookie。</p> : data?.cookies.map(cookie => |
| 68 | <article className="cookie-record" key={cookie.key}> |
| 69 | <div className="cookie-record-heading"><strong>{cookie.name || "(空名称)"}</strong> |
| 70 | <button disabled={busy} onClick={() => value?.key === cookie.key ? setValue(null) : run("details", cookie.key)}> |
| 71 | {value?.key === cookie.key ? "隐藏值" : "查看值"}</button> |
| 72 | <button disabled={busy || cookie.partitionKeyOpaque} aria-label={`删除 Cookie ${cookie.name}`} |
| 73 | title={cookie.partitionKeyOpaque ? "此分区 Cookie 需要清空所有网站" : "删除此 Cookie"} |
| 74 | onClick={() => run("delete", cookie.key)}><Trash2 size={14} /></button></div> |
| 75 | <div className="cookie-metadata">{cookie.domain} · 路径 {cookie.path} · {cookie.size} B</div> |
| 76 | <div className="cookie-metadata">{cookie.session ? "会话结束时过期" : `过期:${new Date(cookie.expires * 1000).toLocaleString()}`} |
| 77 | {cookie.secure && " · Secure"}{cookie.httpOnly && " · HttpOnly"}{cookie.sameSite && ` · SameSite=${cookie.sameSite}`} |
| 78 | {(cookie.partitionKey || cookie.partitionKeyOpaque) && ` · 分区:${cookie.partitionKey?.topLevelSite || "不透明"}`}</div> |
| 79 | {value?.key === cookie.key && <pre className="cookie-value">{value.value || "(空值)"}</pre>} |
| 80 | </article>)} |
| 81 | </div> |
| 82 | {data && <div className="cookie-pagination"><span>共 {data.total} 条 · 第 {data.page + 1} 页</span> |
| 83 | <button disabled={busy || loading || data.page === 0} onClick={() => setPage(data.page - 1)}>上一页</button> |
| 84 | <button disabled={busy || loading || (data.page + 1) * 50 >= data.total} onClick={() => setPage(data.page + 1)}>下一页</button> |
| 85 | </div>} |
| 86 | </section>; |
| 87 | } |