| 1 | import { createHash } from "node:crypto"; |
| 2 | |
| 3 | const key = cookie => createHash("sha256").update(JSON.stringify([ |
| 4 | cookie.name, cookie.domain, cookie.path, cookie.partitionKey || null, Boolean(cookie.partitionKeyOpaque), |
| 5 | ])).digest("hex"); |
| 6 | const forHost = (cookie, hostname) => cookie.domain.startsWith(".") |
| 7 | ? hostname === cookie.domain.slice(1) || hostname.endsWith(cookie.domain) |
| 8 | : hostname === cookie.domain; |
| 9 | |
| 10 | export async function browserCookies(cdp, session, tab, input) { |
| 11 | if (!tab || tab.login) throw new Error("当前标签不可管理 Cookie"); |
| 12 | if (input.url !== undefined && input.url !== tab.url) throw new Error("网页已切换,请重新打开 Cookie 管理"); |
| 13 | const { mode = "list", scope = "site", query = "", page = 0 } = input; |
| 14 | if (!["list", "details", "delete", "clear"].includes(mode) || !["site", "all"].includes(scope) || |
| 15 | typeof query !== "string" || query.length > 200 || !Number.isInteger(page) || page < 0) |
| 16 | throw new Error("Cookie 操作无效"); |
| 17 | let hostname = ""; |
| 18 | if (scope === "site") { |
| 19 | const url = new URL(tab.url); |
| 20 | if (!["http:", "https:"].includes(url.protocol)) throw new Error("当前页面没有网站 Cookie,请选择所有网站"); |
| 21 | hostname = url.hostname; |
| 22 | } |
| 23 | const context = tab.browserContextId ? { browserContextId: tab.browserContextId } : {}; |
| 24 | const read = async () => { |
| 25 | const { cookies } = await cdp("Storage.getCookies", context); |
| 26 | return cookies.filter(cookie => !hostname || forHost(cookie, hostname)); |
| 27 | }; |
| 28 | let cookies = await read(); |
| 29 | const remove = async cookie => { |
| 30 | |
| 31 | if (cookie.partitionKeyOpaque) throw new Error("此 Cookie 的分区不可单独定位,请使用清空所有网站 Cookie"); |
| 32 | await cdp("Network.deleteCookies", { |
| 33 | name: cookie.name, domain: cookie.domain, path: cookie.path, |
| 34 | ...(cookie.partitionKey ? { partitionKey: cookie.partitionKey } : {}), |
| 35 | }, await session(tab.targetId)); |
| 36 | }; |
| 37 | if (mode === "delete" || mode === "details") { |
| 38 | const cookie = cookies.find(cookie => key(cookie) === input.cookieKey); |
| 39 | if (!cookie) throw new Error("Cookie 已变化,请刷新列表"); |
| 40 | if (mode === "details") return { value: cookie.value }; |
| 41 | await remove(cookie); |
| 42 | cookies = await read(); |
| 43 | } else if (mode === "clear") { |
| 44 | if (scope === "all") await cdp("Storage.clearCookies", context); |
| 45 | else { |
| 46 | if (cookies.some(cookie => cookie.partitionKeyOpaque)) throw new Error("此网站含不可单独定位的分区 Cookie,请使用清空所有网站 Cookie"); |
| 47 | for (const cookie of cookies) await remove(cookie); |
| 48 | } |
| 49 | cookies = await read(); |
| 50 | } |
| 51 | const search = query.toLowerCase(); |
| 52 | cookies = cookies.filter(cookie => `${cookie.domain}\n${cookie.name}`.toLowerCase().includes(search)) |
| 53 | .sort((a, b) => a.domain.localeCompare(b.domain) || a.name.localeCompare(b.name) || a.path.localeCompare(b.path)); |
| 54 | const current = Math.min(page, Math.max(0, Math.ceil(cookies.length / 50) - 1)); |
| 55 | return { total: cookies.length, page: current, hostname, incognito: Boolean(tab.incognito), |
| 56 | cookies: cookies.slice(current * 50, (current + 1) * 50).map(({ value, ...cookie }) => ({ |
| 57 | key: key(cookie), name: cookie.name, domain: cookie.domain, path: cookie.path, |
| 58 | expires: cookie.expires, session: cookie.session, secure: cookie.secure, httpOnly: cookie.httpOnly, |
| 59 | sameSite: cookie.sameSite || "", partitionKey: cookie.partitionKey, |
| 60 | partitionKeyOpaque: Boolean(cookie.partitionKeyOpaque), size: cookie.size, |
| 61 | })), |
| 62 | }; |
| 63 | } |