Open
Conversation
Reviewer's GuideThis PR prevents empty or whitespace-only DOM text from being sent for translation by normalizing text nodes and adding early-return guards around translation entry points, eliminating bogus “请提供翻译文本” output on elements like Reddit posts and GitHub contributor avatars. Sequence diagram for updated DOM text translation flow with empty-text guardsequenceDiagram
actor User
participant Page as WebPage
participant Dom as grabAllNode
participant Trans as handleSingleTranslation
participant API as translateText
participant Service as TranslationService
User->>Page: Trigger page translation
Page->>Dom: grabAllNode(rootNode)
Dom->>Dom: Iterate TreeWalker nodes
alt Text node with nonempty cleanedText
Dom-->>Page: Element list includes node
else Text node empty or whitespace-only
Dom-->>Page: Text node skipped (FILTER_SKIP)
end
loop For each node in element list
Page->>Trans: handleSingleTranslation(node, slide)
Trans->>Trans: cleanedText = node.textContent.replace(/\s|\u3000/g, '')
alt cleanedText is empty
Trans-->>Page: Return without translation
else cleanedText not empty
Trans->>API: translateText(origin, context, options)
API->>API: cleanedOrigin = origin.replace(/\s|\u3000/g, '')
alt cleanedOrigin empty
API-->>Trans: Return origin (no request)
else cleanedOrigin not empty
API->>Service: Request translation
Service-->>API: Translated text
API-->>Trans: Translated text
Trans-->>Page: Replace node text with translation
end
end
end
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
grabAllNodeyou now runreplaceon every text node inside the TreeWalker and again inside the child loop; consider centralizing the non-empty-text detection for nodes to avoid duplicated work and make it clearer what qualifies as 'translatable' content.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `grabAllNode` you now run `replace` on every text node inside the TreeWalker and again inside the child loop; consider centralizing the non-empty-text detection for nodes to avoid duplicated work and make it clearer what qualifies as 'translatable' content.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 of empty or whitespace-only text nodes across the DOM traversal and translation pipeline.
Bug Fixes: