Created
May 26, 2021 15:41
-
-
Save c7x43t/075c609e6d04a930e54c308d7d5b62e4 to your computer and use it in GitHub Desktop.
This file contains 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
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