| 358 | |
| 359 | // 处理首行文本 |
| 360 | function handleFirstLineText(node: any): boolean { |
| 361 | // 1. 遍历子节点,找到首个文本节点 |
| 362 | // 2. 若存在可翻译文本,则通过 browser.runtime.sendMessage 进行翻译 |
| 363 | // 3. 翻译成功后,替换该文本;出现错误时,打印错误日志 |
| 364 | let child = node.firstChild; |
| 365 | while (child) { |
| 366 | if (child.nodeType === Node.TEXT_NODE && child.textContent.trim()) { |
| 367 | browser.runtime.sendMessage({ |
| 368 | context: document.title, |
| 369 | origin: child.textContent |
| 370 | }) |
| 371 | .then((text: string) => child.textContent = text) |
| 372 | .catch((error: any) => console.error('翻译失败:', error)); |
| 373 | return false; |
| 374 | } |
| 375 | child = child.nextSibling; |
| 376 | } |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | // 检测子元素中是否包含指定标签以外的元素 |
| 381 | function detectChildMeta(parent: any): boolean { |