@skrillex1224/playwright-toolkit 3.0.5 → 3.0.7

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
@@ -5290,6 +5290,49 @@ var normalizeStringArray = (value) => {
5290
5290
  }
5291
5291
  return value.map((item) => String(item || "").trim()).filter(Boolean);
5292
5292
  };
5293
+ var hashStableSeed = (value = "") => {
5294
+ const raw = String(value || "").trim();
5295
+ if (!raw) return "";
5296
+ let hash = 2166136261;
5297
+ for (let index = 0; index < raw.length; index += 1) {
5298
+ hash ^= raw.charCodeAt(index);
5299
+ hash = Math.imul(hash, 16777619);
5300
+ }
5301
+ const normalized = hash >>> 0 || 1;
5302
+ return String(normalized);
5303
+ };
5304
+ var extractProxySeedSource = (proxyConfiguration = {}) => {
5305
+ const config = normalizeObject2(proxyConfiguration);
5306
+ return String(config.proxy_url || "").trim();
5307
+ };
5308
+ var resolveCloakRuntimeHints = (runtimeState = {}, proxyConfiguration = {}, cloakOptions = {}) => {
5309
+ const normalizedCloakOptions = normalizeObject2(cloakOptions);
5310
+ const browserProfileCore = RuntimeEnv.getBrowserProfileCore(runtimeState);
5311
+ const baseConfig = DefaultAntiCheat.getBaseConfig();
5312
+ const locale = String(
5313
+ normalizedCloakOptions.locale || browserProfileCore?.locale || baseConfig.locale || ""
5314
+ ).trim();
5315
+ const timezone = String(
5316
+ normalizedCloakOptions.timezone || normalizedCloakOptions.timezoneId || browserProfileCore?.timezone_id || baseConfig.timezoneId || ""
5317
+ ).trim();
5318
+ const explicitArgs = normalizeStringArray(normalizedCloakOptions.args);
5319
+ const hasExplicitFingerprint = explicitArgs.some((arg) => arg.startsWith("--fingerprint="));
5320
+ const fingerprintSeedSource = String(
5321
+ runtimeState?.envId || browserProfileCore?.schema_version && JSON.stringify({
5322
+ envId: runtimeState?.envId || "",
5323
+ timezone: browserProfileCore?.timezone_id || "",
5324
+ locale: browserProfileCore?.locale || "",
5325
+ browserMajorVersion: browserProfileCore?.browser_major_version || "",
5326
+ device: browserProfileCore?.device || ""
5327
+ }) || extractProxySeedSource(proxyConfiguration)
5328
+ ).trim();
5329
+ return {
5330
+ locale,
5331
+ timezone,
5332
+ fingerprintSeed: hasExplicitFingerprint ? "" : hashStableSeed(fingerprintSeedSource),
5333
+ hasExplicitFingerprint
5334
+ };
5335
+ };
5293
5336
  var resolveCloakProxy = (proxyConfiguration = {}) => {
5294
5337
  const config = normalizeObject2(proxyConfiguration);
5295
5338
  const proxyUrl = String(config.proxy_url || "").trim();
@@ -5425,6 +5468,7 @@ var CloakLaunch = {
5425
5468
  runInHeadfulMode = false,
5426
5469
  isRunningOnApify = false,
5427
5470
  launcher = null,
5471
+ runtimeState = null,
5428
5472
  cloakOptions = {},
5429
5473
  humanizeOptions = DEFAULT_CLOAK_HUMANIZE_OPTIONS,
5430
5474
  crawlerBaseOptions = {},
@@ -5439,6 +5483,7 @@ var CloakLaunch = {
5439
5483
  const patchedBrowsers = /* @__PURE__ */ new WeakSet();
5440
5484
  const defaultArgs = isRunningOnApify ? ["--no-sandbox", "--disable-setuid-sandbox"] : [];
5441
5485
  const extraArgs = normalizeStringArray(normalizedCloakOptions.args);
5486
+ const runtimeHints = resolveCloakRuntimeHints(runtimeState, proxyConfiguration, normalizedCloakOptions);
5442
5487
  const hasExplicitProxy = hasOwn(normalizedCloakOptions, "proxy");
5443
5488
  const proxyLaunchState = hasExplicitProxy ? resolveLaunchTraffic({ proxyConfiguration, debugMode, useMeter: false }) : resolveLaunchTraffic({ proxyConfiguration, debugMode });
5444
5489
  const proxy = hasExplicitProxy ? normalizedCloakOptions.proxy : proxyLaunchState.launchProxy;
@@ -5448,7 +5493,13 @@ var CloakLaunch = {
5448
5493
  ...normalizedCloakOptions,
5449
5494
  headless,
5450
5495
  proxy,
5451
- args: [...defaultArgs, ...extraArgs]
5496
+ ...runtimeHints.locale ? { locale: runtimeHints.locale } : {},
5497
+ ...runtimeHints.timezone ? { timezone: runtimeHints.timezone } : {},
5498
+ args: [
5499
+ ...defaultArgs,
5500
+ ...extraArgs,
5501
+ ...runtimeHints.fingerprintSeed ? [`--fingerprint=${runtimeHints.fingerprintSeed}`] : []
5502
+ ]
5452
5503
  };
5453
5504
  const launchOptions = await buildCloakLaunchOptions(mergedCloakOptions);
5454
5505
  const fingerprintArg = extractFingerprintArg(launchOptions);
@@ -6275,7 +6326,7 @@ function useCaptchaMonitor(page, options) {
6275
6326
  let isStopped = false;
6276
6327
  let isHandling = false;
6277
6328
  let frameHandler = null;
6278
- let exposedFunctionName = null;
6329
+ let domFrameHandler = null;
6279
6330
  const triggerDetected = async () => {
6280
6331
  if (isStopped || isHandling) return;
6281
6332
  isHandling = true;
@@ -6287,12 +6338,16 @@ function useCaptchaMonitor(page, options) {
6287
6338
  };
6288
6339
  const cleanupFns = [];
6289
6340
  if (domSelector) {
6290
- exposedFunctionName = `__c_d_${uuidv4().replace(/-/g, "_")}`;
6341
+ const exposedFunctionName = `__c_d_${uuidv4().replace(/-/g, "_")}`;
6291
6342
  const cleanerName = `__c_cleaner_${uuidv4().replace(/-/g, "_")}`;
6292
- page.exposeFunction(exposedFunctionName, triggerDetected).catch(() => {
6293
- });
6294
- page.addInitScript(({ selector, callbackName, cleanerName: cleanupName }) => {
6343
+ const installDomMonitor = ({ selector, callbackName, cleanerName: cleanupName }) => {
6295
6344
  (() => {
6345
+ if (typeof window[cleanupName] === "function") {
6346
+ try {
6347
+ window[cleanupName]();
6348
+ } catch {
6349
+ }
6350
+ }
6296
6351
  let observer = null;
6297
6352
  const checkAndReport = () => {
6298
6353
  const element = document.querySelector(selector);
@@ -6318,7 +6373,7 @@ function useCaptchaMonitor(page, options) {
6318
6373
  }
6319
6374
  };
6320
6375
  if (document.readyState === "loading") {
6321
- window.addEventListener("DOMContentLoaded", mountObserver);
6376
+ window.addEventListener("DOMContentLoaded", mountObserver, { once: true });
6322
6377
  } else {
6323
6378
  mountObserver();
6324
6379
  }
@@ -6329,9 +6384,41 @@ function useCaptchaMonitor(page, options) {
6329
6384
  }
6330
6385
  };
6331
6386
  })();
6332
- }, { selector: domSelector, callbackName: exposedFunctionName, cleanerName });
6333
- logger12.success("useCaptchaMonitor", `DOM \u76D1\u63A7\u5DF2\u542F\u7528\uFF1A${domSelector}`);
6387
+ };
6388
+ const installArgs = {
6389
+ selector: domSelector,
6390
+ callbackName: exposedFunctionName,
6391
+ cleanerName
6392
+ };
6393
+ const domMonitorReady = (async () => {
6394
+ await page.exposeFunction(exposedFunctionName, triggerDetected);
6395
+ await page.addInitScript(installDomMonitor, installArgs);
6396
+ await page.evaluate(installDomMonitor, installArgs).catch(() => {
6397
+ });
6398
+ logger12.success("useCaptchaMonitor", `DOM \u76D1\u63A7\u5DF2\u542F\u7528\uFF1A${domSelector}`);
6399
+ })().catch((error) => {
6400
+ if (isStopped) {
6401
+ return;
6402
+ }
6403
+ logger12.warn(`DOM \u76D1\u63A7\u521D\u59CB\u5316\u5931\u8D25 (${domSelector}): ${error?.message || error}`);
6404
+ });
6405
+ domFrameHandler = async (frame) => {
6406
+ if (frame !== page.mainFrame() || isStopped) {
6407
+ return;
6408
+ }
6409
+ await domMonitorReady;
6410
+ if (isStopped) {
6411
+ return;
6412
+ }
6413
+ await page.evaluate(installDomMonitor, installArgs).catch(() => {
6414
+ });
6415
+ };
6416
+ page.on("framenavigated", domFrameHandler);
6334
6417
  cleanupFns.push(async () => {
6418
+ if (domFrameHandler) {
6419
+ page.off("framenavigated", domFrameHandler);
6420
+ }
6421
+ await domMonitorReady;
6335
6422
  try {
6336
6423
  await page.evaluate((name) => {
6337
6424
  if (window[name]) {
@@ -6362,10 +6449,10 @@ function useCaptchaMonitor(page, options) {
6362
6449
  return {
6363
6450
  stop: async () => {
6364
6451
  logger12.info("\u6B63\u5728\u505C\u6B62\u9A8C\u8BC1\u7801\u76D1\u63A7...");
6452
+ isStopped = true;
6365
6453
  for (const fn of cleanupFns) {
6366
6454
  await fn();
6367
6455
  }
6368
- isStopped = true;
6369
6456
  }
6370
6457
  };
6371
6458
  }