puppeteer/assets/js/e18070e6.d5e1c27f.js

2 lines
26 KiB
JavaScript
Raw Normal View History

/*! For license information please see e18070e6.d5e1c27f.js.LICENSE.txt */
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[87486],{18025:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>s,default:()=>u,frontMatter:()=>i,metadata:()=>a,toc:()=>d});var r=t(85893),o=t(11151);const i={},s="Request Interception",a={id:"guides/network-interception",title:"Request Interception",description:"Once request interception is enabled, every request will stall unless it's",source:"@site/../docs/guides/network-interception.md",sourceDirName:"guides",slug:"/guides/network-interception",permalink:"/next/guides/network-interception",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{},sidebar:"docs",previous:{title:"Debugging",permalink:"/next/guides/debugging"},next:{title:"Headless mode",permalink:"/next/guides/headless-modes"}},c={},d=[{value:"Multiple Intercept Handlers and Asynchronous Resolutions",id:"multiple-intercept-handlers-and-asynchronous-resolutions",level:2},{value:"Cooperative Intercept Mode",id:"cooperative-intercept-mode",level:2},{value:"Cooperative Request Continuation",id:"cooperative-request-continuation",level:2},{value:"Upgrading to Cooperative Intercept Mode for package maintainers",id:"upgrading-to-cooperative-intercept-mode-for-package-maintainers",level:2}];function l(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,o.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(n.h1,{id:"request-interception",children:"Request Interception"}),"\n",(0,r.jsx)(n.p,{children:"Once request interception is enabled, every request will stall unless it's\ncontinued, responded or aborted."}),"\n",(0,r.jsx)(n.p,{children:"An example of a na\xefve request interceptor that aborts all image requests:"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-ts",children:"import puppeteer from 'puppeteer';\n\n(async () => {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.setRequestInterception(true);\n page.on('request', interceptedRequest => {\n if (interceptedRequest.isInterceptResolutionHandled()) return;\n if (\n interceptedRequest.url().endsWith('.png') ||\n interceptedRequest.url().endsWith('.jpg')\n )\n interceptedRequest.abort();\n else interceptedRequest.continue();\n });\n await page.goto('https://example.com');\n await browser.close();\n})();\n"})}),"\n",(0,r.jsx)(n.h2,{id:"multiple-intercept-handlers-and-asynchronous-resolutions",children:"Multiple Intercept Handlers and Asynchronous Resolutions"}),"\n",(0,r.jsxs)(n.p,{children:["By default Puppeteer will raise a ",(0,r.jsx)(n.code,{children:"Request is already handled!"})," exception if\n",(0,r.jsx)(n.code,{children:"request.abort"}),", ",(0,r.jsx)(n.code,{children:"request.continue"}),", or ",(0,r.jsx)(n.code,{children:"request.respond"})," are called after any\nof them have already been called."]}),"\n",(0,r.jsxs)(n.p,{children:["Always assume that an unknown handler may have already called\n",(0,r.jsx)(n.code,{children:"abort/continue/respond"}),". Even if your handler is the only one you registered,\n3rd party packages may register their own handlers. It is therefore important to\nalways check the resolution status using\n",(0,r.jsx)(n.a,{href:"../api/puppeteer.httprequest.isinterceptresolutionhandled",children:"request.isInterceptResolutionHandled"}),"\nbefore calling ",(0,r.jsx)(n.code,{children:"abort/continue/respond"}),"."]}),"\n",(0,r.jsxs)(n.p,{children:["Importantly, the intercept resolution may get handled by another listener while\nyour handler is awaiting an asynchronous operation. Therefore, the return value\nof ",(0,r.jsx)(n.code,{children:"request.isInterceptResolutionHandled"})," is only safe in a synchronous code\nblock. Always execute ",(0,r.jsx)(n.code,{children:"request.isInterceptResolutionHandled"})," and\n",(0,r.jsx)(n.code,{children:"abort/continue/respond"})," ",(0,r.jsx)(n.strong,{children:"synchronously"})," together."]}),"\n",(0,r.jsx)(n.p,{children:"This example demonstrates two synchronous handlers w