@skrillex1224/playwright-toolkit 2.1.160 → 2.1.162

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,12 +31,16 @@ const ApifyKit = await KitHook.useApifyKit();
31
31
  // LiveView
32
32
  const { startLiveViewServer, takeLiveScreenshot } = LiveView.useLiveView();
33
33
 
34
+ const crawlerOptions = Launch.getPlaywrightCrawlerOptions({
35
+ runInHeadfulMode: false,
36
+ isRunningOnApify: Actor.isAtHome(),
37
+ launcher: chromium,
38
+ });
39
+
34
40
  const crawler = new PlaywrightCrawler({
35
- launchContext: {
36
- launcher: chromium,
37
- launchOptions: Launch.getAdvancedLaunchOptions(),
38
- },
41
+ ...crawlerOptions,
39
42
  preNavigationHooks: [
43
+ ...crawlerOptions.preNavigationHooks,
40
44
  async ({ page }) => {
41
45
  // 验证码监控
42
46
  Captcha.useCaptchaMonitor(page, {
@@ -84,9 +88,7 @@ await Actor.exit();
84
88
 
85
89
  | 模块 | 方法 | 说明 |
86
90
  | ----------- | ------------------------------------------------ | ------------------------------------------------------------------------------------ |
87
- | `Launch` | `getAdvancedLaunchOptions()` | 增强版启动参数 |
88
- | `Launch` | `getLaunchOptions({ proxyConfiguration, log? })` | 基础启动参数(返回 `onPageCreated(page) => { waitUntil }`,默认 `domcontentloaded`) |
89
- | `Launch` | `getFingerprintGeneratorOptions()` | 指纹生成器选项 |
91
+ | `Launch` | `getPlaywrightCrawlerOptions(options)` | 一次性返回 PlaywrightCrawler 所需公共配置(超时/指纹/代理/导航 hook) |
90
92
  | `AntiCheat` | `applyPage(page, options?)` | 应用时区/语言/权限/视口 |
91
93
  | `AntiCheat` | `applyContext(context, options?)` | 仅应用 Context 设置 |
92
94
  | `AntiCheat` | `syncViewportWithScreen(page)` | 同步视口与屏幕 |
package/dist/index.cjs CHANGED
@@ -423,6 +423,8 @@ function createInternalLogger(moduleName, explicitLogger) {
423
423
  // src/internals/screenshot.js
424
424
  var logger = createInternalLogger("Screenshot");
425
425
  var DEFAULT_TIMEOUT_MS = 5e3;
426
+ var FORCED_FULLPAGE_TYPE = "jpeg";
427
+ var FORCED_FULLPAGE_QUALITY = 50;
426
428
  var SUPPORTED_TYPES = /* @__PURE__ */ new Set(["png", "jpeg", "webp"]);
427
429
  var toPositiveNumber = (value, fallback = 0) => {
428
430
  const n = Number(value);
@@ -458,9 +460,9 @@ var buildFullPageClip = (metrics, viewport, maxClipHeight) => {
458
460
  };
459
461
  };
460
462
  var capturePageScreenshot = async (page, options = {}) => {
461
- const type = normalizeType(options.type);
462
- const quality = normalizeQuality(options.quality, type);
463
463
  const fullPage = Boolean(options.fullPage);
464
+ const type = fullPage ? FORCED_FULLPAGE_TYPE : normalizeType(options.type);
465
+ const quality = fullPage ? FORCED_FULLPAGE_QUALITY : normalizeQuality(options.quality, type);
464
466
  const timeout = toPositiveNumber(options.timeout, DEFAULT_TIMEOUT_MS);
465
467
  const maxClipHeight = Math.round(toPositiveNumber(options.maxClipHeight, 0));
466
468
  const fallbackOptions = {
@@ -1617,6 +1619,12 @@ var createTrafficMeter = ({
1617
1619
 
1618
1620
  // src/launch.js
1619
1621
  var logger7 = createInternalLogger("Launch");
1622
+ var DEFAULT_CRAWLER_BASE_OPTIONS = Object.freeze({
1623
+ maxConcurrency: 1,
1624
+ maxRequestRetries: 0,
1625
+ requestHandlerTimeoutSecs: 240,
1626
+ navigationTimeoutSecs: 120
1627
+ });
1620
1628
  var resolveProxyLaunchOptions = (proxyConfiguration = {}) => {
1621
1629
  const config = proxyConfiguration && typeof proxyConfiguration === "object" && !Array.isArray(proxyConfiguration) ? proxyConfiguration : {};
1622
1630
  const proxyUrl = String(config.proxy_url || "").trim();
@@ -1642,12 +1650,17 @@ var resolveProxyLaunchOptions = (proxyConfiguration = {}) => {
1642
1650
  return { launchProxy, byPassDomains, enableProxy, proxyUrl };
1643
1651
  };
1644
1652
  var Launch = {
1645
- getLaunchOptions(options = {}) {
1653
+ getPlaywrightCrawlerOptions(options = {}) {
1646
1654
  const normalizedOptions = Array.isArray(options) ? { customArgs: options } : options || {};
1647
1655
  const {
1648
1656
  customArgs = [],
1649
1657
  proxyConfiguration = {},
1650
- log: logOptions = null
1658
+ log: logOptions = null,
1659
+ runInHeadfulMode = false,
1660
+ isRunningOnApify = false,
1661
+ launcher = null,
1662
+ preNavigationHooks = [],
1663
+ postNavigationHooks = []
1651
1664
  } = normalizedOptions;
1652
1665
  const { launchProxy, byPassDomains, enableProxy, proxyUrl } = resolveProxyLaunchOptions(proxyConfiguration);
1653
1666
  const byPassRules = buildByPassDomainRules(byPassDomains);
@@ -1699,14 +1712,37 @@ var Launch = {
1699
1712
  page.on("request", requestHandler);
1700
1713
  return recommendedGotoOptions;
1701
1714
  };
1702
- return { launchOptions, onPageCreated };
1703
- },
1704
- /**
1705
- * 推荐的 Fingerprint Generator 选项
1706
- * 确保生成的是桌面端、较新的 Chrome,以匹配我们的脚本逻辑
1707
- */
1708
- getFingerprintGeneratorOptions() {
1709
- return AntiCheat.getFingerprintGeneratorOptions();
1715
+ const launchContext = {
1716
+ useIncognitoPages: true,
1717
+ launchOptions
1718
+ };
1719
+ if (launcher) {
1720
+ launchContext.launcher = launcher;
1721
+ }
1722
+ const crawlerBaseOptions = {
1723
+ ...DEFAULT_CRAWLER_BASE_OPTIONS,
1724
+ headless: !runInHeadfulMode || isRunningOnApify,
1725
+ browserPoolOptions: {
1726
+ useFingerprints: true,
1727
+ fingerprintOptions: {
1728
+ fingerprintGeneratorOptions: AntiCheat.getFingerprintGeneratorOptions()
1729
+ }
1730
+ },
1731
+ launchContext
1732
+ };
1733
+ const normalizedPreNavigationHooks = Array.isArray(preNavigationHooks) ? preNavigationHooks : [];
1734
+ const normalizedPostNavigationHooks = Array.isArray(postNavigationHooks) ? postNavigationHooks : [];
1735
+ const internalPreNavigationHook = async ({ page }, gotoOptions = {}) => {
1736
+ const recommendedGotoOptions = onPageCreated(page);
1737
+ if (recommendedGotoOptions && typeof recommendedGotoOptions === "object") {
1738
+ Object.assign(gotoOptions, recommendedGotoOptions);
1739
+ }
1740
+ };
1741
+ return {
1742
+ ...crawlerBaseOptions,
1743
+ preNavigationHooks: [internalPreNavigationHook, ...normalizedPreNavigationHooks],
1744
+ ...normalizedPostNavigationHooks.length > 0 ? { postNavigationHooks: normalizedPostNavigationHooks } : {}
1745
+ };
1710
1746
  }
1711
1747
  };
1712
1748