Skip to main content
Version: 24.38.0

Page.setRequestInterception() 方法

🌐 Page.setRequestInterception() method

启用请求拦截后,可以使用 HTTPRequest.abort()HTTPRequest.continue()HTTPRequest.respond() 方法。这提供了修改页面发出的网络请求的能力。

🌐 Activating request interception enables HTTPRequest.abort(), HTTPRequest.continue() and HTTPRequest.respond() methods. This provides the capability to modify network requests that are made by a page.

一旦启用请求拦截,除非请求被继续、响应或中止,否则每个请求都会暂停;或者使用浏览器缓存完成。

🌐 Once request interception is enabled, every request will stall unless it's continued, responded or aborted; or completed using the browser cache.

有关详细信息,请参见 请求拦截指南

🌐 See the Request interception guide for more details.

语法

🌐 Signature

class Page {
abstract setRequestInterception(value: boolean): Promise<void>;
}

参数

🌐 Parameters

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

value

boolean

是否启用请求拦截。

🌐 Whether to enable request interception.

返回:

Promise<void>

示例

🌐 Example

中止所有图片请求的简单请求拦截器的示例:

🌐 An example of a naïve request interceptor that aborts all image requests:

import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
if (
interceptedRequest.url().endsWith('.png') ||
interceptedRequest.url().endsWith('.jpg')
)
interceptedRequest.abort();
else interceptedRequest.continue();
});
await page.goto('https://example.com');
await browser.close();