@skrillex1224/playwright-toolkit 2.1.133 → 2.1.135

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 CHANGED
@@ -1707,12 +1707,6 @@ var SHARED_HTTPS_AGENT = new import_https2.Agent({
1707
1707
  maxFreeSockets: 10,
1708
1708
  rejectUnauthorized: false
1709
1709
  });
1710
- var DirectConfig = {
1711
- /** 直连请求超时时间(秒) */
1712
- directTimeout: 12,
1713
- /** 静默扩展名:这些扩展名的直连成功日志用 debug 级别 */
1714
- silentExtensions: [".js"]
1715
- };
1716
1710
  var ARCHIVE_EXTENSIONS = [".7z", ".zip", ".rar", ".gz", ".bz2", ".tar", ".zst"];
1717
1711
  var EXECUTABLE_EXTENSIONS = [".exe", ".apk", ".bin", ".dmg", ".jar", ".class"];
1718
1712
  var DOCUMENT_EXTENSIONS = [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".csv"];
@@ -1738,7 +1732,14 @@ var IMAGE_EXTENSIONS = [
1738
1732
  var MEDIA_EXTENSIONS = [".mp3", ".mp4", ".avi", ".mkv", ".webm", ".midi", ".mid", ".ogg", ".flac", ".swf"];
1739
1733
  var FONT_EXTENSIONS = [".woff", ".woff2", ".ttf", ".otf"];
1740
1734
  var CSS_EXTENSIONS = [".css"];
1735
+ var JS_EXTENSIONS = [".js"];
1741
1736
  var OTHER_EXTENSIONS = [".ps", ".iso"];
1737
+ var DirectConfig = {
1738
+ /** 直连请求超时时间(秒) */
1739
+ directTimeout: 12,
1740
+ /** 静默扩展名:这些扩展名的直连成功日志用 debug 级别 */
1741
+ silentExtensions: JS_EXTENSIONS
1742
+ };
1742
1743
  var DEFAULT_BLOCKING_CONFIG = {
1743
1744
  /** 屏蔽压缩包 */
1744
1745
  blockArchive: true,
@@ -1755,9 +1756,29 @@ var DEFAULT_BLOCKING_CONFIG = {
1755
1756
  /** 屏蔽 CSS (注意:可能影响页面视觉效果) */
1756
1757
  blockCss: false,
1757
1758
  /** 屏蔽其他资源 */
1758
- blockOther: true,
1759
- /** 额外自定义扩展名列表 */
1760
- customExtensions: []
1759
+ blockOther: true
1760
+ };
1761
+ var DEFAULT_DIRECT_CONFIG = {
1762
+ /** 直连域名 */
1763
+ domains: [],
1764
+ /** 直连压缩包 */
1765
+ directAllArchive: false,
1766
+ /** 直连可执行文件 */
1767
+ directAllExecutable: false,
1768
+ /** 直连办公文档 */
1769
+ directAllDocument: false,
1770
+ /** 直连图片 */
1771
+ directAllImage: false,
1772
+ /** 直连音视频 */
1773
+ directAllMedia: false,
1774
+ /** 直连字体 */
1775
+ directAllFont: false,
1776
+ /** 直连 CSS */
1777
+ directAllCss: false,
1778
+ /** 直连 JS */
1779
+ directAllJs: false,
1780
+ /** 直连其他资源 */
1781
+ directAllOther: false
1761
1782
  };
1762
1783
  var SHARED_GOT_OPTIONS = {
1763
1784
  http2: false,
@@ -1785,9 +1806,6 @@ var Interception = {
1785
1806
  if (mergedConfig.blockFont) extensions.push(...FONT_EXTENSIONS);
1786
1807
  if (mergedConfig.blockCss) extensions.push(...CSS_EXTENSIONS);
1787
1808
  if (mergedConfig.blockOther) extensions.push(...OTHER_EXTENSIONS);
1788
- if (mergedConfig.customExtensions?.length > 0) {
1789
- extensions.push(...mergedConfig.customExtensions);
1790
- }
1791
1809
  return [...new Set(extensions)];
1792
1810
  },
1793
1811
  /**
@@ -1807,12 +1825,34 @@ var Interception = {
1807
1825
  other: { name: "\u5176\u4ED6\u8D44\u6E90", extensions: OTHER_EXTENSIONS }
1808
1826
  };
1809
1827
  },
1828
+ /**
1829
+ * 根据配置生成需要直连的扩展名列表
1830
+ *
1831
+ * @param {Object} [config] - 直连扩展名配置
1832
+ * @returns {string[]} 需要直连的扩展名列表
1833
+ */
1834
+ getDirectExtensions(config = {}) {
1835
+ const mergedConfig = { ...DEFAULT_DIRECT_CONFIG, ...config };
1836
+ const extensions = [];
1837
+ if (mergedConfig.directAllArchive) extensions.push(...ARCHIVE_EXTENSIONS);
1838
+ if (mergedConfig.directAllExecutable) extensions.push(...EXECUTABLE_EXTENSIONS);
1839
+ if (mergedConfig.directAllDocument) extensions.push(...DOCUMENT_EXTENSIONS);
1840
+ if (mergedConfig.directAllImage) extensions.push(...IMAGE_EXTENSIONS);
1841
+ if (mergedConfig.directAllMedia) extensions.push(...MEDIA_EXTENSIONS);
1842
+ if (mergedConfig.directAllFont) extensions.push(...FONT_EXTENSIONS);
1843
+ if (mergedConfig.directAllCss) extensions.push(...CSS_EXTENSIONS);
1844
+ if (mergedConfig.directAllJs) extensions.push(...JS_EXTENSIONS);
1845
+ if (mergedConfig.directAllOther) extensions.push(...OTHER_EXTENSIONS);
1846
+ return [...new Set(extensions)];
1847
+ },
1810
1848
  /**
1811
1849
  * 设置网络拦截规则(资源屏蔽 + CDN 直连)
1812
1850
  *
1813
1851
  * @param {import('playwright').Page} page - Playwright Page 对象
1814
1852
  * @param {Object} [options] - 配置选项
1815
- * @param {string[]} [options.directDomains] - 需要直连的域名列表
1853
+ * @param {string[]} [options.directDomains] - [已过时] 需要直连的域名列表,请改用 directConfig.domains
1854
+ * @param {string[]} [options.directExtensions] - 需要直连的扩展名(如 .js/.css)
1855
+ * @param {Object} [options.directConfig] - 直连配置(参考 blockingConfig),支持 domains/directAllXxx 系列开关
1816
1856
  * @param {Object} [options.blockingConfig] - 资源屏蔽配置
1817
1857
  * @param {boolean} [options.fallbackToProxy] - 直连失败时是否回退到代理(默认 true)
1818
1858
  * @returns {Promise<void>}
@@ -1820,12 +1860,27 @@ var Interception = {
1820
1860
  async setup(page, options = {}) {
1821
1861
  const {
1822
1862
  directDomains = [],
1863
+ directExtensions = [],
1864
+ directConfig = {},
1823
1865
  blockingConfig = {},
1824
1866
  fallbackToProxy = true
1825
1867
  } = options;
1826
1868
  const mergedBlockingConfig = { ...DEFAULT_BLOCKING_CONFIG, ...blockingConfig };
1869
+ const mergedDirectConfig = { ...DEFAULT_DIRECT_CONFIG, ...directConfig };
1827
1870
  const blockedExtensions = this.getBlockedExtensions(mergedBlockingConfig);
1828
- const hasDirectDomains = directDomains.length > 0;
1871
+ const configuredDirectExtensions = this.getDirectExtensions(mergedDirectConfig);
1872
+ const normalizedDirectDomains = Array.from(
1873
+ new Set(
1874
+ [...directDomains, ...mergedDirectConfig.domains || []].map((domain) => String(domain || "").toLowerCase().trim()).filter(Boolean)
1875
+ )
1876
+ );
1877
+ const hasDirectDomains = normalizedDirectDomains.length > 0;
1878
+ const normalizedDirectExtensions = Array.from(
1879
+ new Set(
1880
+ [...directExtensions, ...configuredDirectExtensions].map((ext) => String(ext || "").toLowerCase().trim()).filter(Boolean).map((ext) => ext.startsWith(".") ? ext : `.${ext}`)
1881
+ )
1882
+ );
1883
+ const hasDirectExtensions = normalizedDirectExtensions.length > 0;
1829
1884
  const enabledCategories = [];
1830
1885
  if (mergedBlockingConfig.blockArchive) enabledCategories.push("\u538B\u7F29\u5305");
1831
1886
  if (mergedBlockingConfig.blockExecutable) enabledCategories.push("\u53EF\u6267\u884C\u6587\u4EF6");
@@ -1835,7 +1890,10 @@ var Interception = {
1835
1890
  if (mergedBlockingConfig.blockFont) enabledCategories.push("\u5B57\u4F53");
1836
1891
  if (mergedBlockingConfig.blockCss) enabledCategories.push("CSS");
1837
1892
  if (mergedBlockingConfig.blockOther) enabledCategories.push("\u5176\u4ED6");
1838
- logger9.start("setup", hasDirectDomains ? `\u76F4\u8FDE\u57DF\u540D: [${directDomains.length} \u4E2A] | \u5C4F\u853D: [${enabledCategories.join(", ")}]` : `\u4EC5\u8D44\u6E90\u5C4F\u853D\u6A21\u5F0F | \u5C4F\u853D: [${enabledCategories.join(", ")}]`);
1893
+ const directRules = [];
1894
+ if (hasDirectDomains) directRules.push(`\u76F4\u8FDE\u57DF\u540D: [${normalizedDirectDomains.length} \u4E2A]`);
1895
+ if (hasDirectExtensions) directRules.push(`\u76F4\u8FDE\u6269\u5C55\u540D: [${normalizedDirectExtensions.join(", ")}]`);
1896
+ logger9.start("setup", directRules.length > 0 ? `${directRules.join(" | ")} | \u5C4F\u853D: [${enabledCategories.join(", ")}]` : `\u4EC5\u8D44\u6E90\u5C4F\u853D\u6A21\u5F0F | \u5C4F\u853D: [${enabledCategories.join(", ")}]`);
1839
1897
  await page.route("**/*", async (route) => {
1840
1898
  let handled = false;
1841
1899
  try {
@@ -1850,14 +1908,16 @@ var Interception = {
1850
1908
  handled = true;
1851
1909
  return;
1852
1910
  }
1853
- let isDirect = false;
1911
+ let isDirectByDomain = false;
1854
1912
  if (hasDirectDomains) {
1855
1913
  try {
1856
1914
  const hostname = new URL(url).hostname;
1857
- isDirect = directDomains.some((domain) => hostname.startsWith(domain));
1915
+ isDirectByDomain = normalizedDirectDomains.some((domain) => hostname.startsWith(domain));
1858
1916
  } catch (e) {
1859
1917
  }
1860
1918
  }
1919
+ const isDirectByExtension = hasDirectExtensions ? normalizedDirectExtensions.some((ext) => urlPath.endsWith(ext)) : false;
1920
+ const isDirect = isDirectByDomain || isDirectByExtension;
1861
1921
  if (isDirect) {
1862
1922
  try {
1863
1923
  const reqHeaders = await request.allHeaders();