MCPcopy Index your code
hub / github.com/Bistutu/FluentRead / grabNode

Function grabNode

entrypoints/main/dom.ts:105–157  ·  view source on GitHub ↗
(node: any)

Source from the content-addressed store, hash-verified

103
104// 返回最终应该翻译的父节点或 false
105export function grabNode(node: any): any {
106 // 空节点检查
107 if (!node) return false;
108
109 // 对于 Text 节点,尝试找到其可翻译的父节点
110 if (node instanceof Text) {
111 const parentOrSelf = findTranslatableParent(node);
112 if (parentOrSelf && parentOrSelf !== node) {
113 return parentOrSelf;
114 }
115 return false;
116 }
117
118 if (!node.tagName) return false;
119
120 const curTag = node.tagName.toLowerCase();
121
122 // 1. 快速过滤:跳过不需要翻译的节点
123 if (shouldSkipNode(node, curTag)) return false;
124
125 // 2. 特殊适配:根据域名进行特殊处理
126 const domainHandler = selectCompatFn[getMainDomain(location.href.split('?')[0])];
127 if (domainHandler) {
128 const result = domainHandler(node);
129 // 如果返回的是对象且包含skip属性为true,则跳过该节点
130 if (result && typeof result === 'object' && 'skip' in result && result.skip === true) {
131 return false;
132 }
133 // 如果返回值为节点或其他真值,则返回该值作为翻译节点
134 if (result) return result;
135 }
136
137 // 3. 直接翻译:块级元素
138 if (directSet.has(curTag)) return node;
139
140 // 4. 按钮处理:特殊处理按钮内的文本
141 if (isButton(node, curTag)) {
142 handleButtonTranslation(node);
143 return false;
144 }
145
146 // 5. 内联元素处理:向上查找合适的父节点
147 if (isInlineElement(node, curTag)) {
148 return findTranslatableParent(node);
149 }
150
151 // 6. 首行文本处理:处理 div 和 label 的首行文本
152 if (curTag === 'div' || curTag === 'label') {
153 return handleFirstLineText(node);
154 }
155
156 return false;
157}
158
159// 检查是否应该跳过节点
160function shouldSkipNode(node: any, tag: string): boolean {

Callers 3

handleTranslationFunction · 0.90
grabAllNodeFunction · 0.85
findTranslatableParentFunction · 0.85

Calls 7

getMainDomainFunction · 0.90
findTranslatableParentFunction · 0.85
shouldSkipNodeFunction · 0.85
isButtonFunction · 0.85
handleButtonTranslationFunction · 0.85
isInlineElementFunction · 0.85
handleFirstLineTextFunction · 0.85

Tested by

no test coverage detected