@skrillex1224/playwright-toolkit 2.1.151 → 2.1.152

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
@@ -209,6 +209,12 @@ await page.context().addCookies(cookies);
209
209
 
210
210
  // 全页面滚动截图 (自动检测所有滚动元素,强制展开后截图)
211
211
  const base64Image = await Share.captureScreen(page);
212
+
213
+ // 移动端宽度截图(moblie 拼写保持兼容)
214
+ const mobileImage = await Share.captureScreen(page, { moblie: true });
215
+
216
+ // 仅恢复宽度(restore 支持 true / false / 'width-only' / 'height-only')
217
+ const image2 = await Share.captureScreen(page, { restore: 'width-only' });
212
218
  // 返回 base64 编码的 PNG 图片
213
219
  ```
214
220
 
package/dist/index.cjs CHANGED
@@ -3059,6 +3059,8 @@ var logger11 = createInternalLogger("Share");
3059
3059
  var DEFAULT_TIMEOUT_MS = 50 * 1e3;
3060
3060
  var DEFAULT_PAYLOAD_SNAPSHOT_MAX_LEN = 500;
3061
3061
  var DEFAULT_POLL_INTERVAL_MS = 120;
3062
+ var DEFAULT_MOBILE_SCREENSHOT_WIDTH = 414;
3063
+ var MOBILE_VIEWPORT_MAX_WIDTH = 480;
3062
3064
  var normalizePrefix = (value) => value == null ? void 0 : String(value).trim();
3063
3065
  var toSnapshot = (value, maxLen) => {
3064
3066
  const text = String(value || "");
@@ -3447,16 +3449,25 @@ var Share = {
3447
3449
  * @param {import('playwright').Page} page
3448
3450
  * @param {Object} [options]
3449
3451
  * @param {number} [options.buffer]
3450
- * @param {boolean} [options.restore]
3452
+ * @param {boolean | 'width-only' | 'height-only'} [options.restore]
3451
3453
  * @param {number} [options.maxHeight]
3454
+ * @param {boolean} [options.moblie]
3452
3455
  * @returns {Promise<string>} base64 png
3453
3456
  */
3454
3457
  async captureScreen(page, options = {}) {
3455
3458
  const originalViewport = page.viewportSize();
3456
3459
  const defaultBuffer = Math.round((originalViewport?.height || 1080) / 2);
3457
3460
  const buffer = options.buffer ?? defaultBuffer;
3458
- const restore = options.restore ?? false;
3461
+ const useMobileViewport = Boolean(options.moblie ?? options.mobile ?? false);
3462
+ const restoreMode = (() => {
3463
+ if (options.restore === "width-only" || options.restore === "height-only") return options.restore;
3464
+ if (typeof options.restore === "boolean") return options.restore ? "all" : "none";
3465
+ return useMobileViewport ? "width-only" : "none";
3466
+ })();
3459
3467
  const maxHeight = options.maxHeight ?? 8e3;
3468
+ const currentWidth = originalViewport?.width || 1280;
3469
+ const isCurrentMobileWidth = currentWidth <= MOBILE_VIEWPORT_MAX_WIDTH;
3470
+ const targetWidth = useMobileViewport ? isCurrentMobileWidth ? currentWidth : DEFAULT_MOBILE_SCREENSHOT_WIDTH : currentWidth;
3460
3471
  try {
3461
3472
  const maxScrollHeight = await page.evaluate(() => {
3462
3473
  let maxHeight2 = document.body.scrollHeight;
@@ -3480,7 +3491,7 @@ var Share = {
3480
3491
  });
3481
3492
  const targetHeight = Math.min(maxScrollHeight + buffer, maxHeight);
3482
3493
  await page.setViewportSize({
3483
- width: originalViewport?.width || 1280,
3494
+ width: targetWidth,
3484
3495
  height: targetHeight
3485
3496
  });
3486
3497
  await (0, import_delay3.default)(1e3);
@@ -3490,7 +3501,7 @@ var Share = {
3490
3501
  });
3491
3502
  return buffer_.toString("base64");
3492
3503
  } finally {
3493
- if (restore) {
3504
+ if (restoreMode !== "none") {
3494
3505
  await page.evaluate(() => {
3495
3506
  document.querySelectorAll(".__pk_expanded__").forEach((el) => {
3496
3507
  el.style.overflow = el.dataset.pkOrigOverflow || "";
@@ -3503,7 +3514,16 @@ var Share = {
3503
3514
  });
3504
3515
  });
3505
3516
  if (originalViewport) {
3506
- await page.setViewportSize(originalViewport);
3517
+ const currentViewport = page.viewportSize() || {
3518
+ width: targetWidth,
3519
+ height: maxHeight
3520
+ };
3521
+ const shouldRestoreWidth = restoreMode === "all" || restoreMode === "width-only";
3522
+ const shouldRestoreHeight = restoreMode === "all" || restoreMode === "height-only";
3523
+ await page.setViewportSize({
3524
+ width: shouldRestoreWidth ? originalViewport.width : currentViewport.width,
3525
+ height: shouldRestoreHeight ? originalViewport.height : currentViewport.height
3526
+ });
3507
3527
  }
3508
3528
  }
3509
3529
  }