Skip to content

Instantly share code, notes, and snippets.

@captainbrosset
Created March 5, 2021 09:08
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 captainbrosset/73dde5423b590f8f32111f829725c7dc to your computer and use it in GitHub Desktop.
Save captainbrosset/73dde5423b590f8f32111f829725c7dc to your computer and use it in GitHub Desktop.
Code snippets for the "How we built the DevTools Tooltips" article
function querySelectorShadow(selector: string): Element|null {
let found: Element|null = null;
const search = (root: Element|ShadowRoot) => {
const iter = document.createTreeWalker(
root,
NodeFilter.SHOW_ELEMENT,
);
do {
const currentNode = iter.currentNode as HTMLElement;
if (currentNode.shadowRoot) {
search(currentNode.shadowRoot);
}
if (currentNode instanceof ShadowRoot) {
continue;
}
if (!found && currentNode.matches(selector)) {
found = currentNode;
}
} while (!found && iter.nextNode());
};
search(document.documentElement);
return found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment