customer-chat-sdk 1.0.38 → 1.0.40

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.
@@ -2472,12 +2472,24 @@ async function domToDataUrl(node, options) {
2472
2472
  return dataUrl;
2473
2473
  }
2474
2474
 
2475
+ async function domToJpeg(node, options) {
2476
+ return domToDataUrl(
2477
+ await orCreateContext(node, { ...options, type: "image/jpeg" })
2478
+ );
2479
+ }
2480
+
2475
2481
  async function domToPng(node, options) {
2476
2482
  return domToDataUrl(
2477
2483
  await orCreateContext(node, { ...options, type: "image/png" })
2478
2484
  );
2479
2485
  }
2480
2486
 
2487
+ async function domToWebp(node, options) {
2488
+ return domToDataUrl(
2489
+ await orCreateContext(node, { ...options, type: "image/webp" })
2490
+ );
2491
+ }
2492
+
2481
2493
  /*
2482
2494
  * snapdom
2483
2495
  * v.1.9.14
@@ -14311,6 +14323,8 @@ class ScreenshotManager {
14311
14323
  this.screenshotTimer = null;
14312
14324
  // modern-screenshot Worker 上下文(用于复用,避免频繁创建和销毁)
14313
14325
  this.screenshotContext = null;
14326
+ // 截图锁,防止并发截图
14327
+ this.isScreenshotInProgress = false;
14314
14328
  // PostMessage 监听器
14315
14329
  this.messageHandler = null;
14316
14330
  // 动态轮询间隔(由 iframe 消息控制)
@@ -14336,7 +14350,7 @@ class ScreenshotManager {
14336
14350
  this.globalRejectionHandler = null;
14337
14351
  this.targetElement = targetElement;
14338
14352
  this.options = {
14339
- interval: options.interval ?? 5000,
14353
+ interval: options.interval ?? 1000,
14340
14354
  quality: options.quality ?? 0.3, // 降低默认质量:0.4 -> 0.3,减少 base64 大小
14341
14355
  scale: options.scale ?? 1,
14342
14356
  maxHistory: options.maxHistory ?? 10,
@@ -14945,16 +14959,22 @@ class ScreenshotManager {
14945
14959
  console.log('📸 使用 html2canvas 引擎截图...');
14946
14960
  }
14947
14961
  try {
14962
+ // 检测 iOS 设备
14963
+ const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
14948
14964
  // html2canvas 需要确保样式完全加载,额外等待
14949
- // 等待所有样式表加载完成
14965
+ // iOS 设备需要更长的等待时间,因为样式表加载和处理方式不同
14950
14966
  await this.waitForAllStylesLoaded();
14951
14967
  // 等待字体加载完成
14952
14968
  await this.waitForFonts();
14969
+ // iOS 设备需要额外的等待时间,确保样式完全应用
14970
+ if (isIOS) {
14971
+ await new Promise(resolve => setTimeout(resolve, 300)); // iOS 额外等待 300ms
14972
+ }
14953
14973
  // 等待 DOM 完全渲染
14954
14974
  await new Promise(resolve => {
14955
14975
  requestAnimationFrame(() => {
14956
14976
  requestAnimationFrame(() => {
14957
- setTimeout(() => resolve(), 100);
14977
+ setTimeout(() => resolve(), isIOS ? 200 : 100); // iOS 使用更长的等待时间
14958
14978
  });
14959
14979
  });
14960
14980
  });
@@ -14990,9 +15010,11 @@ class ScreenshotManager {
14990
15010
  // width: finalWidth, // ❌ 移除,会导致宽度不正确
14991
15011
  // height: finalHeight, // ❌ 移除,会导致高度不正确
14992
15012
  // 关键配置:确保样式正确渲染
14993
- // 注意:不要设置 foreignObjectRendering,让 html2canvas 自动选择最佳渲染方式
14994
- // foreignObjectRendering 可能导致样式问题,所以不设置它
14995
- onclone: (clonedDoc, _clonedElement) => {
15013
+ // iOS 特定配置:启用 foreignObjectRendering 以确保样式正确渲染
15014
+ // iOS Safari 和 Chrome 对样式表的处理方式不同,需要启用此选项
15015
+ onclone: (clonedDoc, clonedElement) => {
15016
+ // 检测 iOS 设备(需要在 onclone 内部检测,因为这是回调函数)
15017
+ const isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
14996
15018
  // 在克隆的文档中,确保所有样式都正确应用
14997
15019
  // html2canvas 会自动处理样式,但我们需要确保样式表被正确复制
14998
15020
  // 确保 clonedDoc.head 存在
@@ -15081,10 +15103,53 @@ class ScreenshotManager {
15081
15103
  catch (e) {
15082
15104
  // 忽略错误,继续执行
15083
15105
  }
15106
+ // 4. iOS 特定处理:将计算后的样式内联化(确保样式正确应用)
15107
+ // iOS Safari/Chrome 对样式表的处理方式不同,需要将计算后的样式内联化
15108
+ if (isIOS) {
15109
+ try {
15110
+ // 遍历克隆文档中的所有元素,将计算后的样式内联化
15111
+ const allElements = clonedElement.querySelectorAll('*');
15112
+ allElements.forEach((el) => {
15113
+ try {
15114
+ const htmlEl = el;
15115
+ const computedStyle = window.getComputedStyle(htmlEl);
15116
+ // 获取关键样式属性并内联化
15117
+ const importantStyles = [];
15118
+ // 获取所有样式属性(iOS 需要更完整的样式)
15119
+ for (let i = 0; i < computedStyle.length; i++) {
15120
+ const prop = computedStyle[i];
15121
+ const value = computedStyle.getPropertyValue(prop);
15122
+ const priority = computedStyle.getPropertyPriority(prop);
15123
+ // 只内联化非默认值的重要样式
15124
+ if (value && value !== 'none' && value !== 'auto' && value !== 'normal') {
15125
+ importantStyles.push(`${prop}: ${value}${priority === 'important' ? ' !important' : ''}`);
15126
+ }
15127
+ }
15128
+ // 如果有关键样式,添加到内联样式
15129
+ if (importantStyles.length > 0) {
15130
+ const currentStyle = htmlEl.getAttribute('style') || '';
15131
+ const newStyle = currentStyle
15132
+ ? `${currentStyle}; ${importantStyles.join('; ')}`
15133
+ : importantStyles.join('; ');
15134
+ htmlEl.setAttribute('style', newStyle);
15135
+ }
15136
+ }
15137
+ catch (e) {
15138
+ // 忽略单个元素的错误
15139
+ }
15140
+ });
15141
+ }
15142
+ catch (e) {
15143
+ // 忽略内联化错误
15144
+ if (!this.options.silentMode) {
15145
+ console.warn('📸 iOS 样式内联化失败:', e);
15146
+ }
15147
+ }
15148
+ }
15084
15149
  if (!this.options.silentMode) {
15085
15150
  const styleLinks = clonedDoc.querySelectorAll('link[rel="stylesheet"]').length;
15086
15151
  const styleTags = clonedDoc.querySelectorAll('style').length;
15087
- console.log(`📸 onclone: 已复制 ${styleLinks} 个样式表链接和 ${styleTags} 个内联样式标签`);
15152
+ console.log(`📸 onclone: 已复制 ${styleLinks} 个样式表链接和 ${styleTags} 个内联样式标签${isIOS ? ' (iOS 模式:已内联化计算样式)' : ''}`);
15088
15153
  }
15089
15154
  },
15090
15155
  // 性能优化
@@ -15210,387 +15275,413 @@ class ScreenshotManager {
15210
15275
  * - 页面资源较少
15211
15276
  */
