Last active
April 17, 2017 17:35
-
-
Save JeremyRH/4bea3407a0fac1c20d89 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getElementsByText(text, rootNode = document.documentElement) { | |
const nodeIterator = document.createNodeIterator( | |
rootNode, | |
NodeFilter.SHOW_TEXT, | |
(node) => node.nodeValue.trim() === text ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT | |
); | |
const matchingElements = []; | |
let currentNode; | |
while (currentNode = nodeIterator.nextNode()) { | |
const parentElement = currentNode.parentElement; | |
if (parentElement) { | |
matchingElements.push(parentElement); | |
} | |
} | |
return matchingElements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment