diff --git a/entrypoints/main/compat.ts b/entrypoints/main/compat.ts index 8702491..8cde3b5 100644 --- a/entrypoints/main/compat.ts +++ b/entrypoints/main/compat.ts @@ -348,13 +348,7 @@ export const selectCompatFn: SelectCompatFn = { return { skip: true }; } - // 首先翻译最重要的文本内容 - - // 问题(Issue)和PR内容 - const issueBody = findMatchingElement(node, 'div.comment-body'); - if (issueBody) return issueBody; - - // 评论内容 + // Let generic logic handle individual

,

  • elements instead of the entire container const comment = findMatchingElement(node, 'div.comment-body td.comment-body'); if (comment) return comment; diff --git a/entrypoints/main/dom.ts b/entrypoints/main/dom.ts index 5805fd3..265709b 100644 --- a/entrypoints/main/dom.ts +++ b/entrypoints/main/dom.ts @@ -22,6 +22,12 @@ export const inlineSet = new Set([ 'img', 'br', 'wbr', 'svg' ]); +// 块级元素集合(作为翻译单元的块级元素) +const blockLevelTranslationElements = new Set([ + 'p', 'li', 'div', 'section', 'article', 'blockquote', + 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd' +]); + // 传入父节点,返回所有需要翻译的 DOM 元素数组 export function grabAllNode(rootNode: Node): Element[] { if (!rootNode) return []; @@ -350,10 +356,26 @@ function isInlineElement(node: any, tag: string): boolean { // 查找可翻译的父节点 function findTranslatableParent(node: any): any { - // 1. 递归调用 grabNode 查找父节点是否可翻译 - // 2. 若父节点不可翻译,则返回当前节点 - const parentResult = grabNode(node.parentNode); - return parentResult || node; + // 当前节点为内联元素时,向上遍历父节点链 + // 直到找到最近的块级元素为止(如 p, li, div, section 等) + // 这样可以避免过度向上查找,确保翻译粒度合适 + + let current = node; + + // 向上查找,直到找到块级元素或父节点不存在 + while (current && current.parentNode) { + const tag = current.tagName?.toLowerCase(); + + // 如果当前节点是块级元素,返回它 + if (blockLevelTranslationElements.has(tag)) { + return current; + } + + current = current.parentNode; + } + + // 如果没有找到块级元素,返回原节点 + return node; } // 处理首行文本