@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.cjs +34 -15
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +34 -15
- package/dist/index.js.map +3 -3
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1337,8 +1337,11 @@ var Blocking = {
|
|
|
1337
1337
|
};
|
|
1338
1338
|
},
|
|
1339
1339
|
/**
|
|
1340
|
-
* 设置 CDN
|
|
1341
|
-
*
|
|
1340
|
+
* 设置 CDN 直连规则(与 setupBlockingResources 协同工作)
|
|
1341
|
+
*
|
|
1342
|
+
* 对于指定域名:
|
|
1343
|
+
* 1. 如果资源在屏蔽列表中 → 直接 abort(不请求)
|
|
1344
|
+
* 2. 如果资源不在屏蔽列表中 → 使用 Node.js 原生 fetch 直连获取(绕过浏览器代理)
|
|
1342
1345
|
*
|
|
1343
1346
|
* 适用场景:
|
|
1344
1347
|
* - 代理 IP 无法访问某些 CDN 域名(如 statics.moonshot.cn)
|
|
@@ -1349,33 +1352,37 @@ var Blocking = {
|
|
|
1349
1352
|
* - 如果在海外服务器(如 Apify 云端)运行,直连仍使用海外 IP,可能依然无法访问
|
|
1350
1353
|
*
|
|
1351
1354
|
* @param {import('playwright').Page} page - Playwright Page 对象
|
|
1352
|
-
* @param {Object}
|
|
1353
|
-
* @param {string[]}
|
|
1354
|
-
* @param {
|
|
1355
|
+
* @param {Object} options - 配置选项
|
|
1356
|
+
* @param {string[]} options.domains - 需要直连的域名列表(必填)
|
|
1357
|
+
* @param {Object} [options.blockingConfig] - 资源屏蔽配置(与 setupBlockingResources 相同)
|
|
1355
1358
|
* @param {boolean} [options.fallbackToProxy] - 直连失败时是否回退到代理(默认 true)
|
|
1356
1359
|
* @returns {Promise<void>}
|
|
1357
1360
|
*/
|
|
1358
1361
|
async setupDirectConnect(page, options = {}) {
|
|
1359
1362
|
const {
|
|
1360
1363
|
domains = [],
|
|
1361
|
-
|
|
1364
|
+
blockingConfig = {},
|
|
1362
1365
|
fallbackToProxy = true
|
|
1363
1366
|
} = options;
|
|
1364
1367
|
if (domains.length === 0) {
|
|
1365
1368
|
logger9.warn("setupDirectConnect", "\u672A\u6307\u5B9A\u57DF\u540D\u5217\u8868\uFF0C\u8DF3\u8FC7\u76F4\u8FDE\u914D\u7F6E");
|
|
1366
1369
|
return;
|
|
1367
1370
|
}
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
+
const mergedBlockingConfig = { ...DEFAULT_BLOCKING_CONFIG, ...blockingConfig };
|
|
1372
|
+
const blockedExtensions = this.getBlockedExtensions(mergedBlockingConfig);
|
|
1373
|
+
logger9.start("setupDirectConnect", `\u76F4\u8FDE\u57DF\u540D: [${domains.join(", ")}]`);
|
|
1371
1374
|
await page.route("**/*", async (route) => {
|
|
1372
1375
|
const request = route.request();
|
|
1373
1376
|
const url = request.url();
|
|
1374
1377
|
const urlLower = url.toLowerCase();
|
|
1375
1378
|
const urlPath = urlLower.split("?")[0];
|
|
1376
1379
|
const matchesDomain = domains.some((domain) => url.includes(domain));
|
|
1377
|
-
|
|
1378
|
-
|
|
1380
|
+
if (matchesDomain) {
|
|
1381
|
+
const shouldBlock2 = blockedExtensions.some((ext) => urlPath.endsWith(ext));
|
|
1382
|
+
if (shouldBlock2) {
|
|
1383
|
+
logger9.debug(`blocked: ${url.substring(0, 100)}`);
|
|
1384
|
+
return route.abort();
|
|
1385
|
+
}
|
|
1379
1386
|
try {
|
|
1380
1387
|
const response = await fetch(url, {
|
|
1381
1388
|
method: request.method(),
|
|
@@ -1391,7 +1398,6 @@ var Blocking = {
|
|
|
1391
1398
|
response.headers.forEach((value, key) => {
|
|
1392
1399
|
headers[key] = value;
|
|
1393
1400
|
});
|
|
1394
|
-
directCount++;
|
|
1395
1401
|
logger9.debug(`direct: ${url.substring(0, 100)}`);
|
|
1396
1402
|
await route.fulfill({
|
|
1397
1403
|
status: response.status,
|
|
@@ -1401,18 +1407,31 @@ var Blocking = {
|
|
|
1401
1407
|
return;
|
|
1402
1408
|
} catch (e) {
|
|
1403
1409
|
if (fallbackToProxy) {
|
|
1404
|
-
fallbackCount++;
|
|
1405
1410
|
logger9.debug(`fallback: ${url.substring(0, 80)} (${e.message})`);
|
|
1406
1411
|
return route.continue();
|
|
1407
1412
|
} else {
|
|
1408
|
-
logger9.warn("setupDirectConnect", `\u76F4\u8FDE\u5931\u8D25
|
|
1413
|
+
logger9.warn("setupDirectConnect", `\u76F4\u8FDE\u5931\u8D25: ${url.substring(0, 80)}`);
|
|
1409
1414
|
return route.abort();
|
|
1410
1415
|
}
|
|
1411
1416
|
}
|
|
1412
1417
|
}
|
|
1418
|
+
const shouldBlock = blockedExtensions.some((ext) => urlPath.endsWith(ext));
|
|
1419
|
+
if (shouldBlock) {
|
|
1420
|
+
logger9.debug(`blocked: ${url.substring(0, 100)}`);
|
|
1421
|
+
return route.abort();
|
|
1422
|
+
}
|
|
1413
1423
|
return route.continue();
|
|
1414
1424
|
});
|
|
1415
|
-
|
|
1425
|
+
const enabledCategories = [];
|
|
1426
|
+
if (mergedBlockingConfig.blockArchive) enabledCategories.push("\u538B\u7F29\u5305");
|
|
1427
|
+
if (mergedBlockingConfig.blockExecutable) enabledCategories.push("\u53EF\u6267\u884C\u6587\u4EF6");
|
|
1428
|
+
if (mergedBlockingConfig.blockDocument) enabledCategories.push("\u529E\u516C\u6587\u6863");
|
|
1429
|
+
if (mergedBlockingConfig.blockImage) enabledCategories.push("\u56FE\u7247");
|
|
1430
|
+
if (mergedBlockingConfig.blockMedia) enabledCategories.push("\u97F3\u89C6\u9891");
|
|
1431
|
+
if (mergedBlockingConfig.blockFont) enabledCategories.push("\u5B57\u4F53");
|
|
1432
|
+
if (mergedBlockingConfig.blockCss) enabledCategories.push("CSS");
|
|
1433
|
+
if (mergedBlockingConfig.blockOther) enabledCategories.push("\u5176\u4ED6");
|
|
1434
|
+
logger9.success("setupDirectConnect", `\u5C4F\u853D\u5206\u7C7B: [${enabledCategories.join(", ")}]`);
|
|
1416
1435
|
}
|
|
1417
1436
|
};
|
|
1418
1437
|
|