Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions entrypoints/main/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <p>, <li> elements instead of the entire container
const comment = findMatchingElement(node, 'div.comment-body td.comment-body');
if (comment) return comment;

Expand Down
30 changes: 26 additions & 4 deletions entrypoints/main/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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;
}

// 处理首行文本
Expand Down