ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Puppeteer BrowserContext.deleteMatchingCookies(): 按过滤条件精确删除 Cookie 的用法与实现解析

Puppeteer BrowserContext.deleteMatchingCookies(): 按过滤条件精确删除 Cookie 的用法与实现解析 Puppeteer BrowserContext.deleteMatchingCookies(): 按过滤条件精确删除 Cookie 的用法与实现解析【免费下载链接】puppeteerJavaScript API for Chrome and Firefox项目地址: https://gitcode.com/GitHub_Trending/puppeteer1/puppeteerBrowserContext.deleteMatchingCookies()是 Puppeteer 中用于在指定浏览器上下文内批量清除 Cookie 的过滤式删除方法与需要传入完整 Cookie 对象的deleteCookie()不同它只接受一组轻量级的DeleteCookiesRequest过滤条件Cookie 名称加上可选的 URL、域名、路径、分区键即可删除当前上下文中所有匹配的条件组合对应的 Cookie。读完本文你将掌握该方法的签名、每个过滤字段的匹配语义、典型调用示例以及它在 Puppeteer 源码中的过滤与删除实现链路从而在自动化测试清理、登录态重置、Cookie 合规场景下安全、精确地管理浏览器存储。方法概览与签名deleteMatchingCookies()是 BrowserContext 类的方法官方文档页面为 puppeteer.browsercontext.deletematchingcookies.md。其完整签名为class BrowserContext { deleteMatchingCookies(...filters: DeleteCookiesRequest[]): Promisevoid; }参数filters类型是DeleteCookiesRequest[]变长参数表示一组过滤条件返回值Promisevoid删除操作完成后 resolve无返回数据。它的定位是条件删除当你只知道要删的 Cookie 的名字或加上域名/路径等定位信息而不是手里拿着一份从cookies()拿到的完整Cookie对象时就应当使用它。官方文档原文描述为 Deletes cookies matching the provided filters in this browser context删除本浏览器上下文中匹配所提供过滤条件的 Cookie。过滤条件类型 DeleteCookiesRequest每个filters参数都是 DeleteCookiesRequest 接口的一个实例其字段定义在 Cookie.ts 中字段类型是否必填含义namestring是要删除的 Cookie 名称是所有过滤条件的必填锚点urlstring否指定后删除该名称下 domain 与 path 都匹配所提供 URL 的所有 Cookie否则只删除与当前页面 domain 相关的 Cookiedomainstring否指定后只删除 domain 精确相等的 Cookiepathstring否指定后只删除 path 精确相等的 CookiepartitionKeyCookiePartitionKey|string否指定后只删除处于给定分区键下的 Cookie。在 Chrome 中partitionKey匹配分区 Cookie 所归属的顶级站点在 Firefox 中它匹配 WebDriver BiDi 定义的 PartitionKey 的 source origin关于partitionKey的对象形态它对应 CookiePartitionKey 接口包含两个 Chrome 特有的可选字段hasCrossSiteAncestor该 Cookie 是否存在相对 topLevelSite 跨站的祖先仅 Chrome 支持与sourceOrigin源站点 origin。传入字符串时则直接与该键的 source origin 文本比较。匹配逻辑源码级解析Puppeteer 对该方法的实现在 BrowserContext.ts理解这段代码可以准确回答多个字段之间是 AND 还是 OR这个关键问题async deleteMatchingCookies( ...filters: DeleteCookiesRequest[] ): Promisevoid { const cookies await this.cookies(); const cookiesToDelete cookies.filter(cookie { return filters.some(filter { if (filter.name cookie.name) { if (filter.domain ! undefined filter.domain cookie.domain) { return true; } if (filter.path ! undefined filter.path cookie.path) { return true; } // partitionKey 与 url 的匹配逻辑略见源码 return true; } return false; }); }); await this.deleteCookie(...cookiesToDelete); }从源码结构看匹配规则可以归纳为顶层是 ORfilters.some多个过滤条件之间是任一命中即删。传入[{name: a}, {name: b}]会同时删除名为a和b的所有 Cookie。name是前提AND单个过滤对象内filter.name cookie.name必须先成立domain、path、partitionKey、url才有参与比较的机会名称不匹配的 Cookie 一定不会被该过滤对象命中。可选字段的命中是宽松式只要name相等并且任何一个已提供的可选字段domain/path/partitionKey/url与 Cookie 对应属性相等即返回true如果过滤对象只提供了name则该名称的所有 Cookie 全部命中代码末尾的return true保证这一点。url的匹配方式将url解析为URL对象后要求url.hostname cookie.domain url.pathname cookie.path同时成立。注意这是 hostname 与 path 的同时精确相等而不是前缀匹配。partitionKey的类型归一化Cookie 侧的partitionKey必须是对象字符串会抛出Unexpected string partition key错误过滤侧则既允许对象比较sourceOrigin也允许字符串与 Cookie 的partitionKey.sourceOrigin比较。删除阶段并非直接调用协议层的删除命令而是复用deleteCookie()async deleteCookie(...cookies: Cookie[]): Promisevoid { return await this.setCookie( ...cookies.map(cookie { return { ...cookie, expires: 1, }; }), ); }BrowserContext.ts——它把待删除的 Cookie 的expires改写为过去时间戳1 秒即 1970-01-01 00:00:01再通过setCookie()写回浏览器让浏览器自然将其过期清除。也就是说deleteMatchingCookies()的完整链路是cookies()枚举 → 过滤匹配 →deleteCookie()改写过期时间 →setCookie()提交。使用示例以下示例展示了基于真实测试用例browsercontext-cookies.test.ts的可运行用法覆盖按名称 域名/路径/URL/分区键删除的各类场景import puppeteer from puppeteer; const browser await puppeteer.launch(); // 使用隔离上下文避免污染默认浏览器上下文 const context await browser.createBrowserContext(); // 准备两个待删除的 Cookie同域、同路径仅名称不同 await context.setCookie( { name: cookie1, value: secret, domain: example.test, path: /test, secure: true, }, { name: cookie2, value: secret, domain: example.test, path: /test, secure: true, }, ); // 场景 1仅按名称删除 —— cookie1 全部消失 await context.deleteMatchingCookies({name: cookie1}); // 场景 2名称 域名精确匹配 await context.deleteMatchingCookies({name: cookie1, domain: example.test}); // 场景 3名称 URLhostname 与 pathname 同时精确匹配 await context.deleteMatchingCookies({ name: cookie1, url: https://example.test/test, }); // 场景 4名称 路径精确匹配 await context.deleteMatchingCookies({name: cookie1, path: /test}); // 场景 5名称 分区键字符串形式匹配 sourceOrigin await context.deleteMatchingCookies({ name: cookie1, partitionKey: https://example.test, }); await context.close();对应的官方测试位于 test/src/browsercontext-cookies.test.ts测试构造了cookie1与cookie2两个同域同路径的 Cookie依次使用上述五种过滤条件调用context.deleteMatchingCookies(filter)每次断言剩余 Cookie 恰好只剩cookie2从而验证了每种过滤维度都能精确命中且不误伤其他 Cookie。与 Browser / Page 上同名或相关方法的关系Browser.deleteMatchingCookies()是该方法在默认浏览器上下文上的快捷方式其实现Browser.ts就是直接转发return await this.defaultBrowserContext().deleteMatchingCookies(...filters)。如果你没有使用隔离上下文直接在browser上调用即可效果完全等价。BrowserContext.deleteCookie()接收完整Cookie对象是底层精确删除单个对象的能力deleteMatchingCookies()本质上是在其之上的过滤包装。页面级的 Page.deleteCookie() 同样接受DeleteCookiesRequest[]区别在于它作用域限定在当前页面未提供url时会以页面当前 URL 作为默认作用域且 CDP 实现中会额外删除页面所属分区下的同名 Cookie见 cdp/Page.ts 中对Network.deleteCookies的两次调用。跨页面、跨标签的批量清理应优先使用BrowserContext.deleteMatchingCookies()。注意事项与实践建议name必填且是精确匹配源码中为filter.name cookie.name的严格字符串比较不存在通配符或前缀匹配需要批量删除一组不同名称的 Cookie 时请传入多个过滤对象利用 OR 语义。删除范围是整个上下文该方法枚举的是this.cookies()即整个 BrowserContext 的 Cookie不受当前页面限制如果你只想清理当前页面的 Cookie应改用page.deleteCookie()。可选字段是精确相等比较domain、path、url的 hostname/pathname 均为字符串精确比较。例如 Cookie 的 domain 是example.test时domain: .example.test不会命中。分区 Cookie 的特殊性带partitionKey的 CookieChrome 的分区 Cookie / CHIPS 场景在过滤侧可用对象或字符串匹配 source origin从源码看Cookie 侧的partitionKey若被归一化为字符串会触发运行时错误这在跨浏览器运行时属于实现上的已知约束测试中 Firefox 侧甚至无法可靠取得分区键见测试文件注释。删除机制依赖过期时间改写由于底层通过setCookie()将expires置为 1 来实现删除删除是异步且依赖浏览器接受 Cookie 写入的在需要确认删除结果的脚本中建议删除后调用context.cookies()复查这正是官方测试的断言方式。隔离上下文是最佳实践官方测试通过setupSeparateTestBrowserHooks创建独立浏览器环境运行本方法在生产自动化中用browser.createBrowserContext()隔离后再做 Cookie 清理可避免误删其他业务会话的 Cookie。小结BrowserContext.deleteMatchingCookies()提供了以name为锚、以url/domain/path/partitionKey为可选精化维度的条件化 Cookie 清理能力多个过滤对象之间是 OR 关系单个对象内部先按名称精确匹配再按任一提供的可选字段宽松命中实现层面它由cookies()枚举、匹配过滤、deleteCookie()过期时间改写三步组成。理解这一语义后你可以在测试环境清理、会话重置、隐私合规检查等场景中用最小的 API 表面完成精确、可控的 Cookie 删除并通过 docs/api/puppeteer.deletecookiesrequest.md 与 test/src/browsercontext-cookies.test.ts 进一步核对字段行为与回归用例。【免费下载链接】puppeteerJavaScript API for Chrome and Firefox项目地址: https://gitcode.com/GitHub_Trending/puppeteer1/puppeteer创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表