Skip to content

Instantly share code, notes, and snippets.

@c7x43t
Created May 26, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c7x43t/075c609e6d04a930e54c308d7d5b62e4 to your computer and use it in GitHub Desktop.
Save c7x43t/075c609e6d04a930e54c308d7d5b62e4 to your computer and use it in GitHub Desktop.
function getLeafElements(root=document.body,result=[]){
if(root.children.length===0) return result.push(root);
for(var element of root.children){
getLeafElements(element,result);
}
return result;
}
function getElementsByTextContent(reg){
var elements=getLeafElements();
var result=[];
for(var element of elements){
if(!(reg instanceof RegExp) && typeof reg === 'string' ) reg=new RegExp(reg);
if(reg.exec(element.textContent)!==null) result.push(element);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment