@skrillex1224/playwright-toolkit 3.0.10 → 3.0.11

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/browser.js CHANGED
@@ -2853,7 +2853,6 @@ var createActorInfo = (info) => {
2853
2853
  const buildLandingUrl = ({ protocol: protocol2, domain: domain2, path: path2 }) => {
2854
2854
  const safeProtocol = String(protocol2).trim();
2855
2855
  const safeDomain = normalizeDomain(domain2);
2856
- if (!safeDomain) return "";
2857
2856
  const safePath = normalizePath(path2);
2858
2857
  return `${safeProtocol}://${safeDomain}${safePath}`;
2859
2858
  };
@@ -3061,17 +3060,6 @@ var ActorInfo = {
3061
3060
  prefix: "",
3062
3061
  xurl: []
3063
3062
  }
3064
- }),
3065
- webpage: createActorInfo({
3066
- key: "webpage",
3067
- name: "\u901A\u7528\u7F51\u9875",
3068
- domain: "",
3069
- path: "/",
3070
- share: {
3071
- mode: "dom",
3072
- prefix: "",
3073
- xurl: []
3074
- }
3075
3063
  })
3076
3064
  };
3077
3065
 
package/dist/index.cjs CHANGED
@@ -149,7 +149,6 @@ var createActorInfo = (info) => {
149
149
  const buildLandingUrl = ({ protocol: protocol2, domain: domain2, path: path4 }) => {
150
150
  const safeProtocol = String(protocol2).trim();
151
151
  const safeDomain = normalizeDomain(domain2);
152
- if (!safeDomain) return "";
153
152
  const safePath = normalizePath(path4);
154
153
  return `${safeProtocol}://${safeDomain}${safePath}`;
155
154
  };
@@ -357,17 +356,6 @@ var ActorInfo = {
357
356
  prefix: "",
358
357
  xurl: []
359
358
  }
360
- }),
361
- webpage: createActorInfo({
362
- key: "webpage",
363
- name: "\u901A\u7528\u7F51\u9875",
364
- domain: "",
365
- path: "/",
366
- share: {
367
- mode: "dom",
368
- prefix: "",
369
- xurl: []
370
- }
371
359
  })
372
360
  };
373
361
 
@@ -1208,18 +1196,6 @@ var ensureLogPath = () => {
1208
1196
  const label = runId ? `proxy-meter-${runId}-${suffix}.json` : `proxy-meter-${process.pid}-${suffix}.json`;
1209
1197
  return import_path.default.join(baseDir, label);
1210
1198
  };
