Puppeteer 中文文档v25.8.0

BrowserProvider.getDownloadUrl() 方法

获取所请求浏览器的下载 URL。

buildId 可以是精确版本(例如“131.0.6778.109”)或别名(例如“latest”、“stable”)。自定义提供程序如果支持别名,应在内部处理版本解析。

如果 buildId 无法解析为有效版本,则返回 null。URL 不会经过验证——如果该 URL 不存在,下载会在之后失败。

对于简单的 URL 构造可以是同步的;如果需要版本解析或网络请求,则可以是异步的。

签名

interface BrowserProvider {
  getDownloadUrl(options: DownloadOptions): Promise<URL | null> | URL | null;
}

参数

参数

类型

说明

options

DownloadOptions

下载选项(buildId 可以是别名或精确版本)

返回值:

Promise<URL | null> | URL | null

下载 URL;如果无法解析版本,则为 null

示例

// Synchronous example
getDownloadUrl(options) {
  const platform = mapPlatform(options.platform);
  return new URL(`https://releases.example.com/v${options.buildId}/${platform}.zip`);
}

// Asynchronous example with version mapping
async getDownloadUrl(options) {
  const electronVersion = await resolveElectronVersion(options.buildId);
  if (!electronVersion) return null;

  const platform = mapPlatform(options.platform);
  return new URL(`https://github.com/electron/electron/releases/download/v${electronVersion}/${platform}.zip`);
}