Created
March 28, 2019 13:21
-
-
Save ngerritsen/bc4f71700bbc431996d41351367e01ac to your computer and use it in GitHub Desktop.
Element counter
This file contains 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
// 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