Open
Conversation
Reviewer's GuideThis PR hardens the text selection and translation pipeline by stripping both ASCII and full-width whitespace before checks, and short-circuiting when content is effectively empty so that empty/whitespace-only nodes are no longer sent for translation or treated as translatable content. Sequence diagram for the updated text translation pipeline ignoring empty contentsequenceDiagram
actor User
participant BrowserExtension as BrowserExtension
participant DomModule as DomModule_grabAllNode
participant TransModule as TransModule_trans
participant TranslateApi as TranslateApi_translateText
participant TranslationService as External_Translation_Service
User->>BrowserExtension: Trigger_translation
BrowserExtension->>DomModule: grabAllNode(rootNode)
DomModule->>DomModule: Traverse_DOM_nodes
DomModule->>DomModule: For_each_Text_node
DomModule->>DomModule: textContent.replace(/[\s\u3000]/g, '')
alt Text_has_non_empty_content
DomModule-->>BrowserExtension: Return_filtered_nodes_including_Text_node
else Text_is_empty_or_whitespace_only
DomModule-->>BrowserExtension: Skip_Text_node
end
loop For_each_filtered_node
BrowserExtension->>TransModule: singleTranslate(node) / bilingualTranslate(node)
TransModule->>TransModule: cleanedText = node.textContent.replace(/[\s\u3000]/g, '')
alt cleanedText_is_empty
TransModule-->>BrowserExtension: Return_without_translation
else cleanedText_not_empty
TransModule->>TransModule: detectlang(cleanedText)
alt Language_already_target_lang
TransModule-->>BrowserExtension: Return_without_translation
else Language_needs_translation
TransModule->>TranslateApi: translateText(origin, context, options)
TranslateApi->>TranslateApi: cleanedOrigin = origin.replace(/[\s\u3000]/g, '')
alt cleanedOrigin_is_empty
TranslateApi-->>TransModule: Return_origin_without_calling_service
else cleanedOrigin_not_empty
TranslateApi->>TranslateApi: detectlang(origin_without_whitespace)
alt Origin_language_is_target
TranslateApi-->>TransModule: Return_origin
else Origin_language_differs
TranslateApi->>TranslationService: Request_translation
TranslationService-->>TranslateApi: Translated_text
TranslateApi-->>TransModule: Return_translated_text
end
end
TransModule-->>BrowserExtension: Apply_translation_to_node
end
end
end
BrowserExtension-->>User: Display_page_without_placeholder_translations
Flow diagram for whitespace-aware text filtering in DOM scanningflowchart TD
A[Start_grabAllNode_with_rootNode] --> B[Create_TreeWalker_for_elements_and_text_nodes]
B --> C[Get_next_node]
C --> D{Node_is_null?}
D -->|Yes| E[End_traversal_and_return_collected_nodes]
D -->|No| F{Node_instanceof_Text?}
F -->|Yes| G[Compute_textContent = node.textContent_or_empty_string]
G --> H[cleanedText = textContent_without_spaces_and_full_width_spaces]
H --> I{cleanedText.length > 0?}
I -->|Yes| J[ACCEPT_Text_node]
I -->|No| K[SKIP_Text_node]
J --> L[Consider_node_for_translation_pipeline]
K --> L
F -->|No| M{Node_instanceof_Element?}
M -->|No| N[SKIP_node]
N --> C
M -->|Yes| O[Inspect_childNodes]
O --> P[Initialize_hasElement_false]
P --> Q[Initialize_hasNonEmptyElement_false]
Q --> R[Initialize_hasText_false]
R --> S[For_each_child_in_childNodes]
S --> T{child.nodeType_is_ELEMENT_NODE?}
T -->|Yes| U[Set_hasElement_true]
U --> V[childText = child.textContent_or_empty_string]
V --> W[childText_without_spaces_and_full_width_spaces]
W --> X{childText.length > 0?}
X -->|Yes| Y[Set_hasNonEmptyElement_true]
X -->|No| Z[Keep_hasNonEmptyElement_false]
Y --> AA[Continue_children_loop]
Z --> AA
T -->|No| AB{child.nodeType_is_TEXT_NODE?}
AB -->|Yes| AC[textContent = child.textContent_or_empty_string]
AC --> AD[textContent_without_spaces_and_full_width_spaces]
AD --> AE{textContent.length > 0?}
AE -->|Yes| AF[Set_hasText_true]
AE -->|No| AG[Keep_hasText_false]
AF --> AH[Continue_children_loop]
AG --> AH
AB -->|No| AH[Continue_children_loop]
AH --> AI{More_children?}
AI -->|Yes| S
AI -->|No| AJ[Evaluate_hasElement_hasNonEmptyElement_hasText]
AJ --> AK[Decide_ACCEPT_or_SKIP_Element_node]
AK --> C
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
translateText, you computecleanedOriginbut still calldetectlang(origin.replace(...)); reusingcleanedOriginhere would avoid duplicate work and keep the empty-text semantics consistent. - The checks
if (!cleanedText || cleanedText.length === 0)/if (!cleanedOrigin || cleanedOrigin.length === 0)are redundant because the replace/|| ''guarantees a string; these conditions can be simplified to a single falsy check.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `translateText`, you compute `cleanedOrigin` but still call `detectlang(origin.replace(...))`; reusing `cleanedOrigin` here would avoid duplicate work and keep the empty-text semantics consistent.
- The checks `if (!cleanedText || cleanedText.length === 0)` / `if (!cleanedOrigin || cleanedOrigin.length === 0)` are redundant because the replace/`|| ''` guarantees a string; these conditions can be simplified to a single falsy check.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
在多处添加了trim并检查待翻译文本是否为空的判断,解决了reddit帖子、github 贡献者头像处等网站出现大量类似“请提供翻译文本”的bug
前:

后:

Summary by Sourcery
Prevent translation logic from processing empty or whitespace-only text content across DOM scanning and translation entry points.
Bug Fixes: