customer-chat-sdk 1.0.41 → 1.0.43
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/core/ScreenshotManager.d.ts +41 -20
- package/dist/core/ScreenshotManager.d.ts.map +1 -1
- package/dist/customer-sdk.cjs.js +412 -196
- package/dist/customer-sdk.esm.js +412 -196
- package/dist/customer-sdk.min.js +2 -2
- package/dist/index.d.ts +3 -18
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/customer-sdk.esm.js
CHANGED
|
@@ -14301,7 +14301,7 @@ var parseBackgroundColor = function (context, element, backgroundColorOverride)
|
|
|
14301
14301
|
* 负责页面截图、压缩和上传功能
|
|
14302
14302
|
*/
|
|
14303
14303
|
class ScreenshotManager {
|
|
14304
|
-
constructor(targetElement, options = {}) {
|
|
14304
|
+
constructor(targetElement, options = {}, sendToIframe) {
|
|
14305
14305
|
this.targetElement = null;
|
|
14306
14306
|
this.isRunning = false;
|
|
14307
14307
|
this.screenshotCount = 0;
|
|
@@ -14310,10 +14310,9 @@ class ScreenshotManager {
|
|
|
14310
14310
|
this.error = null;
|
|
14311
14311
|
this.isEnabled = false;
|
|
14312
14312
|
// 上传相关状态
|
|
14313
|
-
this.isUploading = false;
|
|
14314
14313
|
this.uploadError = null;
|
|
14315
|
-
this.
|
|
14316
|
-
this.
|
|
14314
|
+
this.currentBinaryConfig = null; // 二进制配置
|
|
14315
|
+
this.sendToIframeCallback = null; // 发送消息到 iframe 的回调函数
|
|
14317
14316
|
// WebWorker 相关
|
|
14318
14317
|
this.worker = null;
|
|
14319
14318
|
this.screenshotTimer = null;
|
|
@@ -14321,6 +14320,9 @@ class ScreenshotManager {
|
|
|
14321
14320
|
this.screenshotContext = null;
|
|
14322
14321
|
this.contextElement = null; // 当前 context 对应的元素
|
|
14323
14322
|
this.contextOptionsHash = ''; // context 配置的哈希值,用于判断是否需要重新创建
|
|
14323
|
+
this.contextContentHash = ''; // DOM 内容哈希值,用于检测内容变化
|
|
14324
|
+
this.contextLastUpdateTime = 0; // context 最后更新时间
|
|
14325
|
+
this.contextMaxAge = 5000; // context 最大存活时间(5秒),超过后强制刷新(缩短到5秒,确保内容及时更新)
|
|
14324
14326
|
// 截图锁,防止并发截图
|
|
14325
14327
|
this.isScreenshotInProgress = false;
|
|
14326
14328
|
// 截图队列(用于处理频繁的截图请求)
|
|
@@ -14350,6 +14352,7 @@ class ScreenshotManager {
|
|
|
14350
14352
|
this.globalErrorHandler = null;
|
|
14351
14353
|
this.globalRejectionHandler = null;
|
|
14352
14354
|
this.targetElement = targetElement;
|
|
14355
|
+
this.sendToIframeCallback = sendToIframe || null;
|
|
14353
14356
|
this.options = {
|
|
14354
14357
|
interval: options.interval ?? 1000,
|
|
14355
14358
|
quality: options.quality ?? 0.3, // 降低默认质量:0.4 -> 0.3,减少 base64 大小
|
|
@@ -14422,10 +14425,80 @@ class ScreenshotManager {
|
|
|
14422
14425
|
this.screenshotContext = null;
|
|
14423
14426
|
this.contextElement = null;
|
|
14424
14427
|
this.contextOptionsHash = '';
|
|
14428
|
+
this.contextContentHash = '';
|
|
14429
|
+
this.contextLastUpdateTime = 0;
|
|
14425
14430
|
}
|
|
14426
14431
|
}
|
|
14427
14432
|
this.targetElement = element;
|
|
14428
14433
|
}
|
|
14434
|
+
/**
|
|
14435
|
+
* 计算 DOM 内容哈希(用于检测内容变化)
|
|
14436
|
+
* 通过检测图片 URL、尺寸、文本内容等来判断内容是否变化
|
|
14437
|
+
*/
|
|
14438
|
+
calculateContentHash(element) {
|
|
14439
|
+
try {
|
|
14440
|
+
// 收集关键内容信息
|
|
14441
|
+
const contentInfo = {
|
|
14442
|
+
// 收集所有图片 URL 和尺寸(用于检测图片变化)
|
|
14443
|
+
// 只收集可见的图片,避免隐藏图片影响哈希
|
|
14444
|
+
images: Array.from(element.querySelectorAll('img'))
|
|
14445
|
+
.filter(img => {
|
|
14446
|
+
const style = window.getComputedStyle(img);
|
|
14447
|
+
return style.display !== 'none' && style.visibility !== 'hidden';
|
|
14448
|
+
})
|
|
14449
|
+
.map(img => ({
|
|
14450
|
+
src: img.src,
|
|
14451
|
+
currentSrc: img.currentSrc || img.src, // 使用 currentSrc 检测响应式图片变化
|
|
14452
|
+
naturalWidth: img.naturalWidth,
|
|
14453
|
+
naturalHeight: img.naturalHeight,
|
|
14454
|
+
complete: img.complete // 检测图片是否加载完成
|
|
14455
|
+
})),
|
|
14456
|
+
// 收集关键文本内容(前 500 个字符,减少计算量)
|
|
14457
|
+
text: element.innerText?.substring(0, 500) || '',
|
|
14458
|
+
// 收集关键元素的类名和 ID(用于检测结构变化)
|
|
14459
|
+
// 只收集前 30 个,减少计算量
|
|
14460
|
+
structure: Array.from(element.querySelectorAll('[class], [id]'))
|
|
14461
|
+
.slice(0, 30)
|
|
14462
|
+
.map(el => ({
|
|
14463
|
+
tag: el.tagName,
|
|
14464
|
+
class: el.className,
|
|
14465
|
+
id: el.id
|
|
14466
|
+
})),
|
|
14467
|
+
// 收集背景图片 URL(只收集前 10 个)
|
|
14468
|
+
backgrounds: Array.from(element.querySelectorAll('[style*="background"]'))
|
|
14469
|
+
.slice(0, 10)
|
|
14470
|
+
.map(el => {
|
|
14471
|
+
try {
|
|
14472
|
+
const style = window.getComputedStyle(el);
|
|
14473
|
+
return {
|
|
14474
|
+
backgroundImage: style.backgroundImage,
|
|
14475
|
+
backgroundSize: style.backgroundSize
|
|
14476
|
+
};
|
|
14477
|
+
}
|
|
14478
|
+
catch {
|
|
14479
|
+
return null;
|
|
14480
|
+
}
|
|
14481
|
+
})
|
|
14482
|
+
.filter(Boolean)
|
|
14483
|
+
};
|
|
14484
|
+
// 生成哈希值(简单的 JSON 字符串哈希)
|
|
14485
|
+
const hashString = JSON.stringify(contentInfo);
|
|
14486
|
+
// 使用简单的哈希算法(FNV-1a)
|
|
14487
|
+
let hash = 2166136261;
|
|
14488
|
+
for (let i = 0; i < hashString.length; i++) {
|
|
14489
|
+
hash ^= hashString.charCodeAt(i);
|
|
14490
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
14491
|
+
}
|
|
14492
|
+
return hash.toString(36);
|
|
14493
|
+
}
|
|
14494
|
+
catch (error) {
|
|
14495
|
+
// 如果计算失败,使用时间戳作为后备(强制刷新)
|
|
14496
|
+
if (!this.options.silentMode) {
|
|
14497
|
+
console.warn('📸 计算内容哈希失败,使用时间戳:', error);
|
|
14498
|
+
}
|
|
14499
|
+
return Date.now().toString();
|
|
14500
|
+
}
|
|
14501
|
+
}
|
|
14429
14502
|
/**
|
|
14430
14503
|
* 设置消息监听
|
|
14431
14504
|
*/
|
|
@@ -14464,75 +14537,76 @@ class ScreenshotManager {
|
|
|
14464
14537
|
if (!event.data || event.data.type !== 'checkScreenshot') {
|
|
14465
14538
|
return;
|
|
14466
14539
|
}
|
|
14540
|
+
// 如果提供了发送消息的回调,保存它(用于后续发送二进制数据)
|
|
14541
|
+
// 注意:消息来源验证在 setupMessageListener 中处理
|
|
14467
14542
|
if (!this.options.silentMode) {
|
|
14468
14543
|
console.log('📸 [iframe] 收到消息:', event.data);
|
|
14469
14544
|
}
|
|
14470
|
-
//
|
|
14471
|
-
const
|
|
14472
|
-
if (
|
|
14473
|
-
|
|
14474
|
-
this.
|
|
14475
|
-
|
|
14476
|
-
|
|
14477
|
-
|
|
14478
|
-
|
|
14479
|
-
|
|
14480
|
-
|
|
14481
|
-
|
|
14482
|
-
|
|
14483
|
-
|
|
14484
|
-
|
|
14485
|
-
|
|
14545
|
+
// 尝试解析为二进制配置(新格式)
|
|
14546
|
+
const binaryConfig = this.parseBinaryConfig(event.data.data);
|
|
14547
|
+
if (binaryConfig) {
|
|
14548
|
+
// 二进制配置
|
|
14549
|
+
this.currentBinaryConfig = binaryConfig;
|
|
14550
|
+
// 根据 ttl 判断是否开启截图功能
|
|
14551
|
+
const currentTime = Date.now();
|
|
14552
|
+
const isValid = binaryConfig.ttl > 0 && binaryConfig.ttl > currentTime;
|
|
14553
|
+
if (isValid) {
|
|
14554
|
+
// 启用截图功能
|
|
14555
|
+
if (!this.isEnabled) {
|
|
14556
|
+
if (!this.options.silentMode) {
|
|
14557
|
+
console.log('📸 [iframe] 启用截图功能(二进制模式)');
|
|
14558
|
+
}
|
|
14559
|
+
this.isEnabled = true;
|
|
14560
|
+
}
|
|
14561
|
+
// 设置动态轮询间隔(使用配置中的 duration)
|
|
14562
|
+
this.dynamicInterval = binaryConfig.duration || this.options.interval;
|
|
14563
|
+
// 计算剩余有效时间(毫秒)
|
|
14564
|
+
const remainingTime = binaryConfig.ttl - currentTime;
|
|
14565
|
+
// 启动或更新截图轮询
|
|
14486
14566
|
if (!this.options.silentMode) {
|
|
14487
|
-
|
|
14567
|
+
const remainingMinutes = Math.ceil(remainingTime / 60000);
|
|
14568
|
+
console.log(`📸 [iframe] 设置轮询间隔: ${this.dynamicInterval}ms,剩余有效时间: ${remainingMinutes}分钟`);
|
|
14488
14569
|
}
|
|
14489
|
-
|
|
14490
|
-
|
|
14491
|
-
|
|
14492
|
-
|
|
14493
|
-
|
|
14494
|
-
|
|
14495
|
-
|
|
14496
|
-
|
|
14497
|
-
|
|
14498
|
-
|
|
14499
|
-
|
|
14500
|
-
|
|
14501
|
-
|
|
14502
|
-
|
|
14503
|
-
|
|
14504
|
-
|
|
14505
|
-
this.expirationTimer = null;
|
|
14570
|
+
// 先执行一次截图,等待完成后再发送二进制数据
|
|
14571
|
+
this.takeScreenshotAndSendBinary(binaryConfig);
|
|
14572
|
+
// 设置过期定时器
|
|
14573
|
+
if (this.expirationTimer) {
|
|
14574
|
+
clearTimeout(this.expirationTimer);
|
|
14575
|
+
this.expirationTimer = null;
|
|
14576
|
+
}
|
|
14577
|
+
this.expirationTimer = setTimeout(() => {
|
|
14578
|
+
if (!this.options.silentMode) {
|
|
14579
|
+
console.log('📸 [iframe] 二进制配置已过期,停止截图');
|
|
14580
|
+
}
|
|
14581
|
+
this.stopScreenshot();
|
|
14582
|
+
this.isEnabled = false;
|
|
14583
|
+
this.currentBinaryConfig = null;
|
|
14584
|
+
this.expirationTimer = null;
|
|
14585
|
+
}, remainingTime);
|
|
14506
14586
|
}
|
|
14507
|
-
|
|
14587
|
+
else {
|
|
14588
|
+
// 禁用截图功能(ttl == 0 或已过期)
|
|
14508
14589
|
if (!this.options.silentMode) {
|
|
14509
|
-
|
|
14590
|
+
if (binaryConfig.ttl === 0) {
|
|
14591
|
+
console.log('📸 [iframe] ttl == 0,禁用截图功能');
|
|
14592
|
+
}
|
|
14593
|
+
else {
|
|
14594
|
+
console.log('📸 [iframe] ttl 已过期,禁用截图功能');
|
|
14595
|
+
}
|
|
14510
14596
|
}
|
|
14511
14597
|
this.stopScreenshot();
|
|
14512
14598
|
this.isEnabled = false;
|
|
14513
|
-
this.
|
|
14514
|
-
this.expirationTimer
|
|
14515
|
-
|
|
14516
|
-
|
|
14517
|
-
else {
|
|
14518
|
-
// 禁用截图功能(ttl == 0 或已过期)
|
|
14519
|
-
if (!this.options.silentMode) {
|
|
14520
|
-
if (config.ttl === 0) {
|
|
14521
|
-
console.log('📸 [iframe] ttl == 0,禁用截图功能');
|
|
14522
|
-
}
|
|
14523
|
-
else {
|
|
14524
|
-
console.log('📸 [iframe] ttl 已过期,禁用截图功能');
|
|
14599
|
+
this.currentBinaryConfig = null;
|
|
14600
|
+
if (this.expirationTimer) {
|
|
14601
|
+
clearTimeout(this.expirationTimer);
|
|
14602
|
+
this.expirationTimer = null;
|
|
14525
14603
|
}
|
|
14526
14604
|
}
|
|
14527
|
-
this.stopScreenshot();
|
|
14528
|
-
this.isEnabled = false;
|
|
14529
|
-
this.currentUploadConfig = null;
|
|
14530
|
-
if (this.expirationTimer) {
|
|
14531
|
-
clearTimeout(this.expirationTimer);
|
|
14532
|
-
this.expirationTimer = null;
|
|
14533
|
-
}
|
|
14534
14605
|
return;
|
|
14535
14606
|
}
|
|
14607
|
+
// 如果不是二进制配置格式,记录错误
|
|
14608
|
+
console.error('📸 [iframe] 解析配置失败:未识别的配置格式');
|
|
14609
|
+
this.uploadError = '解析配置失败:仅支持二进制配置格式';
|
|
14536
14610
|
}
|
|
14537
14611
|
catch (error) {
|
|
14538
14612
|
console.error('📸 [iframe] 处理消息失败:', error);
|
|
@@ -14540,90 +14614,33 @@ class ScreenshotManager {
|
|
|
14540
14614
|
}
|
|
14541
14615
|
}
|
|
14542
14616
|
/**
|
|
14543
|
-
*
|
|
14544
|
-
*/
|
|
14545
|
-
async takeScreenshotAndUpload(config) {
|
|
14546
|
-
// 如果已经在运行,先停止再重新开始(更新间隔)
|
|
14547
|
-
if (this.isRunning) {
|
|
14548
|
-
if (!this.options.silentMode) {
|
|
14549
|
-
console.log(`📸 更新轮询间隔: ${this.dynamicInterval || this.options.interval}ms`);
|
|
14550
|
-
}
|
|
14551
|
-
this.stopScreenshot();
|
|
14552
|
-
}
|
|
14553
|
-
// 启动轮询
|
|
14554
|
-
this.startScreenshot(this.dynamicInterval || config.duration || this.options.interval);
|
|
14555
|
-
// 等待第一次截图完成
|
|
14556
|
-
try {
|
|
14557
|
-
const success = await this.takeScreenshot();
|
|
14558
|
-
if (success) {
|
|
14559
|
-
// 截图完成后,等待一小段时间确保数据已保存
|
|
14560
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
14561
|
-
// 获取最新截图并上传
|
|
14562
|
-
const latestScreenshot = this.getLatestScreenshot();
|
|
14563
|
-
if (latestScreenshot) {
|
|
14564
|
-
// 执行上传
|
|
14565
|
-
this.isUploading = true;
|
|
14566
|
-
this.uploadError = null;
|
|
14567
|
-
this.uploadScreenshot(latestScreenshot, config)
|
|
14568
|
-
.then((success) => {
|
|
14569
|
-
if (success) {
|
|
14570
|
-
if (!this.options.silentMode) {
|
|
14571
|
-
console.log('📸 [iframe] ✅ 截图上传成功');
|
|
14572
|
-
}
|
|
14573
|
-
}
|
|
14574
|
-
else {
|
|
14575
|
-
console.error('📸 [iframe] ❌ 截图上传失败');
|
|
14576
|
-
}
|
|
14577
|
-
})
|
|
14578
|
-
.catch((error) => {
|
|
14579
|
-
console.error('📸 [iframe] ❌ 上传异常:', error);
|
|
14580
|
-
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
14581
|
-
})
|
|
14582
|
-
.finally(() => {
|
|
14583
|
-
this.isUploading = false;
|
|
14584
|
-
});
|
|
14585
|
-
}
|
|
14586
|
-
else {
|
|
14587
|
-
if (!this.options.silentMode) {
|
|
14588
|
-
console.warn('📸 [iframe] 截图完成但未找到截图数据');
|
|
14589
|
-
}
|
|
14590
|
-
}
|
|
14591
|
-
}
|
|
14592
|
-
}
|
|
14593
|
-
catch (error) {
|
|
14594
|
-
console.error('📸 [iframe] 截图失败:', error);
|
|
14595
|
-
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
14596
|
-
}
|
|
14597
|
-
}
|
|
14598
|
-
/**
|
|
14599
|
-
* 解析上传配置
|
|
14617
|
+
* 解析二进制配置
|
|
14600
14618
|
*/
|
|
14601
|
-
|
|
14619
|
+
parseBinaryConfig(data) {
|
|
14602
14620
|
try {
|
|
14603
14621
|
const configStr = typeof data === 'string' ? data : JSON.stringify(data);
|
|
14604
14622
|
const config = JSON.parse(configStr);
|
|
14605
|
-
|
|
14606
|
-
|
|
14607
|
-
|
|
14608
|
-
|
|
14609
|
-
|
|
14610
|
-
|
|
14611
|
-
|
|
14612
|
-
|
|
14613
|
-
|
|
14614
|
-
|
|
14615
|
-
|
|
14616
|
-
|
|
14617
|
-
config.
|
|
14618
|
-
|
|
14619
|
-
|
|
14620
|
-
|
|
14621
|
-
}
|
|
14623
|
+
// 检查是否包含二进制配置所需的字段
|
|
14624
|
+
if (typeof config.sign === 'number' &&
|
|
14625
|
+
typeof config.type === 'number' &&
|
|
14626
|
+
typeof config.topic === 'string' &&
|
|
14627
|
+
typeof config.routingKey === 'string' &&
|
|
14628
|
+
typeof config.ttl === 'number') {
|
|
14629
|
+
return {
|
|
14630
|
+
sign: config.sign,
|
|
14631
|
+
type: config.type,
|
|
14632
|
+
topic: config.topic,
|
|
14633
|
+
routingKey: config.routingKey,
|
|
14634
|
+
ttl: config.ttl,
|
|
14635
|
+
duration: typeof config.duration === 'number' && config.duration > 0
|
|
14636
|
+
? config.duration
|
|
14637
|
+
: this.options.interval // 如果没有提供或无效,使用默认间隔
|
|
14638
|
+
};
|
|
14622
14639
|
}
|
|
14623
|
-
return
|
|
14640
|
+
return null;
|
|
14624
14641
|
}
|
|
14625
14642
|
catch (error) {
|
|
14626
|
-
|
|
14643
|
+
// 不是二进制格式,返回 null
|
|
14627
14644
|
return null;
|
|
14628
14645
|
}
|
|
14629
14646
|
}
|
|
@@ -14656,14 +14673,32 @@ class ScreenshotManager {
|
|
|
14656
14673
|
if (this.isRunning && this.isEnabled && !document.hidden) {
|
|
14657
14674
|
try {
|
|
14658
14675
|
await this.takeScreenshot();
|
|
14659
|
-
//
|
|
14660
|
-
if (this.
|
|
14676
|
+
// 如果配置了二进制模式,发送二进制数据
|
|
14677
|
+
if (this.currentBinaryConfig) {
|
|
14661
14678
|
const latestScreenshot = this.getLatestScreenshot();
|
|
14662
|
-
if (latestScreenshot
|
|
14663
|
-
|
|
14664
|
-
|
|
14665
|
-
|
|
14666
|
-
|
|
14679
|
+
if (latestScreenshot) {
|
|
14680
|
+
try {
|
|
14681
|
+
// 将截图转换为 ArrayBuffer
|
|
14682
|
+
const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
|
|
14683
|
+
// 构建配置的二进制结构
|
|
14684
|
+
const configBuffer = this.buildBinaryConfig(this.currentBinaryConfig);
|
|
14685
|
+
// 合并配置字节和图片字节(配置在前)
|
|
14686
|
+
const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
|
|
14687
|
+
// 发送二进制数据到 iframe
|
|
14688
|
+
if (this.sendToIframeCallback) {
|
|
14689
|
+
const message = {
|
|
14690
|
+
type: 'screenshotBinary',
|
|
14691
|
+
data: combinedBuffer
|
|
14692
|
+
};
|
|
14693
|
+
this.sendToIframeCallback(message);
|
|
14694
|
+
if (!this.options.silentMode) {
|
|
14695
|
+
console.log('📸 [轮询] ✅ 二进制数据已发送到 iframe');
|
|
14696
|
+
}
|
|
14697
|
+
}
|
|
14698
|
+
}
|
|
14699
|
+
catch (error) {
|
|
14700
|
+
console.error('📸 [轮询] ❌ 处理二进制数据失败:', error);
|
|
14701
|
+
}
|
|
14667
14702
|
}
|
|
14668
14703
|
}
|
|
14669
14704
|
}
|
|
@@ -15640,7 +15675,8 @@ class ScreenshotManager {
|
|
|
15640
15675
|
contextOptions.scale = 0.7;
|
|
15641
15676
|
}
|
|
15642
15677
|
// 优化:复用 context,避免频繁创建和销毁(性能提升 20%+)
|
|
15643
|
-
//
|
|
15678
|
+
// 只在元素变化、配置变化或内容变化时重新创建 context
|
|
15679
|
+
// 1. 计算配置哈希
|
|
15644
15680
|
const contextOptionsHash = JSON.stringify({
|
|
15645
15681
|
workerNumber,
|
|
15646
15682
|
quality: finalQuality,
|
|
@@ -15650,13 +15686,36 @@ class ScreenshotManager {
|
|
|
15650
15686
|
maximumCanvasSize: contextOptions.maximumCanvasSize,
|
|
15651
15687
|
timeout: contextOptions.timeout
|
|
15652
15688
|
});
|
|
15689
|
+
// 2. 计算 DOM 内容哈希(检测内容变化)
|
|
15690
|
+
// 通过检测图片 URL、文本内容等来判断内容是否变化
|
|
15691
|
+
// 注意:modern-screenshot 的 context 在创建时会"快照" DOM 状态
|
|
15692
|
+
// 如果 DOM 内容变化了,必须重新创建 context 才能捕获最新内容
|
|
15693
|
+
const contentHash = this.calculateContentHash(element);
|
|
15694
|
+
// 3. 检查 context 是否过期(超过最大存活时间)
|
|
15695
|
+
// 缩短过期时间,确保频繁变化的内容能及时更新
|
|
15696
|
+
const now = Date.now();
|
|
15697
|
+
const isContextExpired = this.contextLastUpdateTime > 0 &&
|
|
15698
|
+
(now - this.contextLastUpdateTime) > this.contextMaxAge;
|
|
15699
|
+
// 4. 判断是否需要重新创建 context
|
|
15700
|
+
// 关键:如果内容哈希变化,必须重新创建 context(modern-screenshot 的限制)
|
|
15653
15701
|
const needsRecreateContext = !this.screenshotContext ||
|
|
15654
15702
|
this.contextElement !== element ||
|
|
15655
|
-
this.contextOptionsHash !== contextOptionsHash
|
|
15703
|
+
this.contextOptionsHash !== contextOptionsHash ||
|
|
15704
|
+
this.contextContentHash !== contentHash || // 内容变化时强制重新创建
|
|
15705
|
+
isContextExpired;
|
|
15656
15706
|
if (needsRecreateContext) {
|
|
15657
15707
|
if (!this.options.silentMode) {
|
|
15658
15708
|
if (this.screenshotContext) {
|
|
15659
|
-
|
|
15709
|
+
let reason = '检测到';
|
|
15710
|
+
if (this.contextElement !== element)
|
|
15711
|
+
reason += '元素变化';
|
|
15712
|
+
if (this.contextOptionsHash !== contextOptionsHash)
|
|
15713
|
+
reason += '配置变化';
|
|
15714
|
+
if (this.contextContentHash !== contentHash)
|
|
15715
|
+
reason += '内容变化';
|
|
15716
|
+
if (isContextExpired)
|
|
15717
|
+
reason += 'context 过期';
|
|
15718
|
+
console.log(`📸 ${reason},重新创建 context...`);
|
|
15660
15719
|
}
|
|
15661
15720
|
else {
|
|
15662
15721
|
console.log(`📸 Worker 模式: ${workerNumber} 个 Worker,质量: ${finalQuality.toFixed(2)},缩放: ${contextOptions.scale || 1}`);
|
|
@@ -15688,9 +15747,33 @@ class ScreenshotManager {
|
|
|
15688
15747
|
const maxRetries = this.options.maxRetries || 2;
|
|
15689
15748
|
while (retries <= maxRetries) {
|
|
15690
15749
|
try {
|
|
15750
|
+
// 等待图片加载完成(确保内容是最新的)
|
|
15751
|
+
await this.waitForImagesToLoad(element);
|
|
15752
|
+
// 等待 DOM 更新完成(确保内容渲染完成)
|
|
15753
|
+
// 使用双重 requestAnimationFrame + setTimeout 确保内容完全渲染
|
|
15754
|
+
await new Promise(resolve => {
|
|
15755
|
+
requestAnimationFrame(() => {
|
|
15756
|
+
requestAnimationFrame(() => {
|
|
15757
|
+
// 根据截图间隔调整等待时间:频繁截图时等待更久
|
|
15758
|
+
const waitTime = this.options.interval < 2000 ? 200 : 100;
|
|
15759
|
+
setTimeout(resolve, waitTime);
|
|
15760
|
+
});
|
|
15761
|
+
});
|
|
15762
|
+
});
|
|
15763
|
+
// 创建 context 前,再次检查内容是否变化(防止在等待期间内容又变化了)
|
|
15764
|
+
const latestContentHash = this.calculateContentHash(element);
|
|
15765
|
+
if (latestContentHash !== contentHash) {
|
|
15766
|
+
if (!this.options.silentMode) {
|
|
15767
|
+
console.log('📸 等待期间内容发生变化,更新内容哈希');
|
|
15768
|
+
}
|
|
15769
|
+
// 更新 contentHash,但继续使用新的 context
|
|
15770
|
+
// 这样下次截图时会检测到变化
|
|
15771
|
+
}
|
|
15691
15772
|
this.screenshotContext = await createContext$1(element, contextOptions);
|
|
15692
15773
|
this.contextElement = element;
|
|
15693
15774
|
this.contextOptionsHash = contextOptionsHash;
|
|
15775
|
+
this.contextContentHash = contentHash;
|
|
15776
|
+
this.contextLastUpdateTime = now;
|
|
15694
15777
|
break;
|
|
15695
15778
|
}
|
|
15696
15779
|
catch (error) {
|
|
@@ -15710,6 +15793,74 @@ class ScreenshotManager {
|
|
|
15710
15793
|
if (!this.options.silentMode) {
|
|
15711
15794
|
console.log('📸 复用现有 context(性能优化)');
|
|
15712
15795
|
}
|
|
15796
|
+
// ⚠️ 重要:modern-screenshot 的 context 在创建时会"快照" DOM 状态
|
|
15797
|
+
// 如果 DOM 内容在 context 创建后发生了变化,复用 context 会捕获到旧内容
|
|
15798
|
+
// 因此,我们需要在每次截图前再次检查内容是否变化
|
|
15799
|
+
// 再次计算内容哈希,检查是否在复用期间内容又变化了
|
|
15800
|
+
const latestContentHash = this.calculateContentHash(element);
|
|
15801
|
+
if (latestContentHash !== this.contextContentHash) {
|
|
15802
|
+
// 内容在复用期间又变化了,必须重新创建 context
|
|
15803
|
+
if (!this.options.silentMode) {
|
|
15804
|
+
console.log('📸 ⚠️ 复用期间检测到内容变化,强制重新创建 context');
|
|
15805
|
+
}
|
|
15806
|
+
// 销毁旧 context
|
|
15807
|
+
if (this.screenshotContext) {
|
|
15808
|
+
try {
|
|
15809
|
+
destroyContext(this.screenshotContext);
|
|
15810
|
+
}
|
|
15811
|
+
catch (e) {
|
|
15812
|
+
// 忽略清理错误
|
|
15813
|
+
}
|
|
15814
|
+
this.screenshotContext = null;
|
|
15815
|
+
}
|
|
15816
|
+
// 等待图片加载和 DOM 更新
|
|
15817
|
+
await this.waitForImagesToLoad(element);
|
|
15818
|
+
await new Promise(resolve => {
|
|
15819
|
+
requestAnimationFrame(() => {
|
|
15820
|
+
requestAnimationFrame(() => {
|
|
15821
|
+
const waitTime = this.options.interval < 2000 ? 200 : 100;
|
|
15822
|
+
setTimeout(resolve, waitTime);
|
|
15823
|
+
});
|
|
15824
|
+
});
|
|
15825
|
+
});
|
|
15826
|
+
// 重新创建 context
|
|
15827
|
+
let retries = 0;
|
|
15828
|
+
const maxRetries = this.options.maxRetries || 2;
|
|
15829
|
+
while (retries <= maxRetries) {
|
|
15830
|
+
try {
|
|
15831
|
+
this.screenshotContext = await createContext$1(element, contextOptions);
|
|
15832
|
+
this.contextElement = element;
|
|
15833
|
+
this.contextOptionsHash = contextOptionsHash;
|
|
15834
|
+
this.contextContentHash = latestContentHash;
|
|
15835
|
+
this.contextLastUpdateTime = Date.now();
|
|
15836
|
+
break;
|
|
15837
|
+
}
|
|
15838
|
+
catch (error) {
|
|
15839
|
+
if (retries === maxRetries) {
|
|
15840
|
+
throw new Error(`重新创建截图上下文失败(已重试 ${maxRetries} 次): ${error instanceof Error ? error.message : String(error)}`);
|
|
15841
|
+
}
|
|
15842
|
+
retries++;
|
|
15843
|
+
const delay = 1000 * retries;
|
|
15844
|
+
if (!this.options.silentMode) {
|
|
15845
|
+
console.warn(`📸 ⚠️ 重新创建截图上下文失败,${delay}ms 后重试 (${retries}/${maxRetries})...`);
|
|
15846
|
+
}
|
|
15847
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
15848
|
+
}
|
|
15849
|
+
}
|
|
15850
|
+
}
|
|
15851
|
+
else {
|
|
15852
|
+
// 内容没有变化,可以安全复用 context
|
|
15853
|
+
// 但还是要等待图片加载完成,确保内容是最新的
|
|
15854
|
+
await this.waitForImagesToLoad(element);
|
|
15855
|
+
// 等待 DOM 更新完成
|
|
15856
|
+
await new Promise(resolve => {
|
|
15857
|
+
requestAnimationFrame(() => {
|
|
15858
|
+
requestAnimationFrame(() => {
|
|
15859
|
+
setTimeout(resolve, 100); // 额外等待 100ms,确保内容完全渲染
|
|
15860
|
+
});
|
|
15861
|
+
});
|
|
15862
|
+
});
|
|
15863
|
+
}
|
|
15713
15864
|
}
|
|
15714
15865
|
try {
|
|
15715
15866
|
// 根据输出格式选择对应的 API,避免格式转换(性能优化)
|
|
@@ -16605,60 +16756,121 @@ class ScreenshotManager {
|
|
|
16605
16756
|
}
|
|
16606
16757
|
}
|
|
16607
16758
|
/**
|
|
16608
|
-
*
|
|
16759
|
+
* 将 base64 data URL 转换为 ArrayBuffer
|
|
16609
16760
|
*/
|
|
16610
|
-
|
|
16611
|
-
|
|
16761
|
+
dataUrlToArrayBuffer(dataUrl) {
|
|
16762
|
+
const arr = dataUrl.split(',');
|
|
16763
|
+
const bstr = atob(arr[1]);
|
|
16764
|
+
const n = bstr.length;
|
|
16765
|
+
const u8arr = new Uint8Array(n);
|
|
16766
|
+
for (let i = 0; i < n; i++) {
|
|
16767
|
+
u8arr[i] = bstr.charCodeAt(i);
|
|
16768
|
+
}
|
|
16769
|
+
return u8arr.buffer;
|
|
16770
|
+
}
|
|
16771
|
+
/**
|
|
16772
|
+
* 构建二进制结构(按顺序:sign, type, topic, routingKey)
|
|
16773
|
+
* sign: 8字节 (BigInt64)
|
|
16774
|
+
* type: 1字节 (Uint8)
|
|
16775
|
+
* topic: 8字节 (字符串,UTF-8编码,不足补0)
|
|
16776
|
+
* routingKey: 8字节 (字符串,UTF-8编码,不足补0)
|
|
16777
|
+
*/
|
|
16778
|
+
buildBinaryConfig(config) {
|
|
16779
|
+
// 总大小:8 + 1 + 8 + 8 = 25 字节
|
|
16780
|
+
const buffer = new ArrayBuffer(25);
|
|
16781
|
+
const view = new DataView(buffer);
|
|
16782
|
+
const encoder = new TextEncoder();
|
|
16783
|
+
let offset = 0;
|
|
16784
|
+
// sign: 8字节 (BigInt64)
|
|
16785
|
+
view.setBigInt64(offset, BigInt(config.sign), true); // little-endian
|
|
16786
|
+
offset += 8;
|
|
16787
|
+
// type: 1字节 (Uint8)
|
|
16788
|
+
view.setUint8(offset, config.type);
|
|
16789
|
+
offset += 1;
|
|
16790
|
+
// topic: 8字节 (字符串,UTF-8编码,不足补0)
|
|
16791
|
+
const topicBytes = encoder.encode(config.topic);
|
|
16792
|
+
const topicArray = new Uint8Array(buffer, offset, 8);
|
|
16793
|
+
topicArray.set(topicBytes.slice(0, 8));
|
|
16794
|
+
offset += 8;
|
|
16795
|
+
// routingKey: 8字节 (字符串,UTF-8编码,不足补0)
|
|
16796
|
+
const routingKeyBytes = encoder.encode(config.routingKey);
|
|
16797
|
+
const routingKeyArray = new Uint8Array(buffer, offset, 8);
|
|
16798
|
+
routingKeyArray.set(routingKeyBytes.slice(0, 8));
|
|
16799
|
+
return buffer;
|
|
16800
|
+
}
|
|
16801
|
+
/**
|
|
16802
|
+
* 将配置字节和图片字节合并
|
|
16803
|
+
*/
|
|
16804
|
+
combineBinaryData(configBuffer, imageBuffer) {
|
|
16805
|
+
const totalLength = configBuffer.byteLength + imageBuffer.byteLength;
|
|
16806
|
+
const combined = new ArrayBuffer(totalLength);
|
|
16807
|
+
const combinedView = new Uint8Array(combined);
|
|
16808
|
+
// 先放配置字节
|
|
16809
|
+
combinedView.set(new Uint8Array(configBuffer), 0);
|
|
16810
|
+
// 再放图片字节
|
|
16811
|
+
combinedView.set(new Uint8Array(imageBuffer), configBuffer.byteLength);
|
|
16812
|
+
return combined;
|
|
16813
|
+
}
|
|
16814
|
+
/**
|
|
16815
|
+
* 执行截图并发送二进制数据到 iframe
|
|
16816
|
+
*/
|
|
16817
|
+
async takeScreenshotAndSendBinary(config) {
|
|
16818
|
+
// 如果已经在运行,先停止再重新开始
|
|
16819
|
+
if (this.isRunning) {
|
|
16612
16820
|
if (!this.options.silentMode) {
|
|
16613
|
-
console.log(
|
|
16614
|
-
}
|
|
16615
|
-
|
|
16616
|
-
|
|
16617
|
-
|
|
16618
|
-
|
|
16619
|
-
|
|
16620
|
-
|
|
16621
|
-
|
|
16821
|
+
console.log(`📸 更新轮询间隔: ${this.dynamicInterval || this.options.interval}ms`);
|
|
16822
|
+
}
|
|
16823
|
+
this.stopScreenshot();
|
|
16824
|
+
}
|
|
16825
|
+
// 启动轮询
|
|
16826
|
+
this.startScreenshot(this.dynamicInterval || this.options.interval);
|
|
16827
|
+
// 等待第一次截图完成
|
|
16828
|
+
try {
|
|
16829
|
+
const success = await this.takeScreenshot();
|
|
16830
|
+
if (success) {
|
|
16831
|
+
// 截图完成后,等待一小段时间确保数据已保存
|
|
16832
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
16833
|
+
// 获取最新截图并转换为二进制
|
|
16834
|
+
const latestScreenshot = this.getLatestScreenshot();
|
|
16835
|
+
if (latestScreenshot) {
|
|
16836
|
+
try {
|
|
16837
|
+
// 将截图转换为 ArrayBuffer
|
|
16838
|
+
const imageBuffer = this.dataUrlToArrayBuffer(latestScreenshot);
|
|
16839
|
+
// 构建配置的二进制结构
|
|
16840
|
+
const configBuffer = this.buildBinaryConfig(config);
|
|
16841
|
+
// 合并配置字节和图片字节(配置在前)
|
|
16842
|
+
const combinedBuffer = this.combineBinaryData(configBuffer, imageBuffer);
|
|
16843
|
+
// 发送二进制数据到 iframe
|
|
16844
|
+
if (this.sendToIframeCallback) {
|
|
16845
|
+
const message = {
|
|
16846
|
+
type: 'screenshotBinary',
|
|
16847
|
+
data: combinedBuffer
|
|
16848
|
+
};
|
|
16849
|
+
this.sendToIframeCallback(message);
|
|
16850
|
+
if (!this.options.silentMode) {
|
|
16851
|
+
console.log('📸 [iframe] ✅ 二进制数据已发送到 iframe');
|
|
16852
|
+
}
|
|
16853
|
+
}
|
|
16854
|
+
else {
|
|
16855
|
+
console.error('📸 [iframe] ❌ 无法发送二进制数据:未提供发送消息的回调函数');
|
|
16856
|
+
}
|
|
16857
|
+
}
|
|
16858
|
+
catch (error) {
|
|
16859
|
+
console.error('📸 [iframe] ❌ 处理二进制数据失败:', error);
|
|
16860
|
+
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
16861
|
+
}
|
|
16622
16862
|
}
|
|
16623
|
-
|
|
16624
|
-
|
|
16625
|
-
|
|
16626
|
-
|
|
16863
|
+
else {
|
|
16864
|
+
if (!this.options.silentMode) {
|
|
16865
|
+
console.warn('📸 [iframe] 截图完成但未找到截图数据');
|
|
16866
|
+
}
|
|
16627
16867
|
}
|
|
16628
|
-
this.uploadProgress.success++;
|
|
16629
|
-
return true;
|
|
16630
|
-
}
|
|
16631
|
-
else {
|
|
16632
|
-
const errorText = await response.text().catch(() => '');
|
|
16633
|
-
const errorMsg = `上传失败: HTTP ${response.status} ${response.statusText}${errorText ? ` - ${errorText.substring(0, 200)}` : ''}`;
|
|
16634
|
-
console.error('📸 [上传] ❌', errorMsg);
|
|
16635
|
-
this.uploadError = errorMsg;
|
|
16636
|
-
this.uploadProgress.failed++;
|
|
16637
|
-
return false;
|
|
16638
16868
|
}
|
|
16639
16869
|
}
|
|
16640
16870
|
catch (error) {
|
|
16641
|
-
|
|
16642
|
-
|
|
16643
|
-
this.uploadError = `上传异常: ${errorMsg}`;
|
|
16644
|
-
this.uploadProgress.failed++;
|
|
16645
|
-
return false;
|
|
16646
|
-
}
|
|
16647
|
-
}
|
|
16648
|
-
/**
|
|
16649
|
-
* 将 base64 data URL 转换为 Blob
|
|
16650
|
-
*/
|
|
16651
|
-
dataUrlToBlob(dataUrl, contentType) {
|
|
16652
|
-
const arr = dataUrl.split(',');
|
|
16653
|
-
const mimeMatch = arr[0].match(/:(.*?);/);
|
|
16654
|
-
const mime = mimeMatch ? mimeMatch[1] : contentType;
|
|
16655
|
-
const bstr = atob(arr[1]);
|
|
16656
|
-
let n = bstr.length;
|
|
16657
|
-
const u8arr = new Uint8Array(n);
|
|
16658
|
-
while (n--) {
|
|
16659
|
-
u8arr[n] = bstr.charCodeAt(n);
|
|
16871
|
+
console.error('📸 [iframe] 截图失败:', error);
|
|
16872
|
+
this.uploadError = error instanceof Error ? error.message : String(error);
|
|
16660
16873
|
}
|
|
16661
|
-
return new Blob([u8arr], { type: mime });
|
|
16662
16874
|
}
|
|
16663
16875
|
/**
|
|
16664
16876
|
* 获取最新截图
|
|
@@ -16690,6 +16902,8 @@ class ScreenshotManager {
|
|
|
16690
16902
|
this.screenshotContext = null;
|
|
16691
16903
|
this.contextElement = null;
|
|
16692
16904
|
this.contextOptionsHash = '';
|
|
16905
|
+
this.contextContentHash = '';
|
|
16906
|
+
this.contextLastUpdateTime = 0;
|
|
16693
16907
|
}
|
|
16694
16908
|
this.stopScreenshot();
|
|
16695
16909
|
if (this.worker) {
|
|
@@ -16840,10 +17054,8 @@ class ScreenshotManager {
|
|
|
16840
17054
|
lastScreenshotTime: this.lastScreenshotTime,
|
|
16841
17055
|
error: this.error,
|
|
16842
17056
|
isEnabled: this.isEnabled,
|
|
16843
|
-
isUploading: this.isUploading,
|
|
16844
17057
|
uploadError: this.uploadError,
|
|
16845
|
-
|
|
16846
|
-
currentUploadConfig: this.currentUploadConfig
|
|
17058
|
+
currentBinaryConfig: this.currentBinaryConfig
|
|
16847
17059
|
};
|
|
16848
17060
|
}
|
|
16849
17061
|
}
|
|
@@ -20180,7 +20392,11 @@ class CustomerServiceSDK {
|
|
|
20180
20392
|
if (config.screenshot) {
|
|
20181
20393
|
// 默认截图目标为 document.body,可以通过配置自定义
|
|
20182
20394
|
const targetElement = document.body;
|
|
20183
|
-
|
|
20395
|
+
// 传入发送消息到 iframe 的回调函数
|
|
20396
|
+
this.screenshotManager = new ScreenshotManager(targetElement, config.screenshot, (data) => {
|
|
20397
|
+
// 通过 IframeManager 发送消息到 iframe
|
|
20398
|
+
this.iframeManager?.sendToIframe(data);
|
|
20399
|
+
});
|
|
20184
20400
|
// 自动启用截图功能(用于测试,实际使用时需要通过 iframe 消息启用)
|
|
20185
20401
|
this.screenshotManager.enable(true);
|
|
20186
20402
|
console.log('CustomerSDK screenshot manager initialized and enabled');
|