()
| 347 | } |
| 348 | |
| 349 | async function takeScreenshot() { |
| 350 | const status = document.getElementById('status'); |
| 351 | const container = document.getElementById('screenshotContainer'); |
| 352 | |
| 353 | // 显示加载状态 |
| 354 | status.innerHTML = '<div class="loading">正在获取截图,请稍候...</div>'; |
| 355 | container.innerHTML = '<div class="loading">截图中...</div>'; |
| 356 | |
| 357 | // 显示加载动画 |
| 358 | // showLoadingAnimation(); |
| 359 | |
| 360 | try { |
| 361 | // 直接调用获取截图的API,该API会自动更新截图 |
| 362 | const response = await fetch('/screenshot'); |
| 363 | |
| 364 | if (response.ok) { |
| 365 | const result = await response.json(); |
| 366 | status.innerHTML = '<div class="success">截图成功!可以点击或滑动进行操作</div>'; |
| 367 | |
| 368 | // 显示截图并添加事件监听 |
| 369 | container.innerHTML = ` |
| 370 | <img id="screenshotImage" |
| 371 | alt="设备截图" |
| 372 | class="screenshot-img" |
| 373 | onerror="this.parentElement.innerHTML='<div class=error>截图加载失败</div>'"> |
| 374 | `; |
| 375 | |
| 376 | // 获取截图元素引用 |
| 377 | screenshotImg = document.getElementById('screenshotImage'); |
| 378 | |
| 379 | // 直接设置截图数据 |
| 380 | if (result.image_data) { |
| 381 | screenshotImg.src = result.image_data; |
| 382 | |
| 383 | // 存储层次结构信息供后续使用 |
| 384 | window.currentHierarchy = result.hierarchy; |
| 385 | |
| 386 | // 解析并保存所有UI元素信息 |
| 387 | if (result.hierarchy) { |
| 388 | currentElements = parseUIElements(result.hierarchy); |
| 389 | const clickableElements = currentElements.filter(el => el.clickable); |
| 390 | console.log(`UI元素信息已加载: ${currentElements.length} 个元素 (其中 ${clickableElements.length} 个可点击)`); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // 显示操作提示 |
| 395 | const hint = document.getElementById('actionHint'); |
| 396 | if (hint) { |
| 397 | hint.style.display = 'block'; |
| 398 | } |
| 399 | |
| 400 | // 为截图添加交互功能 |
| 401 | setupScreenshotInteraction(); |
| 402 | |
| 403 | // 隐藏加载动画 |
| 404 | hideLoadingAnimation(); |
| 405 | } else { |
| 406 | const error = await response.json(); |
no test coverage detected