@skrillex1224/playwright-toolkit 2.0.92 → 2.0.93

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
@@ -1308,8 +1308,11 @@ var Blocking = {
1308
1308
  };
1309
1309
  },
1310
1310
  /**
1311
- * 设置 CDN 直连规则
1312
- * 对于指定域名的请求,使用 Node.js 原生 fetch 直连获取(绕过浏览器代理)
1311
+ * 设置 CDN 直连规则(与 setupBlockingResources 协同工作)
1312
+ *
1313
+ * 对于指定域名:
1314
+ * 1. 如果资源在屏蔽列表中 → 直接 abort(不请求)
1315
+ * 2. 如果资源不在屏蔽列表中 → 使用 Node.js 原生 fetch 直连获取(绕过浏览器代理)
1313
1316
  *
1314
1317
  * 适用场景:
1315
1318
  * - 代理 IP 无法访问某些 CDN 域名(如 statics.moonshot.cn)
@@ -1320,33 +1323,37 @@ var Blocking = {
1320
1323
  * - 如果在海外服务器(如 Apify 云端)运行,直连仍使用海外 IP,可能依然无法访问
1321
1324
  *
1322
1325
  * @param {import('playwright').Page} page - Playwright Page 对象
1323
- * @param {Object} [options] - 配置选项
1324
- * @param {string[]} [options.domains] - 需要直连的域名列表
1325
- * @param {string[]} [options.extensions] - 需要直连的扩展名列表(可选,默认 CSS/JS/字体)
1326
+ * @param {Object} options - 配置选项
1327
+ * @param {string[]} options.domains - 需要直连的域名列表(必填)
1328
+ * @param {Object} [options.blockingConfig] - 资源屏蔽配置(与 setupBlockingResources 相同)
1326
1329
  * @param {boolean} [options.fallbackToProxy] - 直连失败时是否回退到代理(默认 true)
1327
1330
  * @returns {Promise<void>}
1328
1331
  */
1329
1332
  async setupDirectConnect(page, options = {}) {
1330
1333
  const {
1331
1334
  domains = [],
1332
- extensions = [".css", ".js", ".woff", ".woff2", ".ttf", ".otf"],
1335
+ blockingConfig = {},
1333
1336
  fallbackToProxy = true
1334
1337
  } = options;
1335
1338
  if (domains.length === 0) {
1336
1339
  logger9.warn("setupDirectConnect", "\u672A\u6307\u5B9A\u57DF\u540D\u5217\u8868\uFF0C\u8DF3\u8FC7\u76F4\u8FDE\u914D\u7F6E");
1337
1340
  return;
1338
1341
  }
1339
- logger9.start("setupDirectConnect", `\u57DF\u540D: [${domains.join(", ")}]`);
1340
- let directCount = 0;
1341
- let fallbackCount = 0;
1342
+ const mergedBlockingConfig = { ...DEFAULT_BLOCKING_CONFIG, ...blockingConfig };
1343
+ const blockedExtensions = this.getBlockedExtensions(mergedBlockingConfig);
1344
+ logger9.start("setupDirectConnect", `\u76F4\u8FDE\u57DF\u540D: [${domains.join(", ")}]`);
1342
1345
  await page.route("**/*", async (route) => {
1343
1346
  const request = route.request();
1344
1347
  const url = request.url();
1345
1348
  const urlLower = url.toLowerCase();
1346
1349
  const urlPath = urlLower.split("?")[0];
1347
1350
  const matchesDomain = domains.some((domain) => url.includes(domain));
1348
- const matchesExtension = extensions.length === 0 || extensions.some((ext) => urlPath.endsWith(ext));
1349
- if (matchesDomain && matchesExtension) {
1351
+ if (matchesDomain) {
1352
+ const shouldBlock2 = blockedExtensions.some((ext) => urlPath.endsWith(ext));
1353
+ if (shouldBlock2) {
1354
+ logger9.debug(`blocked: ${url.substring(0, 100)}`);
1355
+ return route.abort();
1356
+ }
1350
1357
  try {
1351
1358
  const response = await fetch(url, {
1352
1359
  method: request.method(),
@@ -1362,7 +1369,6 @@ var Blocking = {
1362
1369
  response.headers.forEach((value, key) => {
1363
1370
  headers[key] = value;
1364
1371
  });
1365
- directCount++;
1366
1372
  logger9.debug(`direct: ${url.substring(0, 100)}`);
1367
1373
  await route.fulfill({
1368
1374
  status: response.status,
@@ -1372,18 +1378,31 @@ var Blocking = {
1372
1378
  return;
1373
1379
  } catch (e) {
1374
1380
  if (fallbackToProxy) {
1375
- fallbackCount++;
1376
1381
  logger9.debug(`fallback: ${url.substring(0, 80)} (${e.message})`);
1377
1382
  return route.continue();
1378
1383
  } else {
1379
- logger9.warn("setupDirectConnect", `\u76F4\u8FDE\u5931\u8D25\u4E14\u4E0D\u56DE\u9000: ${url.substring(0, 80)}`);
1384
+ logger9.warn("setupDirectConnect", `\u76F4\u8FDE\u5931\u8D25: ${url.substring(0, 80)}`);
1380
1385
  return route.abort();
1381
1386
  }
1382
1387
  }
1383
1388
  }
1389
+ const shouldBlock = blockedExtensions.some((ext) => urlPath.endsWith(ext));
1390
+ if (shouldBlock) {
1391
+ logger9.debug(`blocked: ${url.substring(0, 100)}`);
1392
+ return route.abort();
1393
+ }
1384
1394
  return route.continue();
1385
1395
  });
1386
- logger9.success("setupDirectConnect", `\u6269\u5C55\u540D: [${extensions.join(", ")}]`);
1396
+ const enabledCategories = [];
1397
+ if (mergedBlockingConfig.blockArchive) enabledCategories.push("\u538B\u7F29\u5305");
1398
+ if (mergedBlockingConfig.blockExecutable) enabledCategories.push("\u53EF\u6267\u884C\u6587\u4EF6");
1399
+ if (mergedBlockingConfig.blockDocument) enabledCategories.push("\u529E\u516C\u6587\u6863");
1400
+ if (mergedBlockingConfig.blockImage) enabledCategories.push("\u56FE\u7247");
1401
+ if (mergedBlockingConfig.blockMedia) enabledCategories.push("\u97F3\u89C6\u9891");
1402
+ if (mergedBlockingConfig.blockFont) enabledCategories.push("\u5B57\u4F53");
1403
+ if (mergedBlockingConfig.blockCss) enabledCategories.push("CSS");
1404
+ if (mergedBlockingConfig.blockOther) enabledCategories.push("\u5176\u4ED6");
1405
+ logger9.success("setupDirectConnect", `\u5C4F\u853D\u5206\u7C7B: [${enabledCategories.join(", ")}]`);
1387
1406
  }
1388
1407
  };
1389
1408