15212
15277
  async takeScreenshotWithModernScreenshot(element) {
15278
+ // 检查是否有截图正在进行(防止并发冲突)
15279
+ if (this.isScreenshotInProgress) {
15280
+ throw new Error('截图正在进行中,请稍后再试');
15281
+ }
15282
+ this.isScreenshotInProgress = true;
15213
15283
  if (!this.options.silentMode) {
15214
15284
  console.log('📸 使用 modern-screenshot 引擎截图(Worker 模式)...');
15215
15285
  }
15216
- // 获取元素的实际尺寸(使用 scrollWidth/scrollHeight 获取完整内容尺寸)
15217
- // 对于 document.body,需要特殊处理,确保截取完整页面内容
15218
- let elementWidth;
15219
- let elementHeight;
15220
- if (element === document.body || element === document.documentElement) {
15221
- // 对于 body 或 html 元素,使用页面的完整尺寸(包括滚动内容)
15222
- elementWidth = Math.max(element.scrollWidth, element.offsetWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth, window.innerWidth);
15223
- elementHeight = Math.max(element.scrollHeight, element.offsetHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight, window.innerHeight);
15224
- }
15225
- else {
15226
- // 对于其他元素,使用元素的完整尺寸
15227
- elementWidth = element.scrollWidth || element.clientWidth || element.offsetWidth;
15228
- elementHeight = element.scrollHeight || element.clientHeight || element.offsetHeight;
15229
- }
15230
- if (!this.options.silentMode) {
15231
- console.log(`📸 目标元素: ${element.tagName}${element.id ? '#' + element.id : ''}${element.className ? '.' + element.className.split(' ').join('.') : ''}`);
15232
- console.log(`📸 元素尺寸: ${elementWidth}x${elementHeight}`);
15233
- console.log(`📸 scrollWidth: ${element.scrollWidth}, scrollHeight: ${element.scrollHeight}`);
15234
- console.log(`📸 clientWidth: ${element.clientWidth}, clientHeight: ${element.clientHeight}`);
15235
- console.log(`📸 offsetWidth: ${element.offsetWidth}, offsetHeight: ${element.offsetHeight}`);
15286
+ try {
15287
+ // 获取元素的实际尺寸(使用 scrollWidth/scrollHeight 获取完整内容尺寸)
15288
+ // 对于 document.body,需要特殊处理,确保截取完整页面内容
15289
+ let elementWidth;
15290
+ let elementHeight;
15236
15291
  if (element === document.body || element === document.documentElement) {
15237
- console.log(`📸 页面完整尺寸: ${document.documentElement.scrollWidth}x${document.documentElement.scrollHeight}`);
15238
- console.log(`📸 窗口尺寸: ${window.innerWidth}x${window.innerHeight}`);
15239
- }
15240
- }
15241
- const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
15242
- const isLowEndDevice = navigator.hardwareConcurrency && navigator.hardwareConcurrency <= 4;
15243
- // 进一步降低质量以减少 base64 大小
15244
- // 桌面设备:使用配置的质量(默认 0.3)
15245
- // 移动设备/低端设备:进一步降低到 0.2(最低)
15246
- const finalQuality = isMobile || isLowEndDevice
15247
- ? Math.max(this.options.quality * 0.65, 0.2) // 移动设备:质量 * 0.65,最低 0.2
15248
- : this.options.quality; // 桌面设备:使用配置的质量(默认 0.3)
15249
- // 计算压缩后的尺寸(对所有元素都应用,包括 document.body)
15250
- // 这样可以避免生成过大的截图,减少 base64 大小
15251
- const { width, height } = this.calculateCompressedSize(elementWidth, elementHeight, this.options.maxWidth, this.options.maxHeight);
15252
- // 对于所有元素都应用尺寸限制(包括 body),避免截图过大
15253
- // 如果计算后的尺寸小于元素实际尺寸,使用压缩尺寸;否则使用元素实际尺寸(但不超过最大值)
15254
- const finalWidth = width < elementWidth ? width : Math.min(elementWidth, this.options.maxWidth);
15255
- const finalHeight = height < elementHeight ? height : Math.min(elementHeight, this.options.maxHeight);
15256
- // 处理跨域图片的函数
15257
- const handleCrossOriginImage = async (url) => {
15258
- // 如果是 data URL 或 blob URL,直接返回
15259
- if (url.startsWith('data:') || url.startsWith('blob:')) {
15260
- return url;
15292
+ // 对于 body 或 html 元素,使用页面的完整尺寸(包括滚动内容)
15293
+ elementWidth = Math.max(element.scrollWidth, element.offsetWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth, window.innerWidth);
15294
+ elementHeight = Math.max(element.scrollHeight, element.offsetHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight, window.innerHeight);
15261
15295
  }
15262
- // 如果是同源图片,直接返回
15263
- try {
15264
- const imgUrl = new URL(url, window.location.href);
15265
- if (imgUrl.origin === window.location.origin) {
15266
- return url;
15267
- }
15296
+ else {
15297
+ // 对于其他元素,使用元素的完整尺寸
15298
+ elementWidth = element.scrollWidth || element.clientWidth || element.offsetWidth;
15299
+ elementHeight = element.scrollHeight || element.clientHeight || element.offsetHeight;
15268
15300
  }
15269
- catch (e) {
15270
- // URL 解析失败,继续处理
15301
+ if (!this.options.silentMode) {
15302
+ console.log(`📸 目标元素: ${element.tagName}${element.id ? '#' + element.id : ''}${element.className ? '.' + element.className.split(' ').join('.') : ''}`);
15303
+ console.log(`📸 元素尺寸: ${elementWidth}x${elementHeight}`);
15304
+ console.log(`📸 scrollWidth: ${element.scrollWidth}, scrollHeight: ${element.scrollHeight}`);
15305
+ console.log(`📸 clientWidth: ${element.clientWidth}, clientHeight: ${element.clientHeight}`);
15306
+ console.log(`📸 offsetWidth: ${element.offsetWidth}, offsetHeight: ${element.offsetHeight}`);
15307
+ if (element === document.body || element === document.documentElement) {
15308
+ console.log(`📸 页面完整尺寸: ${document.documentElement.scrollWidth}x${document.documentElement.scrollHeight}`);
15309
+ console.log(`📸 窗口尺寸: ${window.innerWidth}x${window.innerHeight}`);
15310
+ }
15271
15311
  }
15272
- // 如果配置了代理服务器,使用代理处理跨域图片
15273
- // 只有当 useProxy true proxyUrl 存在时才使用代理
15274
- const shouldUseProxy = this.options.useProxy && this.options.proxyUrl && this.options.proxyUrl.trim() !== '';
15275
- if (shouldUseProxy) {
15276
- // 检查内存缓存(优先使用缓存,带过期时间检查)
15277
- const cachedDataUrl = this.getCachedImage(url);
15278
- if (cachedDataUrl) {
15279
- if (!this.options.silentMode) {
15280
- console.log(`📸 使用内存缓存图片: ${url.substring(0, 50)}...`);
15312
+ const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
15313
+ const isLowEndDevice = navigator.hardwareConcurrency && navigator.hardwareConcurrency <= 4;
15314
+ // 进一步降低质量以减少 base64 大小
15315
+ // 桌面设备:使用配置的质量(默认 0.3)
15316
+ // 移动设备/低端设备:进一步降低到 0.2(最低)
15317
+ const finalQuality = isMobile || isLowEndDevice
15318
+ ? Math.max(this.options.quality * 0.65, 0.2) // 移动设备:质量 * 0.65,最低 0.2
15319
+ : this.options.quality; // 桌面设备:使用配置的质量(默认 0.3)
15320
+ // 计算压缩后的尺寸(对所有元素都应用,包括 document.body)
15321
+ // 这样可以避免生成过大的截图,减少 base64 大小
15322
+ const { width, height } = this.calculateCompressedSize(elementWidth, elementHeight, this.options.maxWidth, this.options.maxHeight);
15323
+ // 对于所有元素都应用尺寸限制(包括 body),避免截图过大
15324
+ // 如果计算后的尺寸小于元素实际尺寸,使用压缩尺寸;否则使用元素实际尺寸(但不超过最大值)
15325
+ const finalWidth = width < elementWidth ? width : Math.min(elementWidth, this.options.maxWidth);
15326
+ const finalHeight = height < elementHeight ? height : Math.min(elementHeight, this.options.maxHeight);
15327
+ // 处理跨域图片的函数
15328
+ const handleCrossOriginImage = async (url) => {
15329
+ // 如果是 data URL 或 blob URL,直接返回
15330
+ if (url.startsWith('data:') || url.startsWith('blob:')) {
15331
+ return url;
15332
+ }
15333
+ // 如果是同源图片,直接返回
15334
+ try {
15335
+ const imgUrl = new URL(url, window.location.href);
15336
+ if (imgUrl.origin === window.location.origin) {
15337
+ return url;
15281
15338
  }
15282
- return cachedDataUrl;
15283
15339
  }
15284
- // 检查 IndexedDB 缓存(如果启用)
15285
- if (this.options.useIndexedDB) {
15286
- const indexedDBCache = await this.getIndexedDBCache(url);
15287
- if (indexedDBCache) {
15288
- // 同步到内存缓存
15289
- this.setCachedImage(url, indexedDBCache);
15340
+ catch (e) {
15341
+ // URL 解析失败,继续处理
15342
+ }
15343
+ // 如果配置了代理服务器,使用代理处理跨域图片
15344
+ // 只有当 useProxy 为 true 且 proxyUrl 存在时才使用代理
15345
+ const shouldUseProxy = this.options.useProxy && this.options.proxyUrl && this.options.proxyUrl.trim() !== '';
15346
+ if (shouldUseProxy) {
15347
+ // 检查内存缓存(优先使用缓存,带过期时间检查)
15348
+ const cachedDataUrl = this.getCachedImage(url);
15349
+ if (cachedDataUrl) {
15290
15350
  if (!this.options.silentMode) {
15291
- console.log(`📸 ✅ 使用 IndexedDB 缓存图片: ${url.substring(0, 50)}...`);
15351
+ console.log(`📸 ✅ 使用内存缓存图片: ${url.substring(0, 50)}...`);
15292
15352
  }
15293
- return indexedDBCache;
15353
+ return cachedDataUrl;
15294
15354
  }
15295
- }
15296
- try {
15297
- // 构建代理请求参数
15298
- const params = new URLSearchParams({
15299
- url: url,
15300
- maxWidth: String(this.options.maxWidth || 1600),
15301
- maxHeight: String(this.options.maxHeight || 900),
15302
- quality: String(Math.round((this.options.quality || 0.4) * 100)),
15303
- format: this.options.outputFormat || 'webp'
15304
- });
15305
- let baseUrl = this.options.proxyUrl;
15306
- baseUrl = baseUrl.replace(/[?&]$/, '');
15307
- const proxyUrl = `${baseUrl}?${params.toString()}`;
15308
- // 请求代理服务器(优化:添加超时控制和优先级)
15309
- const controller = new AbortController();
15310
- const timeoutId = setTimeout(() => controller.abort(), this.options.imageLoadTimeout);
15311
- try {
15312
- const fetchOptions = {
15313
- method: 'GET',
15314
- mode: 'cors',
15315
- credentials: 'omit',
15316
- headers: {
15317
- 'Accept': 'image/*'
15318
- },
15319
- cache: 'no-cache',
15320
- signal: controller.signal
15321
- };
15322
- // 添加 fetch priority(如果支持)
15323
- if ('priority' in fetchOptions) {
15324
- fetchOptions.priority = this.options.fetchPriority;
15355
+ // 检查 IndexedDB 缓存(如果启用)
15356
+ if (this.options.useIndexedDB) {
15357
+ const indexedDBCache = await this.getIndexedDBCache(url);
15358
+ if (indexedDBCache) {
15359
+ // 同步到内存缓存
15360
+ this.setCachedImage(url, indexedDBCache);
15361
+ if (!this.options.silentMode) {
15362
+ console.log(`📸 使用 IndexedDB 缓存图片: ${url.substring(0, 50)}...`);
15363
+ }
15364
+ return indexedDBCache;
15325
15365
  }
15326
- const response = await fetch(proxyUrl, fetchOptions);
15327
- clearTimeout(timeoutId);
15328
- if (!response.ok) {
15329
- throw new Error(`代理请求失败: ${response.status}`);
15366
+ }
15367
+ try {
15368
+ // 构建代理请求参数
15369
+ const params = new URLSearchParams({
15370
+ url: url,
15371
+ maxWidth: String(this.options.maxWidth || 1600),
15372
+ maxHeight: String(this.options.maxHeight || 900),
15373
+ quality: String(Math.round((this.options.quality || 0.4) * 100)),
15374
+ format: this.options.outputFormat || 'webp'
15375
+ });
15376
+ let baseUrl = this.options.proxyUrl;
15377
+ baseUrl = baseUrl.replace(/[?&]$/, '');
15378
+ const proxyUrl = `${baseUrl}?${params.toString()}`;
15379
+ // 请求代理服务器(优化:添加超时控制和优先级)
15380
+ const controller = new AbortController();
15381
+ const timeoutId = setTimeout(() => controller.abort(), this.options.imageLoadTimeout);
15382
+ try {
15383
+ const fetchOptions = {
15384
+ method: 'GET',
15385
+ mode: 'cors',
15386
+ credentials: 'omit',
15387
+ headers: {
15388
+ 'Accept': 'image/*'
15389
+ },
15390
+ cache: 'no-cache',
15391
+ signal: controller.signal
15392
+ };
15393
+ // 添加 fetch priority(如果支持)
15394
+ if ('priority' in fetchOptions) {
15395
+ fetchOptions.priority = this.options.fetchPriority;
15396
+ }
15397
+ const response = await fetch(proxyUrl, fetchOptions);
15398
+ clearTimeout(timeoutId);
15399
+ if (!response.ok) {
15400
+ throw new Error(`代理请求失败: ${response.status}`);
15401
+ }
15402
+ const blob = await response.blob();
15403
+ const dataUrl = await this.blobToDataUrl(blob);
15404
+ // 缓存结果(带时间戳,10分钟有效)
15405
+ this.setCachedImage(url, dataUrl);
15406
+ // 如果启用 IndexedDB,也保存到 IndexedDB
15407
+ if (this.options.useIndexedDB) {
15408
+ await this.setIndexedDBCache(url, dataUrl);
15409
+ }
15410
+ return dataUrl;
15330
15411
  }
15331
- const blob = await response.blob();
15332
- const dataUrl = await this.blobToDataUrl(blob);
15333
- // 缓存结果(带时间戳,10分钟有效)
15334
- this.setCachedImage(url, dataUrl);
15335
- // 如果启用 IndexedDB,也保存到 IndexedDB
15336
- if (this.options.useIndexedDB) {
15337
- await this.setIndexedDBCache(url, dataUrl);
15412
+ catch (fetchError) {
15413
+ clearTimeout(timeoutId);
15414
+ throw fetchError;
15338
15415
  }
15339
- return dataUrl;
15340
15416
  }
15341
- catch (fetchError) {
15342
- clearTimeout(timeoutId);
15343
- throw fetchError;
15417
+ catch (error) {
15418
+ if (!this.options.silentMode) {
15419
+ console.warn(`📸 代理处理图片失败: ${url.substring(0, 100)}...`, error);
15420
+ }
15421
+ // 失败时返回原 URL
15422
+ return url;
15344
15423
  }
15345
15424
  }
15346
- catch (error) {
15347
- if (!this.options.silentMode) {
15348
- console.warn(`📸 代理处理图片失败: ${url.substring(0, 100)}...`, error);
15425
+ // 如果没有配置代理,需要添加内存保护机制
15426
+ // 不使用代理时,modern-screenshot 会直接下载图片,可能导致内存问题
15427
+ // 由于已配置 CORS,可以直接下载并检查大小
15428
+ if (this.options.enableCORS) {
15429
+ // 对于不使用代理的情况,添加内存保护和缓存机制:
15430
+ // 1. 先检查内存缓存(避免重复下载)
15431
+ // 2. 检查 IndexedDB 缓存
15432
+ // 3. 使用下载队列避免并发重复下载
15433
+ // 4. 下载时检查大小,如果过大则使用占位符
15434
+ // 先检查内存缓存(优先使用缓存,避免重复下载)
15435
+ const cachedDataUrl = this.getCachedImage(url);
15436
+ if (cachedDataUrl) {
15437
+ if (!this.options.silentMode) {
15438
+ console.log(`📸 ✅ 使用内存缓存图片(无代理模式): ${url.substring(0, 50)}...`);
15439
+ }
15440
+ return cachedDataUrl;
15349
15441
  }
15350
- // 失败时返回原 URL
15351
- return url;
15352
- }
15353
- }
15354
- // 如果没有配置代理,需要添加内存保护机制
15355
- // 不使用代理时,modern-screenshot 会直接下载图片,可能导致内存问题
15356
- // 由于已配置 CORS,可以直接下载并检查大小
15357
- if (this.options.enableCORS) {
15358
- // 对于不使用代理的情况,添加内存保护和缓存机制:
15359
- // 1. 先检查内存缓存(避免重复下载)
15360
- // 2. 检查 IndexedDB 缓存
15361
- // 3. 使用下载队列避免并发重复下载
15362
- // 4. 下载时检查大小,如果过大则使用占位符
15363
- // 先检查内存缓存(优先使用缓存,避免重复下载)
15364
- const cachedDataUrl = this.getCachedImage(url);
15365
- if (cachedDataUrl) {
15366
- if (!this.options.silentMode) {
15367
- console.log(`📸 ✅ 使用内存缓存图片(无代理模式): ${url.substring(0, 50)}...`);
15442
+ // 检查 IndexedDB 缓存(如果启用)
15443
+ if (this.options.useIndexedDB) {
15444
+ const indexedDBCache = await this.getIndexedDBCache(url);
15445
+ if (indexedDBCache) {
15446
+ // 同步到内存缓存
15447
+ this.setCachedImage(url, indexedDBCache);
15448
+ if (!this.options.silentMode) {
15449
+ console.log(`📸 ✅ 使用 IndexedDB 缓存图片(无代理模式): ${url.substring(0, 50)}...`);
15450
+ }
15451
+ return indexedDBCache;
15452
+ }
15368
15453
  }
15369
- return cachedDataUrl;
15370
- }
15371
- // 检查 IndexedDB 缓存(如果启用)
15372
- if (this.options.useIndexedDB) {
15373
- const indexedDBCache = await this.getIndexedDBCache(url);
15374
- if (indexedDBCache) {
15375
- // 同步到内存缓存
15376
- this.setCachedImage(url, indexedDBCache);
15454
+ // 检查是否正在下载(避免重复下载)
15455
+ if (this.imageDownloadQueue.has(url)) {
15456
+ // 如果已经在下载队列中,等待现有下载完成
15377
15457
  if (!this.options.silentMode) {
15378
- console.log(`📸 使用 IndexedDB 缓存图片(无代理模式): ${url.substring(0, 50)}...`);
15458
+ console.log(`📸 等待图片下载完成: ${url.substring(0, 50)}...`);
15379
15459
  }
15380
- return indexedDBCache;
15460
+ return await this.imageDownloadQueue.get(url);
15381
15461
  }
15382
- }
15383
- // 检查是否正在下载(避免重复下载)
15384
- if (this.imageDownloadQueue.has(url)) {
15385
- // 如果已经在下载队列中,等待现有下载完成
15386
- if (!this.options.silentMode) {
15387
- console.log(`📸 ⏳ 等待图片下载完成: ${url.substring(0, 50)}...`);
15462
+ // 检查并发下载数限制
15463
+ if (this.activeDownloads.size >= this.maxConcurrentImageDownloads) {
15464
+ // 并发数已满,返回原 URL,让 modern-screenshot 自己处理(可能会失败,但不阻塞)
15465
+ if (!this.options.silentMode) {
15466
+ console.warn(`📸 ⚠️ 并发下载数已满(${this.activeDownloads.size}/${this.maxConcurrentImageDownloads}),跳过: ${url.substring(0, 50)}...`);
15467
+ }
15468
+ return url;
15388
15469
  }
15389
- return await this.imageDownloadQueue.get(url);
15390
- }
15391
- // 检查并发下载数限制
15392
- if (this.activeDownloads.size >= this.maxConcurrentImageDownloads) {
15393
- // 并发数已满,返回原 URL,让 modern-screenshot 自己处理(可能会失败,但不阻塞)
15394
- if (!this.options.silentMode) {
15395
- console.warn(`📸 ⚠️ 并发下载数已满(${this.activeDownloads.size}/${this.maxConcurrentImageDownloads}),跳过: ${url.substring(0, 50)}...`);
15470
+ // 创建下载 Promise 并加入队列
15471
+ const downloadPromise = this.downloadImageWithoutProxy(url);
15472
+ this.imageDownloadQueue.set(url, downloadPromise);
15473
+ this.activeDownloads.add(url);
15474
+ try {
15475
+ const result = await downloadPromise;
15476
+ return result;
15477
+ }
15478
+ finally {
15479
+ // 下载完成后清理
15480
+ this.imageDownloadQueue.delete(url);
15481
+ this.activeDownloads.delete(url);
15396
15482
  }
15397
- return url;
15398
15483
  }
15399
- // 创建下载 Promise 并加入队列
15400
- const downloadPromise = this.downloadImageWithoutProxy(url);
15401
- this.imageDownloadQueue.set(url, downloadPromise);
15402
- this.activeDownloads.add(url);
15484
+ // 默认返回原 URL
15485
+ return url;
15486
+ };
15487
+ // 检查元素是否可见且有尺寸
15488
+ const rect = element.getBoundingClientRect();
15489
+ if (rect.width === 0 || rect.height === 0) {
15490
+ throw new Error('元素尺寸为 0,无法截图');
15491
+ }
15492
+ // 每次截图都重新创建 context,确保使用最新的元素状态
15493
+ // 如果已有 context,先清理
15494
+ if (this.screenshotContext) {
15403
15495
  try {
15404
- const result = await downloadPromise;
15405
- return result;
15496
+ destroyContext(this.screenshotContext);
15406
15497
  }
15407
- finally {
15408
- // 下载完成后清理
15409
- this.imageDownloadQueue.delete(url);
15410
- this.activeDownloads.delete(url);
15498
+ catch (e) {
15499
+ // 忽略清理错误
15411
15500
  }
15412
- }
15413
- // 默认返回原 URL
15414
- return url;
15415
- };
15416
- // 检查元素是否可见且有尺寸
15417
- const rect = element.getBoundingClientRect();
15418
- if (rect.width === 0 || rect.height === 0) {
15419
- throw new Error('元素尺寸为 0,无法截图');
15420
- }
15421
- // 每次截图都重新创建 context,确保使用最新的元素状态
15422
- // 如果已有 context,先清理
15423
- if (this.screenshotContext) {
15424
- try {
15425
- destroyContext(this.screenshotContext);
15426
- }
15427
- catch (e) {
15428
- // 忽略清理错误
15429
- }
15430
- this.screenshotContext = null;
15431
- }
15432
- // Worker 数量配置:移动设备/低端设备使用 1 个 Worker,桌面设备使用 2 个
15433
- // workerNumber > 0 会启用 Worker 模式,截图处理在后台线程执行,不会阻塞主线程 UI
15434
- const workerNumber = isMobile || isLowEndDevice ? 1 : 2;
15435
- // 构建 createContext 配置
15436
- // 参考: https://github.com/qq15725/modern-screenshot/blob/main/src/options.ts
15437
- const contextOptions = {
15438
- workerNumber, // Worker 数量,> 0 启用 Worker 模式
15439
- quality: finalQuality, // 图片质量(0-1),已优化为更低的值以减少 base64 大小
15440
- fetchFn: handleCrossOriginImage, // 使用代理服务器处理跨域图片
15441
- fetch: {
15442
- requestInit: {
15443
- cache: 'no-cache',
15501
+ this.screenshotContext = null;
15502
+ }
15503
+ // Worker 数量配置:移动设备/低端设备使用 1 个 Worker,桌面设备使用 2 个
15504
+ // workerNumber > 0 会启用 Worker 模式,截图处理在后台线程执行,不会阻塞主线程 UI
15505
+ const workerNumber = isMobile || isLowEndDevice ? 1 : 2;
15506
+ // 构建 createContext 配置
15507
+ // 参考: https://github.com/qq15725/modern-screenshot/blob/main/src/options.ts
15508
+ const contextOptions = {
15509
+ workerNumber, // Worker 数量,> 0 启用 Worker 模式
15510
+ quality: finalQuality, // 图片质量(0-1),已优化为更低的值以减少 base64 大小
15511
+ fetchFn: handleCrossOriginImage, // 使用代理服务器处理跨域图片
15512
+ fetch: {
15513
+ requestInit: {
15514
+ cache: 'no-cache',
15515
+ },
15516
+ bypassingCache: true,
15444
15517
  },
15445
- bypassingCache: true,
15446
- },
15447
- // 设置最大 canvas 尺寸,防止生成过大的 canvas(避免内存问题)
15448
- // 参考: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas#maximum_canvas_size
15449
- // 大多数浏览器限制为 16,777,216 像素(4096x4096),这里设置为更保守的值
15450
- maximumCanvasSize: 16777216, // 16M 像素(约 4096x4096)
15451
- };
15452
- // 对所有元素都设置尺寸限制(包括 document.body),避免截图过大
15453
- // 这样可以减少 base64 大小,提高性能
15454
- if (finalWidth && finalHeight) {
15455
- contextOptions.width = finalWidth;
15456
- contextOptions.height = finalHeight;
15457
- if (!this.options.silentMode) {
15458
- if (element === document.body || element === document.documentElement) {
15459
- console.log(`📸 截取完整页面(document.body),使用压缩尺寸: ${finalWidth}x${finalHeight}`);
15518
+ // 设置最大 canvas 尺寸,防止生成过大的 canvas(避免内存问题)
15519
+ // 参考: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas#maximum_canvas_size
15520
+ // 大多数浏览器限制为 16,777,216 像素(4096x4096),这里设置为更保守的值
15521
+ maximumCanvasSize: 16777216, // 16M 像素(约 4096x4096)
15522
+ };
15523
+ // 对所有元素都设置尺寸限制(包括 document.body),避免截图过大
15524
+ // 这样可以减少 base64 大小,提高性能
15525
+ if (finalWidth && finalHeight) {
15526
+ contextOptions.width = finalWidth;
15527
+ contextOptions.height = finalHeight;
15528
+ if (!this.options.silentMode) {
15529
+ if (element === document.body || element === document.documentElement) {
15530
+ console.log(`📸 截取完整页面(document.body),使用压缩尺寸: ${finalWidth}x${finalHeight}`);
15531
+ }
15532
+ else {
15533
+ console.log(`📸 使用压缩尺寸: ${finalWidth}x${finalHeight}`);
15534
+ }
15460
15535
  }
15461
- else {
15462
- console.log(`📸 使用压缩尺寸: ${finalWidth}x${finalHeight}`);
15536
+ }
15537
+ else {
15538
+ if (!this.options.silentMode) {
15539
+ console.log(`📸 使用元素实际尺寸: ${elementWidth}x${elementHeight}`);
15463
15540
  }
15464
15541
  }
15465
- }
15466
- else {
15542
+ // 缩放配置:移动设备使用更低的缩放比例,进一步减少图片大小
15543
+ // scale < 1 会降低图片分辨率,减少 base64 大小
15544
+ if (this.options.scale !== 1) {
15545
+ contextOptions.scale = isMobile ? 0.7 : this.options.scale; // 移动设备:0.8 -> 0.7
15546
+ }
15547
+ else if (isMobile) {
15548
+ // 如果未指定 scale,移动设备默认使用 0.7
15549
+ contextOptions.scale = 0.7;
15550
+ }
15551
+ // modern-screenshot 会自动处理 worker URL,不需要手动设置 workerUrl
15552
+ // 当 workerNumber > 0 时,截图处理会在 Worker 线程中执行,不会阻塞主线程 UI
15553
+ // 创建 Worker 上下文(每次截图都创建新的,确保元素状态最新)
15467
15554
  if (!this.options.silentMode) {
15468
- console.log(`📸 使用元素实际尺寸: ${elementWidth}x${elementHeight}`);
15555
+ console.log(`📸 Worker 模式: ${workerNumber} 个 Worker,质量: ${finalQuality.toFixed(2)},缩放: ${contextOptions.scale || 1}`);
15469
15556
  }
15470
- }
15471
- // 缩放配置:移动设备使用更低的缩放比例,进一步减少图片大小
15472
- // scale < 1 会降低图片分辨率,减少 base64 大小
15473
- if (this.options.scale !== 1) {
15474
- contextOptions.scale = isMobile ? 0.7 : this.options.scale; // 移动设备:0.8 -> 0.7
15475
- }
15476
- else if (isMobile) {
15477
- // 如果未指定 scale,移动设备默认使用 0.7
15478
- contextOptions.scale = 0.7;
15479
- }
15480
- // modern-screenshot 会自动处理 worker URL,不需要手动设置 workerUrl
15481
- // workerNumber > 0 时,截图处理会在 Worker 线程中执行,不会阻塞主线程 UI
15482
- // 创建 Worker 上下文(每次截图都创建新的,确保元素状态最新)
15483
- if (!this.options.silentMode) {
15484
- console.log(`📸 Worker 模式: ${workerNumber} 个 Worker,质量: ${finalQuality.toFixed(2)},缩放: ${contextOptions.scale || 1}`);
15485
- }
15486
- this.screenshotContext = await createContext$1(element, contextOptions);
15487
- try {
15488
- // 使用 Worker 上下文进行截图
15489
- const dataUrl = await domToPng(this.screenshotContext);
15490
- // 验证截图结果
15491
- if (!dataUrl || dataUrl.length < 100) {
15492
- throw new Error('生成的截图数据无效或过短');
15557
+ // 添加重试机制
15558
+ let retries = 0;
15559
+ const maxRetries = this.options.maxRetries || 2;
15560
+ let screenshotContext = null;
15561
+ while (retries <= maxRetries) {
15562
+ try {
15563
+ screenshotContext = await createContext$1(element, contextOptions);
15564
+ this.screenshotContext = screenshotContext;
15565
+ break;
15566
+ }
15567
+ catch (error) {
15568
+ if (retries === maxRetries) {
15569
+ throw new Error(`创建截图上下文失败(已重试 ${maxRetries} 次): ${error instanceof Error ? error.message : String(error)}`);
15570
+ }
15571
+ retries++;
15572
+ const delay = 1000 * retries; // 递增延迟:1秒、2秒...
15573
+ if (!this.options.silentMode) {
15574
+ console.warn(`📸 ⚠️ 创建截图上下文失败,${delay}ms 后重试 (${retries}/${maxRetries})...`);
15575
+ }
15576
+ await new Promise(resolve => setTimeout(resolve, delay));
15577
+ }
15493
15578
  }
15494
- // 根据输出格式转换
15495
- if (this.options.outputFormat !== 'png') {
15496
- // modern-screenshot 默认输出 PNG,如果需要其他格式,需要转换
15497
- const canvas = document.createElement('canvas');
15498
- const ctx = canvas.getContext('2d');
15499
- if (!ctx) {
15500
- throw new Error('无法获取 canvas context');
15579
+ try {
15580
+ // 根据输出格式选择对应的 API,避免格式转换(性能优化)
15581
+ // 添加超时机制,防止卡住(30秒超时)
15582
+ const timeoutMs = 30000; // 30秒超时
15583
+ const timeoutPromise = new Promise((_, reject) => {
15584
+ setTimeout(() => {
15585
+ reject(new Error(`截图超时(${timeoutMs}ms),可能页面过大或 Worker 处理时间过长`));
15586
+ }, timeoutMs);
15587
+ });
15588
+ let dataUrl;
15589
+ const outputFormat = this.options.outputFormat || 'webp';
15590
+ if (!this.options.silentMode) {
15591
+ console.log(`📸 使用 ${outputFormat.toUpperCase()} 格式截图(直接输出,无需转换)...`);
15501
15592
  }
15502
- const img = new Image();
15503
- let convertedDataUrl;
15504
- try {
15505
- await new Promise((resolve, reject) => {
15506
- img.onload = () => {
15507
- canvas.width = img.width;
15508
- canvas.height = img.height;
15509
- ctx.drawImage(img, 0, 0);
15510
- resolve();
15511
- };
15512
- img.onerror = reject;
15513
- img.src = dataUrl;
15593
+ // 根据输出格式选择对应的 API
15594
+ if (outputFormat === 'webp') {
15595
+ // 使用 domToWebp,直接输出 WebP 格式,无需转换
15596
+ dataUrl = await Promise.race([
15597
+ domToWebp(this.screenshotContext),
15598
+ timeoutPromise
15599
+ ]);
15600
+ }
15601
+ else if (outputFormat === 'jpeg') {
15602
+ // 使用 domToJpeg,直接输出 JPEG 格式,无需转换
15603
+ dataUrl = await Promise.race([
15604
+ domToJpeg(this.screenshotContext),
15605
+ timeoutPromise
15606
+ ]);
15607
+ }
15608
+ else {
15609
+ // 默认使用 domToPng
15610
+ dataUrl = await Promise.race([
15611
+ domToPng(this.screenshotContext),
15612
+ timeoutPromise
15613
+ ]);
15614
+ }
15615
+ // 验证截图结果
15616
+ if (!dataUrl || dataUrl.length < 100) {
15617
+ throw new Error('生成的截图数据无效或过短');
15618
+ }
15619
+ if (!this.options.silentMode) {
15620
+ console.log(`📸 ✅ modern-screenshot 截图成功(${outputFormat.toUpperCase()} 格式,直接输出,无需转换)`);
15621
+ }
15622
+ return dataUrl;
15623
+ }
15624
+ catch (error) {
15625
+ const errorMessage = error instanceof Error ? error.message : String(error);
15626
+ if (!this.options.silentMode) {
15627
+ console.error('📸 modern-screenshot 截图失败:', errorMessage);
15628
+ console.error('📸 元素信息:', {
15629
+ width: rect.width,
15630
+ height: rect.height,
15631
+ scrollWidth: element.scrollWidth,
15632
+ scrollHeight: element.scrollHeight,
15633
+ display: window.getComputedStyle(element).display,
15634
+ visibility: window.getComputedStyle(element).visibility
15514
15635
  });
15515
- let mimeType = 'image/jpeg';
15516
- // 使用与 createContext 相同的质量设置
15517
- let conversionQuality = finalQuality;
15518
- if (this.options.outputFormat === 'webp' && !isMobile) {
15519
- try {
15520
- const testCanvas = document.createElement('canvas');
15521
- testCanvas.width = 1;
15522
- testCanvas.height = 1;
15523
- const testDataUrl = testCanvas.toDataURL('image/webp');
15524
- if (testDataUrl.indexOf('webp') !== -1) {
15525
- mimeType = 'image/webp';
15526
- }
15636
+ }
15637
+ throw error;
15638
+ }
15639
+ finally {
15640
+ // 每次截图后立即清理 context,释放 Worker 和内存
15641
+ // 这是防止内存泄漏的关键步骤
15642
+ if (this.screenshotContext) {
15643
+ try {
15644
+ destroyContext(this.screenshotContext);
15645
+ if (!this.options.silentMode) {
15646
+ console.log('📸 modern-screenshot context 已清理');
15527
15647
  }
15528
- catch {
15529
- mimeType = 'image/jpeg';
15648
+ }
15649
+ catch (e) {
15650
+ if (!this.options.silentMode) {
15651
+ console.warn('📸 ⚠️ 清理 context 失败:', e);
15530
15652
  }
15531
15653
  }
15532
- // 使用优化后的质量进行格式转换
15533
- convertedDataUrl = mimeType === 'image/png'
15534
- ? canvas.toDataURL(mimeType)
15535
- : canvas.toDataURL(mimeType, conversionQuality);
15654
+ finally {
15655
+ // 确保 context 引用被清除
15656
+ this.screenshotContext = null;
15657
+ }
15536
15658
  }
15537
- finally {
15538
- // 清理资源,释放内存
15539
- img.src = ''; // 清除图片引用
15540
- img.onload = null;
15541
- img.onerror = null;
15542
- canvas.width = 0; // 清空 canvas
15543
- canvas.height = 0;
15544
- ctx.clearRect(0, 0, 0, 0); // 清除绘制内容
15659
+ // 释放截图锁
15660
+ this.isScreenshotInProgress = false;
15661
+ // 强制触发垃圾回收(如果可能)
15662
+ // 注意:这需要浏览器支持,不是所有浏览器都有效
15663
+ if (typeof window !== 'undefined' && window.gc && typeof window.gc === 'function') {
15664
+ try {
15665
+ window.gc();
15666
+ }
15667
+ catch {
15668
+ // 忽略 GC 错误
15669
+ }
15545
15670
  }
15546
- return convertedDataUrl;
15547
15671
  }
15548
- return dataUrl;
15549
15672
  }
15550
15673
  catch (error) {
15674
+ // 外层错误处理:确保即使发生错误也释放锁
15551
15675
  const errorMessage = error instanceof Error ? error.message : String(error);
15552
15676
  if (!this.options.silentMode) {
15553
- console.error('📸 modern-screenshot 截图失败:', errorMessage);
15554
- console.error('📸 元素信息:', {
15555
- width: rect.width,
15556
- height: rect.height,
15557
- scrollWidth: element.scrollWidth,
15558
- scrollHeight: element.scrollHeight,
15559
- display: window.getComputedStyle(element).display,
15560
- visibility: window.getComputedStyle(element).visibility
15561
- });
15677
+ console.error('📸 modern-screenshot 截图异常:', errorMessage);
15562
15678
  }
15563
15679
  throw error;
15564
15680
  }
15565
15681
  finally {
15566
- // 每次截图后立即清理 context,释放 Worker 和内存
15567
- // 这是防止内存泄漏的关键步骤
15568
- if (this.screenshotContext) {
15569
- try {
15570
- destroyContext(this.screenshotContext);
15571
- if (!this.options.silentMode) {
15572
- console.log('📸 ✅ modern-screenshot context 已清理');
15573
- }
15574
- }
15575
- catch (e) {
15576
- if (!this.options.silentMode) {
15577
- console.warn('📸 ⚠️ 清理 context 失败:', e);
15578
- }
15579
- }
15580
- finally {
15581
- // 确保 context 引用被清除
15582
- this.screenshotContext = null;
15583
- }
15584
- }
15585
- // 强制触发垃圾回收(如果可能)
15586
- // 注意:这需要浏览器支持,不是所有浏览器都有效
15587
- if (typeof window !== 'undefined' && window.gc && typeof window.gc === 'function') {
15588
- try {
15589
- window.gc();
15590
- }
15591
- catch {
15592
- // 忽略 GC 错误
15593
- }
15682
+ // 确保截图锁被释放(即使发生未捕获的错误)
15683
+ if (this.isScreenshotInProgress) {
15684
+ this.isScreenshotInProgress = false;
15594
15685
  }
15595
15686
  }
15596
15687
  }