浏览器提供者接口
🌐 BrowserProvider interface
自定义浏览器提供器实现的接口。允许用户为浏览器实现替代的下载来源。
🌐 Interface for custom browser provider implementations. Allows users to implement alternative download sources for browsers.
⚠️ 重要:Puppeteer 官方不支持自定义提供器。
通过实现此接口,你将承担以下全部责任:
🌐 By implementing this interface, you accept full responsibility for:
- 确保下载的二进制文件与 Puppeteer 的期望兼容 - 测试浏览器启动和其他功能是否与你的二进制文件一起工作 - 当 Puppeteer 或你的下载源更改时保持兼容性 - 如果混合来源,在各平台之间保持版本一致性
Puppeteer 仅测试并保证 Chrome for Testing 二进制文件。
🌐 Puppeteer only tests and guarantees Chrome for Testing binaries.
语法
🌐 Signature
export interface BrowserProvider
示例
🌐 Example
class ElectronDownloader implements BrowserProvider {
supports(options: DownloadOptions): boolean {
return options.browser === Browser.CHROMEDRIVER;
}
getDownloadUrl(options: DownloadOptions): URL {
const platform = mapToPlatform(options.platform);
return new URL(
`v${options.buildId}/chromedriver-v${options.buildId}-${platform}.zip`,
'https://github.com/electron/electron/releases/download/',
);
}
getExecutablePath(options): string {
const ext = options.platform.includes('win') ? '.exe' : '';
return `chromedriver/chromedriver${ext}`;
}
}
方法
🌐 Methods
方法 🌐 Method | 描述 🌐 Description |
|---|---|
| getDownloadUrl(options) | 获取所请求浏览器的下载网址。 🌐 Get the download URL for the requested browser. buildId 可以是确切的版本(例如,“131.0.6778.109”)或别名(例如,“latest”,“stable”)。如果自定义提供器支持别名,则应在内部处理版本解析。 🌐 The buildId can be either an exact version (e.g., "131.0.6778.109") or an alias (e.g., "latest", "stable"). Custom providers should handle version resolution internally if they support aliases. 如果 buildId 无法解析为有效版本,则返回 null。URL 未经过验证——如果 URL 不存在,下载将会在之后失败。 🌐 Returns null if the buildId cannot be resolved to a valid version. The URL is not validated - download will fail later if URL doesn't exist. 对于简单的 URL 构建可以是同步的,如果需要版本解析/网络请求则可以是异步的。 🌐 Can be synchronous for simple URL construction or asynchronous if version resolution/network requests are needed. |
| getExecutablePath(options) | 获取提取的归档文件中可执行文件的相对路径。 🌐 Get the relative path to the executable within the extracted archive. |
| getName() | 获取此提供者的名称。用于错误消息和日志记录目的。 🌐 Get the name of this provider. Used for error messages and logging purposes. 附注 此方法用于替代 🌐 This method is used instead of |
| supports(options) | 检查此提供商是否支持指定的浏览器/平台。用于在尝试下载前进行筛选。 🌐 Check if this provider supports the given browser/platform. Used for filtering before attempting downloads. 可以是同步的以进行快速检查,或者如果需要版本解析/网络请求,则可以是异步的。 🌐 Can be synchronous for quick checks or asynchronous if version resolution/network requests are needed. |