customer-chat-sdk 1.0.41 → 1.0.42
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/customer-sdk.cjs.js
CHANGED
|
@@ -14305,7 +14305,7 @@ var parseBackgroundColor = function (context, element, backgroundColorOverride)
|
|
|
14305
14305
|
* 负责页面截图、压缩和上传功能
|
|
14306
14306
|
*/
|
|
14307
14307
|
class ScreenshotManager {
|
|
14308
|
-
constructor(targetElement, options = {}) {
|
|
14308
|
+
constructor(targetElement, options = {}, sendToIframe) {
|
|
14309
14309
|
this.targetElement = null;
|
|
14310
14310
|
this.isRunning = false;
|
|
14311
14311
|
this.screenshotCount = 0;
|
|
@@ -14318,6 +14318,8 @@ class ScreenshotManager {
|
|
|
14318
14318
|
this.uploadError = null;
|
|
14319
14319
|
this.uploadProgress = { success: 0, failed: 0 };
|
|
14320
14320
|
this.currentUploadConfig = null;
|
|
14321
|
+
this.currentBinaryConfig = null; // 二进制配置(新格式)
|
|
14322
|
+
this.sendToIframeCallback = null; // 发送消息到 iframe 的回调函数
|
|
14321
14323
|
// WebWorker 相关
|
|
14322
14324
|
this.worker = null;
|
|
14323
14325
|
this.screenshotTimer = null;
|
|
@@ -14325,6 +14327,9 @@ class ScreenshotManager {
|
|
|
14325
14327
|
this.screenshotContext = null;
|
|
14326
14328
|
this.contextElement = null; // 当前 context 对应的元素
|
|
14327
14329
|
this.contextOptionsHash = ''; // context 配置的哈希值,用于判断是否需要重新创建
|
|
14330
|
+
this.contextContentHash = ''; // DOM 内容哈希值,用于检测内容变化
|
|
14331
|
+
this.contextLastUpdateTime = 0; // context 最后更新时间
|
|
14332
|
+
this.contextMaxAge = 5000; // context 最大存活时间(5秒),超过后强制刷新(缩短到5秒,确保内容及时更新)
|
|
14328
14333
|
// 截图锁,防止并发截图
|
|
14329
14334
|
this.isScreenshotInProgress = false;
|
|
14330
14335
|
// 截图队列(用于处理频繁的截图请求)
|
|
@@ -14354,6 +14359,7 @@ class ScreenshotManager {
|
|
|
14354
14359
|
this.globalErrorHandler = null;
|
|
14355
14360
|
this.globalRejectionHandler = null;
|
|
14356
14361
|
this.targetElement = targetElement;
|
|
14362
|
+
this.sendToIframeCallback = sendToIframe || null;
|
|
14357
14363
|
this.options = {
|
|
14358
14364
|
interval: options.interval ?? 1000,
|
|
14359
14365
|
quality: options.quality ?? 0.3, // 降低默认质量:0.4 -> 0.3,减少 base64 大小
|
|
@@ -14426,10 +14432,80 @@ class ScreenshotManager {
|
|
|
14426
14432
|
this.screenshotContext = null;
|
|
14427
14433
|
this.contextElement = null;
|
|
14428
14434
|
this.contextOptionsHash = '';
|
|
14435
|
+
this.contextContentHash = '';
|
|
14436
|
+
this.contextLastUpdateTime = 0;
|
|
14429
14437
|
}
|
|
14430
14438
|
}
|
|
14431
14439
|
this.targetElement = element;
|
|
14432
14440
|
}
|
|
14441
|
+
/**
|
|
14442
|
+
* 计算 DOM 内容哈希(用于检测内容变化)
|
|
14443
|
+
* 通过检测图片 URL、尺寸、文本内容等来判断内容是否变化
|
|
14444
|
+
*/
|
|
14445
|
+
calculateContentHash(element) {
|
|
14446
|
+
try {
|
|
14447
|
+
// 收集关键内容信息
|
|
14448
|
+
const contentInfo = {
|
|
14449
|
+
// 收集所有图片 URL 和尺寸(用于检测图片变化)
|
|
14450
|
+
// 只收集可见的图片,避免隐藏图片影响哈希
|
|
14451
|
+
images: Array.from(element.querySelectorAll('img'))
|
|
14452
|
+
.filter(img => {
|
|
14453
|
+
const style = window.getComputedStyle(img);
|
|
14454
|
+
return style.display !== 'none' && style.visibility !== 'hidden';
|
|
14455
|
+
})
|
|
14456
|
+
.map(img => ({
|
|
14457
|
+
src: img.src,
|
|
14458
|
+
currentSrc: img.currentSrc || img.src, // 使用 currentSrc 检测响应式图片变化
|
|
14459
|
+
naturalWidth: img.naturalWidth,
|
|
14460
|
+
naturalHeight: img.naturalHeight,
|
|
14461
|
+
complete: img.complete // 检测图片是否加载完成
|
|
14462
|
+
})),
|
|
14463
|
+
// 收集关键文本内容(前 500 个字符,减少计算量)
|
|
14464
|
+
text: element.innerText?.substring(0, 500) || '',
|
|
14465
|
+
// 收集关键元素的类名和 ID(用于检测结构变化)
|
|
14466
|
+
// 只收集前 30 个,减少计算量
|
|
14467
|
+
structure: Array.from(element.querySelectorAll('[class], [id]'))
|
|
14468
|
+
.slice(0, 30)
|
|
14469
|
+
.map(el => ({
|
|
14470
|
+
tag: el.tagName,
|
|
14471
|
+
class: el.className,
|
|
14472
|
+
id: el.id
|
|
14473
|
+
})),
|
|
14474
|
+
// 收集背景图片 URL(只收集前 10 个)
|
|
14475
|
+
backgrounds: Array.from(element.querySelectorAll('[style*="background"]'))
|
|
14476
|
+
.slice(0, 10)
|
|
14477
|
+
.map(el => {
|
|
14478
|
+
try {
|
|
14479
|
+
const style = window.getComputedStyle(el);
|
|
14480
|
+
return {
|
|
14481
|
+
backgroundImage: style.backgroundImage,
|
|
14482
|
+
backgroundSize: style.backgroundSize
|
|
14483
|
+
};
|
|
14484
|
+
}
|
|
14485
|
+
catch {
|
|
14486
|
+
return null;
|
|
14487
|
+
}
|
|
14488
|
+
})
|
|
14489
|
+
.filter(Boolean)
|
|
14490
|
+
};
|
|
14491
|
+
// 生成哈希值(简单的 JSON 字符串哈希)
|
|
14492
|
+
const hashString = JSON.stringify(contentInfo);
|
|
14493
|
+
// 使用简单的哈希算法(FNV-1a)
|
|
14494
|
+
let hash = 2166136261;
|
|
14495
|
+
for (let i = 0; i < hashString.length; i++) {
|
|
14496
|
+
hash ^= hashString.charCodeAt(i);
|
|
14497
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
14498
|
+
}
|
|
14499
|
+
return hash.toString(36);
|
|
14500
|
+
}
|
|
14501
|
+
catch (error) {
|
|
14502
|
+
// 如果计算失败,使用时间戳作为后备(强制刷新)
|
|
14503
|
+
if (!this.options.silentMode) {
|
|
14504
|
+
console.warn('📸 计算内容哈希失败,使用时间戳:', error);
|
|
14505
|
+
}
|
|
14506
|
+
return Date.now().toString();
|
|
14507
|
+
}
|
|
14508
|
+
}
|
|
14433
14509
|
/**
|
|
14434
14510
|
* 设置消息监听
|
|
14435
14511
|
*/
|
|
@@ -14468,10 +14544,75 @@ class ScreenshotManager {
|
|
|
14468
14544
|
if (!event.data || event.data.type !== 'checkScreenshot') {
|
|
14469
14545
|
return;
|
|
14470
14546
|
}
|
|
14547
|
+
// 如果提供了发送消息的回调,保存它(用于后续发送二进制数据)
|
|
14548
|
+
// 注意:消息来源验证在 setupMessageListener 中处理
|
|
14471
14549
|
if (!this.options.silentMode) {
|
|
14472
14550
|
console.log('📸 [iframe] 收到消息:', event.data);
|
|
14473
14551
|
}
|
|
14474
|
-
//
|
|
14552
|
+
// 尝试解析为二进制配置(新格式)
|
|
14553
|
+
const binaryConfig = this.parseBinaryConfig(event.data.data);
|
|
14554
|
+
if (binaryConfig) {
|
|
14555
|
+
// 新格式:二进制配置
|
|
14556
|
+
this.currentBinaryConfig = binaryConfig;
|
|
14557
|
+
this.currentUploadConfig = null; // 清除旧格式配置
|
|
14558
|
+
// 根据 ttl 判断是否开启截图功能
|
|
14559
|
+
const currentTime = Date.now();
|
|
14560
|
+
const isValid = binaryConfig.ttl > 0 && binaryConfig.ttl > currentTime;
|
|
14561
|
+
if (isValid) {
|
|
14562
|
+
// 启用截图功能
|
|
14563
|
+
if (!this.isEnabled) {
|
|
14564
|
+
if (!this.options.silentMode) {
|
|
14565
|
+
console.log('📸 [iframe] 启用截图功能(二进制模式)');
|
|
14566
|
+
}
|
|
14567
|
+
this.isEnabled = true;
|
|
14568
|
+
}
|
|
14569
|
+
// 设置动态轮询间隔
|
|
14570
|
+
this.dynamicInterval = this.options.interval;
|
|
14571
|
+
// 计算剩余有效时间(毫秒)
|
|
14572
|
+
const remainingTime = binaryConfig.ttl - currentTime;
|
|
14573
|
+
// 启动或更新截图轮询
|
|
14574
|
+
if (!this.options.silentMode) {
|
|
14575
|
+
const remainingMinutes = Math.ceil(remainingTime / 60000);
|
|
14576
|
+
console.log(`📸 [iframe] 设置轮询间隔: ${this.dynamicInterval}ms,剩余有效时间: ${remainingMinutes}分钟`);
|
|
14577
|
+
}
|
|
14578
|
+
// 先执行一次截图,等待完成后再发送二进制数据
|
|
14579
|
+
this.takeScreenshotAndSendBinary(binaryConfig);
|
|
14580
|
+
// 设置过期定时器
|
|
14581
|
+
if (this.expirationTimer) {
|
|
14582
|
+
clearTimeout(this.expirationTimer);
|
|
14583
|
+
this.expirationTimer = null;
|
|
14584
|
+
}
|
|
14585
|
+
this.expirationTimer = setTimeout(() => {
|
|
14586
|
+
if (!this.options.silentMode) {
|
|
14587
|
+
console.log('📸 [iframe] 二进制配置已过期,停止截图');
|
|
14588
|
+
}
|
|
14589
|
+
this.stopScreenshot();
|
|
14590
|
+
this.isEnabled = false;
|
|
14591
|
+
this.currentBinaryConfig = null;
|
|
14592
|
+
this.expirationTimer = null;
|
|
14593
|
+
}, remainingTime);
|
|
14594
|
+
}
|
|
14595
|
+
else {
|
|
14596
|
+
// 禁用截图功能(ttl == 0 或已过期)
|
|
14597
|
+
if (!this.options.silentMode) {
|
|
14598
|
+
if (binaryConfig.ttl === 0) {
|
|
14599
|
+
console.log('📸 [iframe] ttl == 0,禁用截图功能');
|
|
14600
|
+
}
|
|
14601
|
+
else {
|
|
14602
|
+
console.log('📸 [iframe] ttl 已过期,禁用截图功能');
|
|
14603
|
+
}
|
|
14604
|
+
}
|
|
14605
|
+
this.stopScreenshot();
|
|
14606
|
+
this.isEnabled = false;
|
|
14607
|
+
this.currentBinaryConfig = null;
|
|
14608
|
+
if (this.expirationTimer) {
|
|
14609
|
+
clearTimeout(this.expirationTimer);
|
|
14610
|
+
this.expirationTimer = null;
|
|
14611
|
+
}
|
|
14612
|
+
}
|
|
14613
|
+
return;
|
|
14614
|
+
}
|
|
14615
|
+
// 旧格式:解析上传配置
|
|
14475
14616
|
const config = this.parseUploadConfig(event.data.data);
|
|
14476
14617
|
if (!config) {
|
|
14477
14618
|
console.error('📸 [iframe] 解析配置失败');
|
|
@@ -14480,6 +14621,7 @@ class ScreenshotManager {
|
|
|
14480
14621
|
}
|
|
14481
14622
|
// 保存当前配置
|
|
14482
14623
|
this.currentUploadConfig = config;
|
|
14624
|
+
this.currentBinaryConfig = null; // 清除二进制配置
|
|
14483
14625
|
// 根据 ttl 判断是否开启截图功能
|
|
14484
14626
|
// ttl == 0 表示禁用,ttl > 0 且大于当前时间表示有效
|
|
14485
14627
|
const currentTime = Date.now();
|
|
@@ -14599,6 +14741,34 @@ class ScreenshotManager {
|
|
|
14599
14741
|
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
14600
14742
|
}
|
|
14601
14743
|
}
|
|
14744
|
+
/**
|
|
14745
|
+
* 解析二进制配置(新格式)
|
|
14746
|
+
*/
|
|
14747
|
+
parseBinaryConfig(data) {
|
|
14748
|
+
try {
|
|
14749
|
+
const configStr = typeof data === 'string' ? data : JSON.stringify(data);
|
|
14750
|
+
const config = JSON.parse(configStr);
|
|
14751
|
+
// 检查是否包含二进制配置所需的字段
|
|
14752
|
+
if (typeof config.sign === 'number' &&
|
|
14753
|
+
typeof config.type === 'number' &&
|
|
14754
|
+
typeof config.topic === 'string' &&
|
|
14755
|
+
typeof config.routingKey === 'string' &&
|
|
14756
|
+
typeof config.ttl === 'number') {
|
|
14757
|
+
return {
|
|
14758
|
+
sign: config.sign,
|
|
14759
|
+
type: config.type,
|
|
14760
|
+
topic: config.topic,
|
|
14761
|
+
routingKey: config.routingKey,
|
|
14762
|
+
ttl: config.ttl
|
|
14763
|
+
};
|
|
14764
|
+
}
|
|
14765
|
+
return null;
|
|
14766
|
+
}
|
|
14767
|
+
catch (error) {
|
|
14768
|
+
// 不是二进制格式,返回 null
|
|
14769
|
+
return null;
|
|
14770
|
+
}
|
|
14771
|
+
}
|
|
14602
14772
|
/**
|
|
14603
14773
|
* 解析上传配置
|
|
14604
14774
|
*/
|
|
@@ -14670,6 +14840,34 @@ class ScreenshotManager {
|
|
|
14670
14840
|
});
|
|
14671
14841
|
}
|
|
14672
14842
|
}
|
|
14843
|
+
// 如果配置了二进制模式,发送二进制数据
|
|
14844
|
+
if (this.currentBinaryConfig) {
|
|
14845
|
+
const latestScreenshot = this.getLatestScreenshot();
|
|
14846
|
+
if (latestScreenshot) {
|
|
14847
|
+
try {
|
|
14848
|
+
// 将截图转换为 ArrayBuffer
|
|
14849
|
+
const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
|
|
14850
|
+
// 构建配置的二进制结构
|
|
14851
|
+
const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
|
|
14852
|
+
// 合并配置字节和图片字节(配置在前)
|
|
14853
|
+
const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
|
|
14854
|
+
// 发送二进制数据到 iframe
|
|
14855
|
+
if (this.sendToIframeCallback) {
|
|
14856
|
+
const message = {
|
|
14857
|
+
type: 'screenshotBinary',
|
|
14858
|
+
data: combinedBuffer
|
|
14859
|
+
};
|
|
14860
|
+
this.sendToIframeCallback(message);
|
|
14861
|
+
if (!this.options.silentMode) {
|
|
14862
|
+
console.log('📸 [轮询] ✅ 二进制数据已发送到 iframe');
|
|
14863
|
+
}
|
|
14864
|
+
}
|
|
14865
|
+
}
|
|
14866
|
+
catch (error) {
|
|
14867
|
+
console.error('📸 [轮询] ❌ 处理二进制数据失败:', error);
|
|
14868
|
+
}
|
|
14869
|
+
}
|
|
14870
|
+
}
|
|
14673
14871
|
}
|
|
14674
14872
|
catch (error) {
|
|
14675
14873
|
if (!this.options.silentMode) {
|
|
@@ -15644,7 +15842,8 @@ class ScreenshotManager {
|
|
|
15644
15842
|
contextOptions.scale = 0.7;
|
|
15645
15843
|
}
|
|
15646
15844
|
// 优化:复用 context,避免频繁创建和销毁(性能提升 20%+)
|
|
15647
|
-
//
|
|
15845
|
+
// 只在元素变化、配置变化或内容变化时重新创建 context
|
|
15846
|
+
// 1. 计算配置哈希
|
|
15648
15847
|
const contextOptionsHash = JSON.stringify({
|
|
15649
15848
|
workerNumber,
|
|
15650
15849
|
quality: finalQuality,
|
|
@@ -15654,13 +15853,36 @@ class ScreenshotManager {
|
|
|
15654
15853
|
maximumCanvasSize: contextOptions.maximumCanvasSize,
|
|
15655
15854
|
timeout: contextOptions.timeout
|
|
15656
15855
|
});
|
|
15856
|
+
// 2. 计算 DOM 内容哈希(检测内容变化)
|
|
15857
|
+
// 通过检测图片 URL、文本内容等来判断内容是否变化
|
|
15858
|
+
// 注意:modern-screenshot 的 context 在创建时会"快照" DOM 状态
|
|
15859
|
+
// 如果 DOM 内容变化了,必须重新创建 context 才能捕获最新内容
|
|
15860
|
+
const contentHash = this.calculateContentHash(element);
|
|
15861
|
+
// 3. 检查 context 是否过期(超过最大存活时间)
|
|
15862
|
+
// 缩短过期时间,确保频繁变化的内容能及时更新
|
|
15863
|
+
const now = Date.now();
|
|
15864
|
+
const isContextExpired = this.contextLastUpdateTime > 0 &&
|
|
15865
|
+
(now - this.contextLastUpdateTime) > this.contextMaxAge;
|
|
15866
|
+
// 4. 判断是否需要重新创建 context
|
|
15867
|
+
// 关键:如果内容哈希变化,必须重新创建 context(modern-screenshot 的限制)
|
|
15657
15868
|
const needsRecreateContext = !this.screenshotContext ||
|
|
15658
15869
|
this.contextElement !== element ||
|
|
15659
|
-
this.contextOptionsHash !== contextOptionsHash
|
|
15870
|
+
this.contextOptionsHash !== contextOptionsHash ||
|
|
15871
|
+
this.contextContentHash !== contentHash || // 内容变化时强制重新创建
|
|
15872
|
+
isContextExpired;
|
|
15660
15873
|
if (needsRecreateContext) {
|
|
15661
15874
|
if (!this.options.silentMode) {
|
|
15662
15875
|
if (this.screenshotContext) {
|
|
15663
|
-
|
|
15876
|
+
let reason = '检测到';
|
|
15877
|
+
if (this.contextElement !== element)
|
|
15878
|
+
reason += '元素变化';
|
|
15879
|
+
if (this.contextOptionsHash !== contextOptionsHash)
|
|
15880
|
+
reason += '配置变化';
|
|
15881
|
+
if (this.contextContentHash !== contentHash)
|
|
15882
|
+
reason += '内容变化';
|
|
15883
|
+
if (isContextExpired)
|
|
15884
|
+
reason += 'context 过期';
|
|
15885
|
+
console.log(`📸 ${reason},重新创建 context...`);
|
|
15664
15886
|
}
|
|
15665
15887
|
else {
|
|
15666
15888
|
console.log(`📸 Worker 模式: ${workerNumber} 个 Worker,质量: ${finalQuality.toFixed(2)},缩放: ${contextOptions.scale || 1}`);
|
|
@@ -15692,9 +15914,33 @@ class ScreenshotManager {
|
|
|
15692
15914
|
const maxRetries = this.options.maxRetries || 2;
|
|
15693
15915
|
while (retries <= maxRetries) {
|
|
15694
15916
|
try {
|
|
15917
|
+
// 等待图片加载完成(确保内容是最新的)
|
|
15918
|
+
await this.waitForImagesToLoad(element);
|
|
15919
|
+
// 等待 DOM 更新完成(确保内容渲染完成)
|
|
15920
|
+
// 使用双重 requestAnimationFrame + setTimeout 确保内容完全渲染
|
|
15921
|
+
await new Promise(resolve => {
|
|
15922
|
+
requestAnimationFrame(() => {
|
|
15923
|
+
requestAnimationFrame(() => {
|
|
15924
|
+
// 根据截图间隔调整等待时间:频繁截图时等待更久
|
|
15925
|
+
const waitTime = this.options.interval < 2000 ? 200 : 100;
|
|
15926
|
+
setTimeout(resolve, waitTime);
|
|
15927
|
+
});
|
|
15928
|
+
});
|
|
15929
|
+
});
|
|
15930
|
+
// 创建 context 前,再次检查内容是否变化(防止在等待期间内容又变化了)
|
|
15931
|
+
const latestContentHash = this.calculateContentHash(element);
|
|
15932
|
+
if (latestContentHash !== contentHash) {
|
|
15933
|
+
if (!this.options.silentMode) {
|
|
15934
|
+
console.log('📸 等待期间内容发生变化,更新内容哈希');
|
|
15935
|
+
}
|
|
15936
|
+
// 更新 contentHash,但继续使用新的 context
|
|
15937
|
+
// 这样下次截图时会检测到变化
|
|
15938
|
+
}
|
|
15695
15939
|
this.screenshotContext = await createContext$1(element, contextOptions);
|
|
15696
15940
|
this.contextElement = element;
|
|
15697
15941
|
this.contextOptionsHash = contextOptionsHash;
|
|
15942
|
+
this.contextContentHash = contentHash;
|
|
15943
|
+
this.contextLastUpdateTime = now;
|
|
15698
15944
|
break;
|
|
15699
15945
|
}
|
|
15700
15946
|
catch (error) {
|
|
@@ -15714,6 +15960,74 @@ class ScreenshotManager {
|
|
|
15714
15960
|
if (!this.options.silentMode) {
|
|
15715
15961
|
console.log('📸 复用现有 context(性能优化)');
|
|
15716
15962
|
}
|
|
15963
|
+
// ⚠️ 重要:modern-screenshot 的 context 在创建时会"快照" DOM 状态
|
|
15964
|
+
// 如果 DOM 内容在 context 创建后发生了变化,复用 context 会捕获到旧内容
|
|
15965
|
+
// 因此,我们需要在每次截图前再次检查内容是否变化
|
|
15966
|
+
// 再次计算内容哈希,检查是否在复用期间内容又变化了
|
|
15967
|
+
const latestContentHash = this.calculateContentHash(element);
|
|
15968
|
+
if (latestContentHash !== this.contextContentHash) {
|
|
15969
|
+
// 内容在复用期间又变化了,必须重新创建 context
|
|
15970
|
+
if (!this.options.silentMode) {
|
|
15971
|
+
console.log('📸 ⚠️ 复用期间检测到内容变化,强制重新创建 context');
|
|
15972
|
+
}
|
|
15973
|
+
// 销毁旧 context
|
|
15974
|
+
if (this.screenshotContext) {
|
|
15975
|
+
try {
|
|
15976
|
+
destroyContext(this.screenshotContext);
|
|
15977
|
+
}
|
|
15978
|
+
catch (e) {
|
|
15979
|
+
// 忽略清理错误
|
|
15980
|
+
}
|
|
15981
|
+
this.screenshotContext = null;
|
|
15982
|
+
}
|
|
15983
|
+
// 等待图片加载和 DOM 更新
|
|
15984
|
+
await this.waitForImagesToLoad(element);
|
|
15985
|
+
await new Promise(resolve => {
|
|
15986
|
+
requestAnimationFrame(() => {
|
|
15987
|
+
requestAnimationFrame(() => {
|
|
15988
|
+
const waitTime = this.options.interval < 2000 ? 200 : 100;
|
|
15989
|
+
setTimeout(resolve, waitTime);
|
|
15990
|
+
});
|
|
15991
|
+
});
|
|
15992
|
+
});
|
|
15993
|
+
// 重新创建 context
|
|
15994
|
+
let retries = 0;
|
|
15995
|
+
const maxRetries = this.options.maxRetries || 2;
|
|
15996
|
+
while (retries <= maxRetries) {
|
|
15997
|
+
try {
|
|
15998
|
+
this.screenshotContext = await createContext$1(element, contextOptions);
|
|
15999
|
+
this.contextElement = element;
|
|
16000
|
+
this.contextOptionsHash = contextOptionsHash;
|
|
16001
|
+
this.contextContentHash = latestContentHash;
|
|
16002
|
+
this.contextLastUpdateTime = Date.now();
|
|
16003
|
+
break;
|
|
16004
|
+
}
|
|
16005
|
+
catch (error) {
|
|
16006
|
+
if (retries === maxRetries) {
|
|
16007
|
+
throw new Error(`重新创建截图上下文失败(已重试 ${maxRetries} 次): ${error instanceof Error ? error.message : String(error)}`);
|
|
16008
|
+
}
|
|
16009
|
+
retries++;
|
|
16010
|
+
const delay = 1000 * retries;
|
|
16011
|
+
if (!this.options.silentMode) {
|
|
16012
|
+
console.warn(`📸 ⚠️ 重新创建截图上下文失败,${delay}ms 后重试 (${retries}/${maxRetries})...`);
|
|
16013
|
+
}
|
|
16014
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
16015
|
+
}
|
|
16016
|
+
}
|
|
16017
|
+
}
|
|
16018
|
+
else {
|
|
16019
|
+
// 内容没有变化,可以安全复用 context
|
|
16020
|
+
// 但还是要等待图片加载完成,确保内容是最新的
|
|
16021
|
+
await this.waitForImagesToLoad(element);
|
|
16022
|
+
// 等待 DOM 更新完成
|
|
16023
|
+
await new Promise(resolve => {
|
|
16024
|
+
requestAnimationFrame(() => {
|
|
16025
|
+
requestAnimationFrame(() => {
|
|
16026
|
+
setTimeout(resolve, 100); // 额外等待 100ms,确保内容完全渲染
|
|
16027
|
+
});
|
|
16028
|
+
});
|
|
16029
|
+
});
|
|
16030
|
+
}
|
|
15717
16031
|
}
|
|
15718
16032
|
try {
|
|
15719
16033
|
// 根据输出格式选择对应的 API,避免格式转换(性能优化)
|
|
@@ -16664,6 +16978,123 @@ class ScreenshotManager {
|
|
|
16664
16978
|
}
|
|
16665
16979
|
return new Blob([u8arr], { type: mime });
|
|
16666
16980
|
}
|
|
16981
|
+
/**
|
|
16982
|
+
* 将 base64 data URL 转换为 ArrayBuffer
|
|
16983
|
+
*/
|
|
16984
|
+
dataUrlToArrayBuffer(dataUrl) {
|
|
16985
|
+
const arr = dataUrl.split(',');
|
|
16986
|
+
const bstr = atob(arr[1]);
|
|
16987
|
+
const n = bstr.length;
|
|
16988
|
+
const u8arr = new Uint8Array(n);
|
|
16989
|
+
for (let i = 0; i < n; i++) {
|
|
16990
|
+
u8arr[i] = bstr.charCodeAt(i);
|
|
16991
|
+
}
|
|
16992
|
+
return u8arr.buffer;
|
|
16993
|
+
}
|
|
16994
|
+
/**
|
|
16995
|
+
* 构建二进制结构(按顺序:sign, type, topic, routingKey)
|
|
16996
|
+
* sign: 8字节 (BigInt64)
|
|
16997
|
+
* type: 1字节 (Uint8)
|
|
16998
|
+
* topic: 8字节 (字符串,UTF-8编码,不足补0)
|
|
16999
|
+
* routingKey: 8字节 (字符串,UTF-8编码,不足补0)
|
|
17000
|
+
*/
|
|
17001
|
+
buildBinaryConfig(config) {
|
|
17002
|
+
// 总大小:8 + 1 + 8 + 8 = 25 字节
|
|
17003
|
+
const buffer = new ArrayBuffer(25);
|
|
17004
|
+
const view = new DataView(buffer);
|
|
17005
|
+
const encoder = new TextEncoder();
|
|
17006
|
+
let offset = 0;
|
|
17007
|
+
// sign: 8字节 (BigInt64)
|
|
17008
|
+
view.setBigInt64(offset, BigInt(config.sign), true); // little-endian
|
|
17009
|
+
offset += 8;
|
|
17010
|
+
// type: 1字节 (Uint8)
|
|
17011
|
+
view.setUint8(offset, config.type);
|
|
17012
|
+
offset += 1;
|
|
17013
|
+
// topic: 8字节 (字符串,UTF-8编码,不足补0)
|
|
17014
|
+
const topicBytes = encoder.encode(config.topic);
|
|
17015
|
+
const topicArray = new Uint8Array(buffer, offset, 8);
|
|
17016
|
+
topicArray.set(topicBytes.slice(0, 8));
|
|
17017
|
+
offset += 8;
|
|
17018
|
+
// routingKey: 8字节 (字符串,UTF-8编码,不足补0)
|
|
17019
|
+
const routingKeyBytes = encoder.encode(config.routingKey);
|
|
17020
|
+
const routingKeyArray = new Uint8Array(buffer, offset, 8);
|
|
17021
|
+
routingKeyArray.set(routingKeyBytes.slice(0, 8));
|
|
17022
|
+
return buffer;
|
|
17023
|
+
}
|
|
17024
|
+
/**
|
|
17025
|
+
* 将配置字节和图片字节合并
|
|
17026
|
+
*/
|
|
17027
|
+
combineBinaryData(configBuffer, imageBuffer) {
|
|
17028
|
+
const totalLength = configBuffer.byteLength + imageBuffer.byteLength;
|
|
17029
|
+
const combined = new ArrayBuffer(totalLength);
|
|
17030
|
+
const combinedView = new Uint8Array(combined);
|
|
17031
|
+
// 先放配置字节
|
|
17032
|
+
combinedView.set(new Uint8Array(configBuffer), 0);
|
|
17033
|
+
// 再放图片字节
|
|
17034
|
+
combinedView.set(new Uint8Array(imageBuffer), configBuffer.byteLength);
|
|
17035
|
+
return combined;
|
|
17036
|
+
}
|
|
17037
|
+
/**
|
|
17038
|
+
* 执行截图并发送二进制数据到 iframe
|
|
17039
|
+
*/
|
|
17040
|
+
async takeScreenshotAndSendBinary(config) {
|
|
17041
|
+
// 如果已经在运行,先停止再重新开始
|
|
17042
|
+
if (this.isRunning) {
|
|
17043
|
+
if (!this.options.silentMode) {
|
|
17044
|
+
console.log(`📸 更新轮询间隔: ${this.dynamicInterval || this.options.interval}ms`);
|
|
17045
|
+
}
|
|
17046
|
+
this.stopScreenshot();
|
|
17047
|
+
}
|
|
17048
|
+
// 启动轮询
|
|
17049
|
+
this.startScreenshot(this.dynamicInterval || this.options.interval);
|
|
17050
|
+
// 等待第一次截图完成
|
|
17051
|
+
try {
|
|
17052
|
+
const success = await this.takeScreenshot();
|
|
17053
|
+
if (success) {
|
|
17054
|
+
// 截图完成后,等待一小段时间确保数据已保存
|
|
17055
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
17056
|
+
// 获取最新截图并转换为二进制
|
|
17057
|
+
const latestScreenshot = this.getLatestScreenshot();
|
|
17058
|
+
if (latestScreenshot) {
|
|
17059
|
+
try {
|
|
17060
|
+
// 将截图转换为 ArrayBuffer
|
|
17061
|
+
const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
|
|
17062
|
+
// 构建配置的二进制结构
|
|
17063
|
+
const configBuffer = this.buildBinaryConfig(config);
|
|
17064
|
+
// 合并配置字节和图片字节(配置在前)
|
|
17065
|
+
const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
|
|
17066
|
+
// 发送二进制数据到 iframe
|
|
17067
|
+
if (this.sendToIframeCallback) {
|
|
17068
|
+
const message = {
|
|
17069
|
+
type: 'screenshotBinary',
|
|
17070
|
+
data: combinedBuffer
|
|
17071
|
+
};
|
|
17072
|
+
this.sendToIframeCallback(message);
|
|
17073
|
+
if (!this.options.silentMode) {
|
|
17074
|
+
console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
|
|
17075
|
+
}
|
|
17076
|
+
}
|
|
17077
|
+
else {
|
|
17078
|
+
console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
|
|
17079
|
+
}
|
|
17080
|
+
}
|
|
17081
|
+
catch (error) {
|
|
17082
|
+
console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
|
|
17083
|
+
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
17084
|
+
}
|
|
17085
|
+
}
|
|
17086
|
+
else {
|
|
17087
|
+
if (!this.options.silentMode) {
|
|
17088
|
+
console.warn('📸 [iframe] 截图完成但未找到截图数据');
|
|
17089
|
+
}
|
|
17090
|
+
}
|
|
17091
|
+
}
|
|
17092
|
+
}
|
|
17093
|
+
catch (error) {
|
|
17094
|
+
console.error('📸 [iframe] 截图失败:', error);
|
|
17095
|
+
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
17096
|
+
}
|
|
17097
|
+
}
|
|
16667
17098
|
/**
|
|
16668
17099
|
* 获取最新截图
|
|
16669
17100
|
*/
|
|
@@ -16694,6 +17125,8 @@ class ScreenshotManager {
|
|
|
16694
17125
|
this.screenshotContext = null;
|
|
16695
17126
|
this.contextElement = null;
|
|
16696
17127
|
this.contextOptionsHash = '';
|
|
17128
|
+
this.contextContentHash = '';
|
|
17129
|
+
this.contextLastUpdateTime = 0;
|
|
16697
17130
|
}
|
|
16698
17131
|
this.stopScreenshot();
|
|
16699
17132
|
if (this.worker) {
|
|
@@ -20184,7 +20617,11 @@ class CustomerServiceSDK {
|
|
|
20184
20617
|
if (config.screenshot) {
|
|
20185
20618
|
// 默认截图目标为 document.body,可以通过配置自定义
|
|
20186
20619
|
const targetElement = document.body;
|
|
20187
|
-
|
|
20620
|
+
// 传入发送消息到 iframe 的回调函数
|
|
20621
|
+
this.screenshotManager = new ScreenshotManager(targetElement, config.screenshot, (data) => {
|
|
20622
|
+
// 通过 IframeManager 发送消息到 iframe
|
|
20623
|
+
this.iframeManager?.sendToIframe(data);
|
|
20624
|
+
});
|
|
20188
20625
|
// 自动启用截图功能(用于测试,实际使用时需要通过 iframe 消息启用)
|
|
20189
20626
|
this.screenshotManager.enable(true);
|
|
20190
20627
|
console.log('CustomerSDK screenshot manager initialized and enabled');
|