/wakita-toolkit:playwright-cookie-injection-pitfallsfrom wakita-toolkit
Playwright/Chromium cookie 注入不持久化、session cookie 丢失、 persistent_context 读不到 cookie 的排障;也覆盖双 profile 目录、461/CAPTCHA 双语义、 进程组信号、networkidle 不收敛。
Ships in wakita-toolkit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
适用边界:Playwright / Patchright + Chromium。Selenium、Puppeteer 的 cookie/profile 机制不同; Firefox/WebKit 的持久化行为也不同,不直接套用。
browser_context.add_cookies([{name, value, domain, path}]) 不设 expires 字段时,
Chromium 默认把它当作 session cookie(等价 expires=-1)。
你注入完成立刻 ctx.cookies() 能看到,但一旦调 context.close() / cleanup() 关闭
context(或浏览器进程退出),这些 session cookie 不会被写回 user_data_dir 的
Default/Cookies SQLite 文件,下次启动 context 它们就消失了。
Chromium 把持久化 cookie 写到 Default/Cookies SQLite 的 cookies 表;
session cookie 只在内存中。这是 Chromium 而非 Playwright 的设计,
Playwright 文档
里未直接强调,踩坑成本极高。
隔离变量实验:用假 cookie 字符串验证"expires 是持久化的唯一决定因素"。 注意:假 cookie 只用于机制验证,生产路径必须用真实 cookie 内容, 否则浏览器可能被平台反爬识别为伪造态导致页面崩溃。
# 不设 expires → 不持久化
await ctx.add_cookies([{'name':'test_session','value':'abc','domain':'.example.com','path':'/'}])
await ctx.close()
cookies = await ctx.cookies() # 返回 [] ← 注入消失
# 设 expires=未来 timestamp(秒级)→ 持久化
import time
await ctx.add_cookies([{'name':'test_session','value':'abc','domain':'.example.com','path':'/',
'expires': int(time.time()) + 30*86400}])
await ctx.close()
cookies = await ctx.cookies() # 返回 [{'name':'test_session','value':'abc', ...}] ✓
parse_cookie_header_to_dicts 这类把 cookie 字符串转 Playwright cookie 字典的工具函数,
默认要给每条 cookie 设 expires=now+N天:
def parse_cookie_header_to_dicts(cookie_header: str, domain: str, expires_in_days: int = 30) -> list[dict]:
import time
future_ts = int(time.time()) + expires_in_days * 86400
result = []
for pair in cookie_header.split(";"):
pair = pair.strip()
if not pair or "=" not in pair:
continue
name, _, value = pair.partition("=")
result.append({
"name": name.strip(),
"value": value.strip(),
"domain": domain,
"path": "/",
"expires": future_ts, # ← 必须给正向未来 timestamp
})
return result
⚠️ 注意:若真实 cookie 本来就有 expires(比如从浏览器导出的),优先用真实值,
别套固定 N 天。从 Playwright ctx.cookies() 或 dump 文件读出来的 cookie 字典
已经带正确 expires,直接传给 add_cookies 即可,不要重新解析丢失字段。
page.goto + 验证,不打算持久化的场景可不设 expiresSingletonLock 会让两个进程用同一 user_data_dir 启动 chromium 时
第二个进程启动失败。所以注入要在独占 user_data_dir 的窗口内进行。登录/体检浏览器与爬虫子进程可能用不同命名的 user_data_dir(如平台全名 vs 短名)。 判断「登录了但爬虫不认」时:
Default/Cookies 的写入时间,确认哪个角色在写rmtree 目录,会删掉在役登录态同一状态码可能同时覆盖「验证码挑战」和「登录态过期」,客户端日志的标签不可全信。
先看实际页面:显示登录页 = 登录过期(重新登录即可);显示滑块/验证码 = 才需要人工过验证。
pong 接口返回已登录,也不代表搜索/详情接口认同一会话。
Playwright/patchright 的浏览器可能以 detached 方式自立进程组,而 node driver 留在爬虫组内。
对整组发 SIGTERM,打中的是 driver,driver 收到即关闭浏览器,导致「退出前落盘」静默失败。
正确做法:优雅阶段只对 Python 进程 SIGTERM,让它自己收尾;随后再对整组补 SIGKILL 清场。
SPA + 长连接/心跳站点的 waitUntil: 'networkidle' 会超时。改用
domcontentloaded + 明确的元素/文本 waitForFunction 或固定等待。
SSE 端点用 curl 验证会挂住,必须加 --max-time。
one-way-sync-antipatternroot-cause-no-patch:
不要为了让 cookie 校验过就在校验函数里加兜底补丁,正解是补齐注入路径operate-through-channels:优先走 HTTP API 走完整业务路径chinese-commit-messagesWhat SKILL.md sets; muted values are Claude Code's defaults
add_cookies、cookie 注入、persistent_context、expires -1、session cookie 丢失、 cookie 不持久化、user_data_dir Cookies、Chromium SingletonLock、cookie 注入后消失、 461、验证码、profile 目录、networkidle 不收敛;cookie 注入后关闭 context 再打开就丢失。
Other plugins' skills for the same job
npx claudepluginhub aaaakita/wakita-plugins --plugin wakita-toolkit39 skills in this plugin
/ai-code-failure-modesAI 生成代码特有的失败模式审查——查 stub/假数据冒充核心逻辑、只覆盖快乐路径、调用不存在的 API 或依赖包、 配置变更没验证、测试只断言当前实现、静默兜底掩盖失败。当用户说“AI 写的代码看着能跑但心里没底” “是不是 stub 冒充”“测试全过但功能没做”“AI 加了个不存在的包”“配置改了没生效”时使用。 也作为 clean-home 清理房间的第 4 步被加载。只审 AI 特有失败模式,不重复测试规范与静默失败审计。
/brainstorm创作性头脑风暴——在动手写代码前,和 AI 一起发散思考、对比方案、画架构图。 不是工作流程,是一种思考姿态。不写业务代码,只做设计讨论和方案探索。 触发词:头脑风暴、帮我想想、设计一下、分析方案、对比方案、架构怎么设计、 技术选型、brainstorm、design discussion、想清楚再动手、先别写代码、 先想想、方案对比、tradeoff、利弊分析。
/canvas-interaction-spaceCanvas/WebGL 与 DOM 叠加层错位、指针吸附/命中手感差、缩放后交互容差变化时的排查与设计规范。 核心:屏幕空间与模型空间是两套坐标系。先验证 devicePixelRatio 与 canvas CSS 尺寸,再怀疑锚点; 交互容差按屏幕像素定义再换算成模型单位;坐标转换只保留一个来源。 触发词:Canvas 错位、CSS2D 标签飘移、叠加层偏移、WebGL 坐标、devicePixelRatio、dpr、 setSize、updateStyle、吸附不灵、命中半径、点击选不中、缩放后手感、屏幕像素、模型单位、 screen space、model space、overlay alignment。