Skip to content

Instantly share code, notes, and snippets.

@geotrev
Last active February 1, 2024 15:23
Show Gist options
  • Save geotrev/32e8c10075494b08f907bd9eebde9fdc to your computer and use it in GitHub Desktop.
Save geotrev/32e8c10075494b08f907bd9eebde9fdc to your computer and use it in GitHub Desktop.
Active Element Live Expression
/**
* This script recursively checks `shadowRoot`s and `contentDocument`s until the true `activeElement` is found.
*
* HOW TO USE: Copy and paste this into your Chrome dev tools in the `Create live expression` input.
*/
(function(){function d(){var u=arguments[0]||document.activeElement;if(u.shadowRoot&&u.shadowRoot.activeElement){return d(u.shadowRoot.activeElement)}if(u.contentDocument&&u.contentDocument.activeElement){return d(u.contentDocument.activeElement)}return u}return d()})();
/**
Unminified version:
(function getActiveElement(el = document.activeElement) {
if (el.shadowRoot && el.shadowRoot.activeElement) {
return getActiveElement(el.shadowRoot.activeElement)
}
if (el.contentDocument && el.contentDocument.activeElement) {
return getActiveElement(el.contentDocument.activeElement)
}
return el
})()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment