Last active
December 24, 2020 10:53
-
-
Save anthowave/aa53f5312c3a268f2ffb7bae6b38aaca to your computer and use it in GitHub Desktop.
6-line javascript code for lazy DFS over the DOM
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* search(node) { | |
if (!node) return; | |
yield node; | |
yield* search(node.firstChild); | |
yield* search(node.nextSibling); | |
} | |
// example usage | |
for (let node of search(document)) { | |
if (node.localName === 'title') { | |
console.log(node.textContent); | |
break; | |
} | |
} | |
// from: https://www.youtube.com/watch?v=cLxNdLK--yI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment