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
18 changes: 13 additions & 5 deletions entrypoints/main/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export function grabAllNode(rootNode: Node): Element[] {
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
{
acceptNode: (node: Node): number => {
if (node instanceof Text) return NodeFilter.FILTER_ACCEPT;
if (node instanceof Text) {
// 检查文本节点是否有实际内容(去除空白后)
const textContent = node.textContent?.replace(/[\s\u3000]/g, '') || '';
return textContent.length > 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}

if (!(node instanceof Element)) return NodeFilter.FILTER_SKIP;

Expand All @@ -59,13 +63,17 @@ export function grabAllNode(rootNode: Node): Element[] {
for (const child of node.childNodes) {
if (child.nodeType === Node.ELEMENT_NODE) {
hasElement = true;
// 检查子元素是否包含文本
if (child.textContent?.trim()) {
// 检查子元素是否包含文本(去除空白后)
const childText = child.textContent?.replace(/[\s\u3000]/g, '') || '';
if (childText.length > 0) {
hasNonEmptyElement = true;
}
}
if (child.nodeType === Node.TEXT_NODE && child.textContent?.trim()) {
hasText = true;
if (child.nodeType === Node.TEXT_NODE) {
const textContent = child.textContent?.replace(/[\s\u3000]/g, '') || '';
if (textContent.length > 0) {
hasText = true;
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions entrypoints/main/trans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ export function handleSingleTranslation(node: any, slide: boolean) {


function bilingualTranslate(node: any, nodeOuterHTML: any) {
if (detectlang(node.textContent.replace(/[\s\u3000]/g, '')) === config.to) return;
const cleanedText = node.textContent.replace(/[\s\u3000]/g, '');
if (!cleanedText || cleanedText.length === 0) return;
if (detectlang(cleanedText) === config.to) return;

let origin = node.textContent;
let spinner = insertLoadingSpinner(node);
Expand All @@ -272,7 +274,9 @@ function bilingualTranslate(node: any, nodeOuterHTML: any) {


export function singleTranslate(node: any) {
if (detectlang(node.textContent.replace(/[\s\u3000]/g, '')) === config.to) return;
const cleanedText = node.textContent.replace(/[\s\u3000]/g, '');
if (!cleanedText || cleanedText.length === 0) return;
if (detectlang(cleanedText) === config.to) return;

let origin = servicesType.isMachine(config.service) ? node.innerHTML : LLMStandardHTML(node);
let spinner = insertLoadingSpinner(node);
Expand Down
6 changes: 6 additions & 0 deletions entrypoints/utils/translateApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export async function translateText(origin: string, context: string = document.t
useCache = config.useCache,
} = options;

// 检查 origin 是否为空或只有空白字符
const cleanedOrigin = origin?.replace(/[\s\u3000]/g, '') || '';
if (!cleanedOrigin || cleanedOrigin.length === 0) {
return origin || '';
}

// 如果目标语言与当前文本语言相同,直接返回原文
if (detectlang(origin.replace(/[\s\u3000]/g, '')) === config.to) {
return origin;
Expand Down