@skrillex1224/playwright-toolkit 2.1.238 → 2.1.240

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/dist/index.js CHANGED
@@ -56,7 +56,7 @@ var createActorInfo = (info) => {
56
56
  const normalizeShare2 = (value) => {
57
57
  const raw = value && typeof value === "object" ? value : {};
58
58
  const modeRaw = String(raw.mode || "dom").trim().toLowerCase();
59
- const mode = modeRaw === "response" ? "response" : "dom";
59
+ const mode = ["dom", "response", "custom"].includes(modeRaw) ? modeRaw : "dom";
60
60
  const prefix = String(raw.prefix || "").trim();
61
61
  const rawXurl = Array.isArray(raw.xurl) ? raw.xurl : [];
62
62
  const normalizeMatcherList = (input) => {
@@ -213,7 +213,7 @@ var ActorInfo = {
213
213
  path: "/",
214
214
  device: Device.Mobile,
215
215
  share: {
216
- mode: "dom",
216
+ mode: "custom",
217
217
  prefix: "",
218
218
  xurl: []
219
219
  }
@@ -8339,7 +8339,7 @@ var normalizeXurl = (value) => {
8339
8339
  var normalizeShare = (share) => {
8340
8340
  const source = share && typeof share === "object" ? share : {};
8341
8341
  const modeRaw = String(source.mode || "dom").trim().toLowerCase();
8342
- const mode = modeRaw === "response" ? "response" : "dom";
8342
+ const mode = ["dom", "response", "custom"].includes(modeRaw) ? modeRaw : "dom";
8343
8343
  return {
8344
8344
  mode,
8345
8345
  prefix: normalizePrefix(source.prefix),
@@ -8455,16 +8455,17 @@ var Share = {
8455
8455
  * 捕获分享链接(单模式)
8456
8456
  * - mode='dom': 仅 DOM 监听
8457
8457
  * - mode='response': 仅接口监听
8458
+ * - mode='custom': 使用 performActions 返回值作为链接
8458
8459
  *
8459
8460
  * @param {import('playwright').Page} page
8460
8461
  * @param {Object} [options]
8461
- * @param {{ mode?: 'dom' | 'response', prefix: string, xurl?: Array<string | string[]> }} options.share
8462
+ * @param {{ mode?: 'dom' | 'response' | 'custom', prefix: string, xurl?: Array<string | string[]> }} options.share
8462
8463
  * @param {number} [options.timeoutMs=50000]
8463
8464
  * @param {number} [options.payloadSnapshotMaxLen=500]
8464
8465
  * @param {string | string[]} [options.domSelectors='html']
8465
8466
  * @param {'added' | 'changed' | 'all'} [options.domMode='all']
8466
- * @param {() => Promise<void>} [options.performActions]
8467
- * @returns {Promise<{ link: string | null; payloadText: string; payloadSnapshot: string; source: 'dom' | 'response' | 'none' }>}
8467
+ * @param {() => void | string | { link?: string | null; payloadText?: string } | Promise<void | string | { link?: string | null; payloadText?: string }>} [options.performActions]
8468
+ * @returns {Promise<{ link: string | null; payloadText: string; payloadSnapshot: string; source: 'dom' | 'response' | 'custom' | 'none' }>}
8468
8469
  */
8469
8470
  async captureLink(page, options = {}) {
8470
8471
  const share = normalizeShare(options.share);
@@ -8477,7 +8478,7 @@ var Share = {
8477
8478
  if (share.prefix === void 0) {
8478
8479
  throw new Error("Share.captureLink requires options.share.prefix");
8479
8480
  }
8480
- if (share.mode !== "dom" && share.mode !== "response") {
8481
+ if (share.mode !== "dom" && share.mode !== "response" && share.mode !== "custom") {
8481
8482
  throw new Error(`Share.captureLink unsupported share.mode: ${share.mode}`);
8482
8483
  }
8483
8484
  const apiMatchers = share.mode === "response" ? share.xurl[0] || [] : [];
@@ -8499,7 +8500,8 @@ var Share = {
8499
8500
  };
8500
8501
  const candidates = {
8501
8502
  dom: null,
8502
- response: null
8503
+ response: null,
8504
+ custom: null
8503
8505
  };
8504
8506
  const setCandidate = (source, link, payloadText = "") => {
8505
8507
  const safe = String(link || "").trim();
@@ -8599,37 +8601,47 @@ var Share = {
8599
8601
  logger15.info(`\u5F53\u524D\u4E3A\u63A5\u53E3\u6A21\u5F0F\uFF0C\u6302\u8F7D response \u76D1\u542C: apiMatchers=${toJsonInline(apiMatchers, 160)}`);
8600
8602
  page.on("response", onResponse);
8601
8603
  }
8604
+ if (share.mode === "custom") {
8605
+ logger15.info("\u5F53\u524D\u4E3A custom \u6A21\u5F0F\uFF0C\u5C06\u4F7F\u7528 performActions \u8FD4\u56DE\u503C");
8606
+ }
8602
8607
  const deadline = Date.now() + timeoutMs;
8603
8608
  const getRemainingMs = () => Math.max(0, deadline - Date.now());
8604
8609
  try {
8605
8610
  const actionTimeout = getRemainingMs();
8606
8611
  logger15.start("captureLink.performActions", `\u6267\u884C\u52A8\u4F5C\u9884\u7B97=${actionTimeout}ms`);
8612
+ let actionValue;
8607
8613
  if (actionTimeout > 0) {
8608
8614
  let timer = null;
8609
- let actionError = null;
8610
- const actionPromise = Promise.resolve().then(() => performActions()).then(() => "__ACTION_DONE__").catch((error) => {
8611
- actionError = error;
8612
- return "__ACTION_ERROR__";
8613
- });
8615
+ const actionPromise = Promise.resolve().then(() => performActions()).then((result) => ({ type: "done", result })).catch((error) => ({ type: "error", error }));
8614
8616
  const timeoutPromise = new Promise((resolve) => {
8615
- timer = setTimeout(() => resolve("__ACTION_TIMEOUT__"), actionTimeout);
8617
+ timer = setTimeout(() => resolve({ type: "timeout" }), actionTimeout);
8616
8618
  });
8617
8619
  const actionResult = await Promise.race([actionPromise, timeoutPromise]);
8618
8620
  if (timer) clearTimeout(timer);
8619
- if (actionResult === "__ACTION_ERROR__") {
8620
- logger15.fail("captureLink.performActions", actionError);
8621
- throw actionError;
8621
+ if (actionResult.type === "error") {
8622
+ logger15.fail("captureLink.performActions", actionResult.error);
8623
+ throw actionResult.error;
8622
8624
  }
8623
- if (actionResult === "__ACTION_TIMEOUT__") {
8625
+ if (actionResult.type === "timeout") {
8624
8626
  stats.actionTimedOut = true;
8625
8627
  logger15.warning(`performActions \u5DF2\u8D85\u65F6 (${actionTimeout}ms)\uFF0C\u52A8\u4F5C\u53EF\u80FD\u4ECD\u5728\u5F02\u6B65\u6267\u884C`);
8626
8628
  } else {
8629
+ actionValue = actionResult.result;
8627
8630
  logger15.success("captureLink.performActions", "\u6267\u884C\u52A8\u4F5C\u5B8C\u6210");
8628
8631
  }
8629
8632
  }
8633
+ if (share.mode === "custom") {
8634
+ const customLink = typeof actionValue === "string" ? actionValue : actionValue?.link || actionValue?.payloadText;
8635
+ const customPayloadText = typeof actionValue === "string" ? actionValue : actionValue?.payloadText;
8636
+ if (setCandidate("custom", customLink, customPayloadText)) {
8637
+ logger15.success("captureLink.customResult", `custom \u547D\u4E2D\u5206\u4EAB\u94FE\u63A5: link=${candidates.custom.link}`);
8638
+ } else {
8639
+ logger15.warning("performActions \u6267\u884C\u5B8C\u6210\u4F46\u672A\u8FD4\u56DE\u6709\u6548\u5206\u4EAB\u94FE\u63A5");
8640
+ }
8641
+ }
8630
8642
  let nextProgressLogTs = Date.now() + 3e3;
8631
8643
  while (true) {
8632
- const selected = share.mode === "dom" ? candidates.dom : candidates.response;
8644
+ const selected = candidates[share.mode];
8633
8645
  if (selected?.link) {
8634
8646
  logger15.success("captureLink", `\u6355\u83B7\u6210\u529F: source=${share.mode}, link=${selected.link}`);
8635
8647
  return {
@@ -8639,6 +8651,7 @@ var Share = {
8639
8651
  source: share.mode
8640
8652
  };
8641
8653
  }
8654
+ if (share.mode === "custom") break;
8642
8655
  const remaining = getRemainingMs();
8643
8656
  if (remaining <= 0) break;
8644
8657
  const now = Date.now();