MCPcopy
hub / github.com/ocsjs/ocsjs / optimizationElementWithImage

Function optimizationElementWithImage

packages/scripts/src/utils/work.ts:187–206  ·  view source on GitHub ↗
(root: HTMLElement, clone_node: boolean = false)

Source from the content-addressed store, hash-verified

185 * 返回一个克隆的节点
186 */
187export function optimizationElementWithImage(root: HTMLElement, clone_node: boolean = false): HTMLElement {
188 const clone = clone_node ? (root.cloneNode(true) as HTMLElement) : root;
189 for (const img of Array.from(clone.querySelectorAll('img'))) {
190 // 如果已经存在识别结果,则不处理
191 if (
192 Array.from(img.parentElement!.querySelectorAll('span')).some(
193 (e) => e.style.fontSize === '0px' && e.textContent?.includes(img.src)
194 )
195 ) {
196 continue;
197 }
198
199 const src = document.createElement('span');
200 src.innerText = img.src;
201 // 隐藏图片,但不影响 innerText 的获取
202 src.style.fontSize = '0px';
203 img.after(src);
204 }
205 return clone;
206}
207
208/**
209 * 创建一个不可见的文本节点,追加到图片后面,便于文本获取

Callers 11

workOrExamFunction · 0.90
chapterFunction · 0.90
titleTransformFunction · 0.90
workAndExamFunction · 0.90
onElementSearchedFunction · 0.90
titleTransformFunction · 0.90
aiWorkFunction · 0.90
onElementSearchedFunction · 0.90
titleTransformFunction · 0.90

Calls 1

fromMethod · 0.80

Tested by

no test coverage detected