Skip to content

Instantly share code, notes, and snippets.

@ngerritsen
Created March 28, 2019 13:21
Show Gist options
  • Save ngerritsen/bc4f71700bbc431996d41351367e01ac to your computer and use it in GitHub Desktop.
Save ngerritsen/bc4f71700bbc431996d41351367e01ac to your computer and use it in GitHub Desktop.
Element counter
// Paste into browser console and hover elements.
(function checkElementCount() {
let current = null;
let counter = null;
function init() {
document.body.addEventListener('mouseover', onMouseOver);
updateCounter(getChildElementCount(document));
}
function onMouseOver(event) {
if (current) {
resetCurrent();
}
outlineElement(event.target);
updateCounter(getChildElementCount(event.target))
}
function resetCurrent() {
current.element.style['box-shadow'] = current.boxShadow;
current.element = null;
}
function outlineElement(element) {
current = { boxShadow: element.style['box-shadow'], element }
element.style['box-shadow'] = 'inset 0px 0px 0px 2px #D980FA';
}
function getChildElementCount(element) {
return element.querySelectorAll('*').length;
}
function updateCounter(count) {
if (counter) {
counter.textContent = count;
return;
}
counter = document.createElement('div');
counter.textContent = count;
counter.style = 'padding: 10px; position: fixed; top: 0; left: 0; background-color: #D980FA; z-index: 99999999; color: white;';
document.body.appendChild(counter);
}
init();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment