customer-chat-sdk 1.0.25 → 1.0.27
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 +4 -3
- package/dist/core/ScreenshotManager.d.ts.map +1 -1
- package/dist/customer-sdk.cjs.js +213 -86
- package/dist/customer-sdk.esm.js +213 -86
- package/dist/customer-sdk.min.js +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/customer-sdk.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { destroyContext, createContext, domToPng } from 'modern-screenshot';
|
|
2
2
|
|
|
3
3
|
// 直接使用base64字符串,避免打包后路径问题
|
|
4
4
|
const iconImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAYAAAA5ZDbSAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAArKSURBVHgB7Z1NbFTXFcf/93mwaGyEabLoIgFTqWRFMZUitSoIu80ial01SFWrKgsGKXYpWdRtFilRK4xUgbKICosmCKIySKCo2UAE6i61XVroDuOoi2YT02RZVFNwQTAzN+e8N88ej9+8j5n3ce5wf5JhPvyY4f3f/9xzz73vXoUeYKish6oljCiFYQ2MaAebVR3D/Jzf1xpDUPTTggIW6feXoOlH0Q9wS9exRK/Pl6qYX6qoJRiOgoEMvqpHWUjlYJ9iQeEJmTb07y6y2CT6HP99/z01C8MwQmB26OMSyuTIH9LTkSA35oLn9FmKCB/WHmP2YUUtQjhiBV4jqsIoJKJJbOC8ZLHFCczhl75UWXvCFuPUDiBXV5TGeWlhXIzALCx9m6Ni3RqfeRL71PJZVYEAChfYdayDc1klSkXBCRr9caxooQsTuIccG4qbiddxsKjQnbvAlDwNVzfgXK8L2wq30ZSMHcs7GctV4IFX9ZTyXGtM8pQq2i2qnCQ3H0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|
|
|
921
921
|
}
|
|
922
922
|
}
|
|
923
923
|
|
|
924
|
-
// @ts-ignore -
|
|
924
|
+
// @ts-ignore - modern-screenshot may not have type definitions
|
|
925
|
+
// Worker URL 将在创建上下文时动态获取
|
|
926
|
+
// 在 Vite 环境中可以使用: import workerUrl from 'modern-screenshot/worker?url'
|
|
927
|
+
// 在 Rollup 中,modern-screenshot 会自动处理 worker URL
|
|
928
|
+
let workerUrl = undefined;
|
|
929
|
+
// 尝试动态获取 worker URL(仅在支持的环境中)
|
|
930
|
+
async function getWorkerUrl() {
|
|
931
|
+
if (workerUrl) {
|
|
932
|
+
return workerUrl;
|
|
933
|
+
}
|
|
934
|
+
try {
|
|
935
|
+
// 尝试使用 Vite 的 ?url 语法(仅在 Vite 环境中有效)
|
|
936
|
+
// @ts-ignore - Vite 特有的 ?url 语法
|
|
937
|
+
const workerModule = await import('modern-screenshot/worker?url');
|
|
938
|
+
workerUrl = workerModule.default || workerModule;
|
|
939
|
+
return workerUrl;
|
|
940
|
+
}
|
|
941
|
+
catch {
|
|
942
|
+
// Rollup 或其他构建工具不支持 ?url 语法
|
|
943
|
+
// modern-screenshot 会自动处理 worker URL,返回 undefined 即可
|
|
944
|
+
return undefined;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
925
947
|
/**
|
|
926
948
|
* 截图管理器
|
|
927
949
|
* 负责页面截图、压缩和上传功能
|
|
@@ -943,6 +965,8 @@ class ScreenshotManager {
|
|
|
943
965
|
// WebWorker 相关
|
|
944
966
|
this.worker = null;
|
|
945
967
|
this.screenshotTimer = null;
|
|
968
|
+
// modern-screenshot Worker 上下文(用于复用,避免频繁创建和销毁)
|
|
969
|
+
this.screenshotContext = null;
|
|
946
970
|
// PostMessage 监听器
|
|
947
971
|
this.messageHandler = null;
|
|
948
972
|
// 动态轮询间隔(由 iframe 消息控制)
|
|
@@ -966,7 +990,7 @@ class ScreenshotManager {
|
|
|
966
990
|
outputFormat: options.outputFormat ?? 'webp',
|
|
967
991
|
enableCORS: options.enableCORS ?? true,
|
|
968
992
|
proxyUrl: options.proxyUrl ?? '',
|
|
969
|
-
engine: options.engine ?? '
|
|
993
|
+
engine: options.engine ?? 'modern-screenshot',
|
|
970
994
|
corsMode: options.corsMode ?? 'canvas-proxy',
|
|
971
995
|
silentMode: options.silentMode ?? false,
|
|
972
996
|
maxRetries: options.maxRetries ?? 2
|
|
@@ -978,6 +1002,16 @@ class ScreenshotManager {
|
|
|
978
1002
|
* 设置目标元素
|
|
979
1003
|
*/
|
|
980
1004
|
setTargetElement(element) {
|
|
1005
|
+
// 如果元素改变了,清理旧的 Worker 上下文
|
|
1006
|
+
if (this.targetElement !== element && this.screenshotContext) {
|
|
1007
|
+
try {
|
|
1008
|
+
destroyContext(this.screenshotContext);
|
|
1009
|
+
}
|
|
1010
|
+
catch (e) {
|
|
1011
|
+
// 忽略清理错误
|
|
1012
|
+
}
|
|
1013
|
+
this.screenshotContext = null;
|
|
1014
|
+
}
|
|
981
1015
|
this.targetElement = element;
|
|
982
1016
|
}
|
|
983
1017
|
/**
|
|
@@ -1266,10 +1300,10 @@ class ScreenshotManager {
|
|
|
1266
1300
|
this.waitForStylesAndFonts(),
|
|
1267
1301
|
this.waitForFonts()
|
|
1268
1302
|
]);
|
|
1269
|
-
// 选择截图引擎(仅支持
|
|
1270
|
-
const selectedEngine = '
|
|
1303
|
+
// 选择截图引擎(仅支持 modern-screenshot)
|
|
1304
|
+
const selectedEngine = 'modern-screenshot';
|
|
1271
1305
|
if (!this.options.silentMode) {
|
|
1272
|
-
console.log(`📸 使用截图引擎: ${selectedEngine}`);
|
|
1306
|
+
console.log(`📸 使用截图引擎: ${selectedEngine} (Worker 模式)`);
|
|
1273
1307
|
}
|
|
1274
1308
|
// 预处理网络图片
|
|
1275
1309
|
if (this.options.enableCORS) {
|
|
@@ -1279,8 +1313,8 @@ class ScreenshotManager {
|
|
|
1279
1313
|
let dataUrl;
|
|
1280
1314
|
// 等待一小段时间,确保 DOM 更新完成
|
|
1281
1315
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
1282
|
-
// 使用
|
|
1283
|
-
dataUrl = await this.
|
|
1316
|
+
// 使用 modern-screenshot 截图(启用 Worker)
|
|
1317
|
+
dataUrl = await this.takeScreenshotWithModernScreenshot(this.targetElement);
|
|
1284
1318
|
const timestamp = Date.now();
|
|
1285
1319
|
// 更新状态
|
|
1286
1320
|
this.screenshotCount++;
|
|
@@ -1345,11 +1379,11 @@ class ScreenshotManager {
|
|
|
1345
1379
|
}
|
|
1346
1380
|
}
|
|
1347
1381
|
/**
|
|
1348
|
-
* 使用
|
|
1382
|
+
* 使用 modern-screenshot 截图(启用 Worker)
|
|
1349
1383
|
*/
|
|
1350
|
-
async
|
|
1384
|
+
async takeScreenshotWithModernScreenshot(element) {
|
|
1351
1385
|
if (!this.options.silentMode) {
|
|
1352
|
-
console.log('📸 使用
|
|
1386
|
+
console.log('📸 使用 modern-screenshot 引擎截图(Worker 模式)...');
|
|
1353
1387
|
}
|
|
1354
1388
|
const { width, height } = this.calculateCompressedSize(element.scrollWidth, element.scrollHeight, this.options.maxWidth, this.options.maxHeight);
|
|
1355
1389
|
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
|
@@ -1357,85 +1391,164 @@ class ScreenshotManager {
|
|
|
1357
1391
|
const mobileQuality = isMobile || isLowEndDevice ? Math.max(this.options.quality * 0.7, 0.3) : this.options.quality;
|
|
1358
1392
|
const mobileWidth = isMobile ? Math.min(width, 1280) : width;
|
|
1359
1393
|
const mobileHeight = isMobile ? Math.min(height, 720) : height;
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
height: mobileHeight,
|
|
1368
|
-
scrollX: 0,
|
|
1369
|
-
scrollY: 0,
|
|
1370
|
-
removeContainer: false,
|
|
1371
|
-
foreignObjectRendering: false,
|
|
1372
|
-
onclone: (clonedDoc) => {
|
|
1373
|
-
const clonedImages = clonedDoc.querySelectorAll('img');
|
|
1374
|
-
clonedImages.forEach((img) => {
|
|
1375
|
-
if (img.src && !img.src.startsWith('data:') && !img.src.startsWith('blob:')) {
|
|
1376
|
-
try {
|
|
1377
|
-
const imgUrl = new URL(img.src, window.location.href);
|
|
1378
|
-
const currentOrigin = window.location.origin;
|
|
1379
|
-
if (this.options.proxyUrl && img.src.includes(this.options.proxyUrl.split('/api/image-proxy')[0])) {
|
|
1380
|
-
return;
|
|
1381
|
-
}
|
|
1382
|
-
if (imgUrl.origin !== currentOrigin) {
|
|
1383
|
-
if (this.options.corsMode === 'canvas-proxy') {
|
|
1384
|
-
const cachedDataUrl = this.imageProxyCache.get(img.src);
|
|
1385
|
-
if (cachedDataUrl) {
|
|
1386
|
-
img.src = cachedDataUrl;
|
|
1387
|
-
}
|
|
1388
|
-
else if (img.crossOrigin) {
|
|
1389
|
-
img.removeAttribute('crossOrigin');
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
else if (!img.crossOrigin) {
|
|
1393
|
-
img.crossOrigin = 'anonymous';
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
catch (e) {
|
|
1398
|
-
// URL 解析失败,跳过
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
});
|
|
1402
|
-
},
|
|
1403
|
-
ignoreElements: (element) => {
|
|
1404
|
-
const htmlElement = element;
|
|
1405
|
-
return element.classList.contains('van-popup') ||
|
|
1406
|
-
element.classList.contains('van-overlay') ||
|
|
1407
|
-
element.classList.contains('van-toast') ||
|
|
1408
|
-
element.classList.contains('van-dialog') ||
|
|
1409
|
-
element.classList.contains('van-loading') ||
|
|
1410
|
-
htmlElement.style.display === 'none' ||
|
|
1411
|
-
htmlElement.style.visibility === 'hidden';
|
|
1412
|
-
},
|
|
1413
|
-
imageTimeout: 15000
|
|
1414
|
-
});
|
|
1415
|
-
let mimeType = 'image/jpeg';
|
|
1416
|
-
let finalQuality = mobileQuality;
|
|
1417
|
-
if (this.options.outputFormat === 'webp' && !isMobile) {
|
|
1394
|
+
// 处理跨域图片的函数
|
|
1395
|
+
const handleCrossOriginImage = async (url) => {
|
|
1396
|
+
// 如果是 data URL 或 blob URL,直接返回
|
|
1397
|
+
if (url.startsWith('data:') || url.startsWith('blob:')) {
|
|
1398
|
+
return url;
|
|
1399
|
+
}
|
|
1400
|
+
// 如果是同源图片,直接返回
|
|
1418
1401
|
try {
|
|
1419
|
-
const
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
const testDataUrl = testCanvas.toDataURL('image/webp');
|
|
1423
|
-
if (testDataUrl.indexOf('webp') !== -1) {
|
|
1424
|
-
mimeType = 'image/webp';
|
|
1402
|
+
const imgUrl = new URL(url, window.location.href);
|
|
1403
|
+
if (imgUrl.origin === window.location.origin) {
|
|
1404
|
+
return url;
|
|
1425
1405
|
}
|
|
1426
1406
|
}
|
|
1427
|
-
catch {
|
|
1428
|
-
|
|
1407
|
+
catch (e) {
|
|
1408
|
+
// URL 解析失败,继续处理
|
|
1409
|
+
}
|
|
1410
|
+
// 如果配置了代理服务器,使用代理处理跨域图片
|
|
1411
|
+
if (this.options.proxyUrl && this.options.proxyUrl.trim() !== '') {
|
|
1412
|
+
// 检查缓存
|
|
1413
|
+
if (this.imageProxyCache.has(url)) {
|
|
1414
|
+
return this.imageProxyCache.get(url);
|
|
1415
|
+
}
|
|
1416
|
+
try {
|
|
1417
|
+
// 构建代理请求参数
|
|
1418
|
+
const params = new URLSearchParams({
|
|
1419
|
+
url: url,
|
|
1420
|
+
maxWidth: String(this.options.maxWidth || 1600),
|
|
1421
|
+
maxHeight: String(this.options.maxHeight || 900),
|
|
1422
|
+
quality: String(Math.round((this.options.quality || 0.4) * 100)),
|
|
1423
|
+
format: this.options.outputFormat || 'webp'
|
|
1424
|
+
});
|
|
1425
|
+
let baseUrl = this.options.proxyUrl;
|
|
1426
|
+
baseUrl = baseUrl.replace(/[?&]$/, '');
|
|
1427
|
+
const proxyUrl = `${baseUrl}?${params.toString()}`;
|
|
1428
|
+
// 请求代理服务器
|
|
1429
|
+
const response = await fetch(proxyUrl, {
|
|
1430
|
+
method: 'GET',
|
|
1431
|
+
mode: 'cors',
|
|
1432
|
+
credentials: 'omit',
|
|
1433
|
+
headers: {
|
|
1434
|
+
'Accept': 'image/*'
|
|
1435
|
+
},
|
|
1436
|
+
cache: 'no-cache'
|
|
1437
|
+
});
|
|
1438
|
+
if (!response.ok) {
|
|
1439
|
+
throw new Error(`代理请求失败: ${response.status}`);
|
|
1440
|
+
}
|
|
1441
|
+
const blob = await response.blob();
|
|
1442
|
+
const dataUrl = await this.blobToDataUrl(blob);
|
|
1443
|
+
// 缓存结果
|
|
1444
|
+
this.imageProxyCache.set(url, dataUrl);
|
|
1445
|
+
return dataUrl;
|
|
1446
|
+
}
|
|
1447
|
+
catch (error) {
|
|
1448
|
+
if (!this.options.silentMode) {
|
|
1449
|
+
console.warn(`📸 代理处理图片失败: ${url.substring(0, 100)}...`, error);
|
|
1450
|
+
}
|
|
1451
|
+
// 失败时返回原 URL
|
|
1452
|
+
return url;
|
|
1453
|
+
}
|
|
1429
1454
|
}
|
|
1455
|
+
// 如果没有配置代理,尝试使用 CORS
|
|
1456
|
+
if (this.options.enableCORS) {
|
|
1457
|
+
return url;
|
|
1458
|
+
}
|
|
1459
|
+
// 默认返回原 URL
|
|
1460
|
+
return url;
|
|
1461
|
+
};
|
|
1462
|
+
// 如果还没有创建 Worker 上下文,则创建
|
|
1463
|
+
if (!this.screenshotContext) {
|
|
1464
|
+
const workerNumber = isMobile || isLowEndDevice ? 1 : 2;
|
|
1465
|
+
// 构建 createContext 配置
|
|
1466
|
+
const contextOptions = {
|
|
1467
|
+
workerNumber,
|
|
1468
|
+
quality: mobileQuality,
|
|
1469
|
+
fetchFn: handleCrossOriginImage, // 使用代理服务器处理跨域图片
|
|
1470
|
+
fetch: {
|
|
1471
|
+
requestInit: {
|
|
1472
|
+
cache: 'no-cache',
|
|
1473
|
+
},
|
|
1474
|
+
bypassingCache: true,
|
|
1475
|
+
},
|
|
1476
|
+
};
|
|
1477
|
+
// 如果指定了尺寸,添加尺寸配置
|
|
1478
|
+
if (mobileWidth && mobileHeight) {
|
|
1479
|
+
contextOptions.width = mobileWidth;
|
|
1480
|
+
contextOptions.height = mobileHeight;
|
|
1481
|
+
}
|
|
1482
|
+
// 如果指定了缩放比例,添加缩放配置
|
|
1483
|
+
if (this.options.scale !== 1) {
|
|
1484
|
+
contextOptions.scale = isMobile ? 0.8 : this.options.scale;
|
|
1485
|
+
}
|
|
1486
|
+
// 尝试设置 workerUrl(如果可用)
|
|
1487
|
+
const resolvedWorkerUrl = await getWorkerUrl();
|
|
1488
|
+
if (resolvedWorkerUrl) {
|
|
1489
|
+
contextOptions.workerUrl = resolvedWorkerUrl;
|
|
1490
|
+
}
|
|
1491
|
+
// 创建 Worker 上下文
|
|
1492
|
+
this.screenshotContext = await createContext(element, contextOptions);
|
|
1493
|
+
}
|
|
1494
|
+
try {
|
|
1495
|
+
// 使用 Worker 上下文进行截图
|
|
1496
|
+
const dataUrl = await domToPng(this.screenshotContext);
|
|
1497
|
+
// 根据输出格式转换
|
|
1498
|
+
if (this.options.outputFormat !== 'png') {
|
|
1499
|
+
// modern-screenshot 默认输出 PNG,如果需要其他格式,需要转换
|
|
1500
|
+
const canvas = document.createElement('canvas');
|
|
1501
|
+
const ctx = canvas.getContext('2d');
|
|
1502
|
+
if (!ctx) {
|
|
1503
|
+
throw new Error('无法获取 canvas context');
|
|
1504
|
+
}
|
|
1505
|
+
const img = new Image();
|
|
1506
|
+
await new Promise((resolve, reject) => {
|
|
1507
|
+
img.onload = () => {
|
|
1508
|
+
canvas.width = img.width;
|
|
1509
|
+
canvas.height = img.height;
|
|
1510
|
+
ctx.drawImage(img, 0, 0);
|
|
1511
|
+
resolve();
|
|
1512
|
+
};
|
|
1513
|
+
img.onerror = reject;
|
|
1514
|
+
img.src = dataUrl;
|
|
1515
|
+
});
|
|
1516
|
+
let mimeType = 'image/jpeg';
|
|
1517
|
+
let finalQuality = mobileQuality;
|
|
1518
|
+
if (this.options.outputFormat === 'webp' && !isMobile) {
|
|
1519
|
+
try {
|
|
1520
|
+
const testCanvas = document.createElement('canvas');
|
|
1521
|
+
testCanvas.width = 1;
|
|
1522
|
+
testCanvas.height = 1;
|
|
1523
|
+
const testDataUrl = testCanvas.toDataURL('image/webp');
|
|
1524
|
+
if (testDataUrl.indexOf('webp') !== -1) {
|
|
1525
|
+
mimeType = 'image/webp';
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
catch {
|
|
1529
|
+
mimeType = 'image/jpeg';
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
const convertedDataUrl = mimeType === 'image/png'
|
|
1533
|
+
? canvas.toDataURL(mimeType)
|
|
1534
|
+
: canvas.toDataURL(mimeType, finalQuality);
|
|
1535
|
+
return convertedDataUrl;
|
|
1536
|
+
}
|
|
1537
|
+
return dataUrl;
|
|
1430
1538
|
}
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1539
|
+
catch (error) {
|
|
1540
|
+
// 如果截图失败,清理上下文以便下次重新创建
|
|
1541
|
+
if (this.screenshotContext) {
|
|
1542
|
+
try {
|
|
1543
|
+
destroyContext(this.screenshotContext);
|
|
1544
|
+
}
|
|
1545
|
+
catch (e) {
|
|
1546
|
+
// 忽略清理错误
|
|
1547
|
+
}
|
|
1548
|
+
this.screenshotContext = null;
|
|
1549
|
+
}
|
|
1550
|
+
throw error;
|
|
1434
1551
|
}
|
|
1435
|
-
const dataUrl = mimeType === 'image/png'
|
|
1436
|
-
? canvas.toDataURL(mimeType)
|
|
1437
|
-
: canvas.toDataURL(mimeType, finalQuality);
|
|
1438
|
-
return dataUrl;
|
|
1439
1552
|
}
|
|
1440
1553
|
/**
|
|
1441
1554
|
* 预处理网络图片
|
|
@@ -1548,7 +1661,7 @@ class ScreenshotManager {
|
|
|
1548
1661
|
throw new Error(`代理请求失败: ${response.status} ${response.statusText}${errorText ? ` - ${errorText.substring(0, 200)}` : ''}`);
|
|
1549
1662
|
}
|
|
1550
1663
|
const blob = await response.blob();
|
|
1551
|
-
// 将 blob 转换为 data URL(用于
|
|
1664
|
+
// 将 blob 转换为 data URL(用于 modern-screenshot 兼容性)
|
|
1552
1665
|
const dataUrl = await this.blobToDataUrl(blob);
|
|
1553
1666
|
if (!this.options.silentMode) {
|
|
1554
1667
|
console.log(`📸 ✅ 代理模式成功(已转换为 data URL): ${imageUrl.substring(0, 100)}...`);
|
|
@@ -1678,7 +1791,9 @@ class ScreenshotManager {
|
|
|
1678
1791
|
newWorker.onerror = (e) => {
|
|
1679
1792
|
console.error('📸 WebWorker 错误:', e);
|
|
1680
1793
|
};
|
|
1681
|
-
URL
|
|
1794
|
+
// 注意:不要立即 revokeObjectURL,因为 Worker 需要这个 URL 保持有效
|
|
1795
|
+
// 在 destroy() 方法中清理 Worker 时再 revoke
|
|
1796
|
+
// URL.revokeObjectURL(workerUrl) // 已移除,在 destroy 时清理
|
|
1682
1797
|
return newWorker;
|
|
1683
1798
|
}
|
|
1684
1799
|
catch (err) {
|
|
@@ -1809,6 +1924,16 @@ class ScreenshotManager {
|
|
|
1809
1924
|
this.worker.terminate();
|
|
1810
1925
|
this.worker = null;
|
|
1811
1926
|
}
|
|
1927
|
+
// 清理 modern-screenshot Worker 上下文
|
|
1928
|
+
if (this.screenshotContext) {
|
|
1929
|
+
try {
|
|
1930
|
+
destroyContext(this.screenshotContext);
|
|
1931
|
+
}
|
|
1932
|
+
catch (e) {
|
|
1933
|
+
// 忽略清理错误
|
|
1934
|
+
}
|
|
1935
|
+
this.screenshotContext = null;
|
|
1936
|
+
}
|
|
1812
1937
|
if (this.expirationTimer) {
|
|
1813
1938
|
clearTimeout(this.expirationTimer);
|
|
1814
1939
|
this.expirationTimer = null;
|
|
@@ -1818,6 +1943,8 @@ class ScreenshotManager {
|
|
|
1818
1943
|
this.messageHandler = null;
|
|
1819
1944
|
}
|
|
1820
1945
|
this.removeGlobalErrorHandlers();
|
|
1946
|
+
// 清理图片代理缓存
|
|
1947
|
+
this.imageProxyCache.clear();
|
|
1821
1948
|
}
|
|
1822
1949
|
/**
|
|
1823
1950
|
* 获取状态
|