Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Finds all elements on the page, including those within shadow dom.
* @author ebidel@ (Eric Bidelman)
* License Apache-2.0
* Finds all elements on the page, inclusive of those within shadow roots.
* @param {string=} selector Simple selector to filter the elements by. e.g. 'a', 'div.main'
* @return {!Array<string>} List of anchor hrefs.
function collectAllElementsDeep(selector = null) {
const allElements = [];
const findAllElements = function(nodes) {
for (let i = 0, el; el = nodes[i]; ++i) {
// If the element has a shadow root, dig deeper.
if (el.shadowRoot) {
return selector ? allElements.filter(el => el.matches(selector)) : allElements;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.