UNPKG

lingator

Version:

A plug-and-play npm package to auto-translate webpages using Google's Gemini AI.

24 lines (22 loc) 634 B
export function getAllTextNodes(root = document.body) { const walker = document.createTreeWalker( root, NodeFilter.SHOW_TEXT, { acceptNode: (node) => { const text = node.nodeValue.trim(); if (!text) return NodeFilter.FILTER_REJECT; if ( node.parentNode && ["SCRIPT", "STYLE", "NOSCRIPT"].includes(node.parentNode.nodeName) ) { return NodeFilter.FILTER_REJECT; } return NodeFilter.FILTER_ACCEPT; } } ); const nodes = []; while (walker.nextNode()) nodes.push(walker.currentNode); return nodes; }