1211
- var ensureStdioLogPath = (logPath) => `${logPath}.stdio.log`;
1212
- var writeChildOutput = (stream, output, prefix) => {
1213
- if (!stream || !output) return;
1214
- output.on("data", (chunk) => {
1215
- const text = chunk?.toString?.() || String(chunk || "");
1216
- if (!text) return;
1217
- for (const line of text.split(/\r?\n/)) {
1218
- if (!line) continue;
1219
- stream.write(`[${(/* @__PURE__ */ new Date()).toISOString()}] ${prefix} ${line}\n`);
1220
- }
1221
- });
1222
- };
1223
1199
  var readSnapshot = (logPath) => {
1224
1200
  if (!logPath || !(0, import_fs.existsSync)(logPath)) return null;
1225
1201
  try {
@@ -1387,7 +1363,6 @@ var startProxyMeter = (options = {}) => {
1387
1363
  observedDomainResourceTypes = /* @__PURE__ */ new Map();
1388
1364
  const port = pickFreePort();
1389
1365
  const logPath = ensureLogPath();
1390
- const stdioLogPath = ensureStdioLogPath(logPath);
1391
1366
  const scriptPath = resolveScriptPath();
1392
1367
  const debugMode = Boolean(options.debugMode);
1393
1368
  const debugMaxEvents = Math.max(10, toSafeInt(options.debugMaxEvents) || DEFAULT_DEBUG_MAX_EVENTS);
@@ -1400,26 +1375,19 @@ var startProxyMeter = (options = {}) => {
1400
1375
  PROXY_METER_DEBUG: debugMode ? "1" : "0",
1401
1376
  PROXY_METER_DEBUG_MAX_EVENTS: String(debugMaxEvents)
1402
1377
  };
1403
- const stdioLog = (0, import_fs.createWriteStream)(stdioLogPath, { flags: "a" });
1404
- stdioLog.write(`[${(/* @__PURE__ */ new Date()).toISOString()}] [proxy-meter-runtime] start script=${scriptPath} port=${port} snapshot=${logPath}\n`);
1405
1378
  const child = (0, import_child_process.spawn)(process.execPath, [scriptPath], {
1406
1379
  env,
1407
- stdio: ["ignore", "pipe", "pipe"]
1380
+ stdio: ["ignore", "ignore", "ignore"]
1408
1381
  });
1409
- writeChildOutput(stdioLog, child.stdout, "stdout");
1410
- writeChildOutput(stdioLog, child.stderr, "stderr");
1411
- child.once("exit", (code, signal) => {
1412
- stdioLog.write(`[${(/* @__PURE__ */ new Date()).toISOString()}] [proxy-meter-runtime] exit code=${code ?? ""} signal=${signal ?? ""}\n`);
1413
- stdioLog.end();
1382
+ child.once("exit", (code) => {
1414
1383
  if (code && code !== 0) {
1415
- logger2.warn(`[proxy-meter] exited with code ${code}; stdio=${stdioLogPath}`);
1384
+ logger2.warn(`[proxy-meter] exited with code ${code}`);
1416
1385
  }
1417
1386
  });
1418
1387
  runtime = {
1419
1388
  proc: child,
1420
1389
  port,
1421
1390
  logPath,
1422
- stdioLogPath,
1423
1391
  startedAt: Date.now()
1424
1392
  };
1425
1393
  registerCleanup();
@@ -5643,9 +5611,6 @@ var LiveView = {
5643
5611
  useLiveView
5644
5612
  };
5645
5613
 
5646
- // src/chaptcha.js
5647
- var import_uuid = require("uuid");
5648
-
5649
5614
  // src/internals/captcha/bytedance.js
5650
5615
  var import_promises = require("fs/promises");
5651
5616
  var import_path2 = __toESM(require("path"), 1);
@@ -6304,6 +6269,9 @@ var sloveCaptcha = solveCaptcha;
6304
6269
 
6305
6270
  // src/chaptcha.js
6306
6271
  var logger12 = createInternalLogger("Captcha");
6272
+ var DOM_MONITOR_WAIT_TIMEOUT_MS = 1e3;
6273
+ var DOM_MONITOR_POST_DETECT_HIDDEN_WAIT_MS = 300;
6274
+ var DOM_MONITOR_RECOVERY_WAIT_MS = 100;
6307
6275
  var DEFAULT_CAPTCHA_RECOGNITION_OPTIONS = Object.freeze({
6308
6276
  token: "eKJvBfwfN0YRav0-VD_44E2VBSfm7l0YtddUQ7cFySI",
6309
6277
  apiUrl: "https://api.jfbym.com/api/YmServer/customApi"
@@ -6324,6 +6292,15 @@ var mergeDefinedOptions = (...sources) => {
6324
6292
  }
6325
6293
  return merged;
6326
6294
  };
6295
+ var sleep = (ms) => new Promise((resolve) => {
6296
+ setTimeout(resolve, ms);
6297
+ });
6298
+ var getErrorMessage = (error) => String(error?.message || error || "");
6299
+ var isTimeoutError = (error) => error?.name === "TimeoutError" || getErrorMessage(error).includes("Timeout");
6300
+ var isPageLifecycleError = (error) => {
6301
+ const message = getErrorMessage(error);
6302
+ return message.includes("Execution context was destroyed") || message.includes("Frame was detached") || message.includes("Target page, context or browser has been closed") || message.includes("Target closed") || message.includes("Most likely the page has been closed");
6303
+ };
6327
6304
  function useCaptchaMonitor(page, options) {
6328
6305
  const { domSelector, urlPattern, onDetected } = options;
6329
6306
  if (!domSelector && !urlPattern) {
@@ -6335,10 +6312,13 @@ function useCaptchaMonitor(page, options) {
6335
6312
  let isStopped = false;
6336
6313
  let isHandling = false;
6337
6314
  let frameHandler = null;
6338
- let exposedFunctionName = null;
6315
+ let domMonitorTask = null;
6316
+ let lastTriggeredAt = 0;
6339
6317
  const triggerDetected = async () => {
6340
- if (isStopped || isHandling) return;
6318
+ const now = Date.now();
6319
+ if (isStopped || isHandling || now - lastTriggeredAt < 250) return;
6341
6320
  isHandling = true;
6321
+ lastTriggeredAt = now;
6342
6322
  try {
6343
6323
  await onDetected();
6344
6324
  } finally {
@@ -6347,60 +6327,38 @@ function useCaptchaMonitor(page, options) {
6347
6327
  };
6348
6328
  const cleanupFns = [];
6349
6329
  if (domSelector) {
6350
- exposedFunctionName = `__c_d_${(0, import_uuid.v4)().replace(/-/g, "_")}`;
6351
- const cleanerName = `__c_cleaner_${(0, import_uuid.v4)().replace(/-/g, "_")}`;
6352
- page.exposeFunction(exposedFunctionName, triggerDetected).catch(() => {
6353
- });
6354
- page.addInitScript(({ selector, callbackName, cleanerName: cleanupName }) => {
6355
- (() => {
6356
- let observer = null;
6357
- const checkAndReport = () => {
6358
- const element = document.querySelector(selector);
6359
- if (!element) {
6360
- return false;
6361
- }
6362
- if (window[callbackName]) {
6363
- window[callbackName]();
6364
- }
6365
- return true;
6366
- };
6367
- checkAndReport();
6368
- observer = new MutationObserver((mutations) => {
6369
- const shouldCheck = mutations.some((mutation) => mutation.addedNodes.length > 0);
6370
- if (shouldCheck && observer) {
6371
- checkAndReport();
6372
- }
6373
- });
6374
- const mountObserver = () => {
6375
- const target = document.documentElement;
6376
- if (target && observer) {
6377
- observer.observe(target, { childList: true, subtree: true });
6330
+ domMonitorTask = (async () => {
6331
+ const locator = page.locator(domSelector).first();
6332
+ while (!isStopped) {
6333
+ try {
6334
+ await locator.waitFor({ state: "visible", timeout: DOM_MONITOR_WAIT_TIMEOUT_MS });
6335
+ if (isStopped) break;
6336
+ await triggerDetected();
6337
+ if (isStopped) break;
6338
+ await locator.waitFor({
6339
+ state: "hidden",
6340
+ timeout: DOM_MONITOR_POST_DETECT_HIDDEN_WAIT_MS
6341
+ }).catch(() => {
6342
+ });
6343
+ } catch (error) {
6344
+ if (isStopped) break;
6345
+ if (page?.isClosed?.() && isPageLifecycleError(error)) break;
6346
+ if (isTimeoutError(error) || isPageLifecycleError(error)) {
6347
+ await sleep(DOM_MONITOR_RECOVERY_WAIT_MS);
6348
+ continue;
6378
6349
  }
6379
- };
6380
- if (document.readyState === "loading") {
6381
- window.addEventListener("DOMContentLoaded", mountObserver);
6382
- } else {
6383
- mountObserver();
6350
+ logger12.warning(
6351
+ "useCaptchaMonitor",
6352
+ `DOM \u76D1\u63A7\u51FA\u73B0\u5F02\u5E38\uFF08\u7EE7\u7EED\u91CD\u8BD5\uFF09: selector=${domSelector}, error=${getErrorMessage(error)}`
6353
+ );
6354
+ await sleep(DOM_MONITOR_RECOVERY_WAIT_MS);
6384
6355
  }
6385
- window[cleanupName] = () => {
6386
- if (observer) {
6387
- observer.disconnect();
6388
- observer = null;
6389
- }
6390
- };
6391
- })();
6392
- }, { selector: domSelector, callbackName: exposedFunctionName, cleanerName });
6356
+ }
6357
+ })();
6393
6358
  logger12.success("useCaptchaMonitor", `DOM \u76D1\u63A7\u5DF2\u542F\u7528\uFF1A${domSelector}`);
6394
6359
  cleanupFns.push(async () => {
6395
- try {
6396
- await page.evaluate((name) => {
6397
- if (window[name]) {
6398
- window[name]();
6399
- delete window[name];
6400
- }
6401
- }, cleanerName);
6402
- } catch {
6403
- }
6360
+ await domMonitorTask?.catch(() => {
6361
+ });
6404
6362
  });
6405
6363
  }
6406
6364
  if (urlPattern) {
@@ -6415,6 +6373,12 @@ function useCaptchaMonitor(page, options) {
6415
6373
  };
6416
6374
  page.on("framenavigated", frameHandler);
6417
6375
  logger12.success("useCaptchaMonitor", `URL \u76D1\u63A7\u5DF2\u542F\u7528\uFF1A${urlPattern}`);
6376
+ Promise.resolve().then(async () => {
6377
+ if (!isStopped && page.url().includes(urlPattern)) {
6378
+ await triggerDetected();
6379
+ }
6380
+ }).catch(() => {
6381
+ });
6418
6382
  cleanupFns.push(async () => {
6419
6383
  page.off("framenavigated", frameHandler);
6420
6384
  });
@@ -6422,10 +6386,10 @@ function useCaptchaMonitor(page, options) {
6422
6386
  return {
6423
6387
  stop: async () => {
6424
6388
  logger12.info("\u6B63\u5728\u505C\u6B62\u9A8C\u8BC1\u7801\u76D1\u63A7...");
6389
+ isStopped = true;
6425
6390
  for (const fn of cleanupFns) {
6426
6391
  await fn();
6427
6392
  }
6428
- isStopped = true;
6429
6393
  }
6430
6394
  };
6431
6395
  }
@@ -6470,7 +6434,7 @@ var Captcha = {
6470
6434
 
6471
6435
  // src/mutation.js
6472
6436
  var import_node_crypto = require("node:crypto");
6473
- var import_uuid2 = require("uuid");
6437
+ var import_uuid = require("uuid");
6474
6438
  var logger13 = createInternalLogger("Mutation");
6475
6439
  var MUTATION_MONITOR_MODE = Object.freeze({
6476
6440
  Added: "added",
@@ -6478,7 +6442,7 @@ var MUTATION_MONITOR_MODE = Object.freeze({
6478
6442
  All: "all"
6479
6443
  });
6480
6444
  function generateKey(prefix) {
6481
- return `__${prefix}_${(0, import_uuid2.v4)().replace(/-/g, "_")}`;
6445
+ return `__${prefix}_${(0, import_uuid.v4)().replace(/-/g, "_")}`;
6482
6446
  }
6483
6447
  var Mutation = {
6484
6448
  Mode: MUTATION_MONITOR_MODE,
@@ -6668,7 +6632,7 @@ var Mutation = {
6668
6632
  const overallTimeout = options.timeout ?? 180 * 1e3;
6669
6633
  const onMutation = options.onMutation;
6670
6634
  const pollInterval = 500;
6671
- const sleep = (ms) => new Promise((resolve) => {
6635
+ const sleep2 = (ms) => new Promise((resolve) => {
6672
6636
  setTimeout(resolve, ms);
6673
6637
  });
6674
6638
  const truncate = (value, max = 800) => {
@@ -6860,7 +6824,7 @@ var Mutation = {
6860
6824
  const deadline = Date.now() + overallTimeout;
6861
6825
  let lastState = state2;
6862
6826
  while (Date.now() < deadline) {
6863
- await sleep(pollInterval);
6827
+ await sleep2(pollInterval);
6864
6828
  lastState = await buildState();
6865
6829
  if (!lastState?.hasMatched) {
6866
6830
  continue;