Last active
November 22, 2024 00:09
-
-
Save mmocny/d84177f0352532f72599e5c212ca0a3b to your computer and use it in GitHub Desktop.
Add elementtiming attribute to all elements, as page loads or dynamically updates. Useful polyfill.
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
<!-- Add to <head> or as first script in body before any content --> | |
<script type='text/javascript'> | |
const observedElements = new WeakSet(); // Keep track of processed elements | |
new MutationObserver((mutationsList, observer) => { | |
for (let mutation of mutationsList) { | |
for (let node of mutation.addedNodes) { | |
if (node.nodeType === Node.ELEMENT_NODE && !observedElements.has(node)) { | |
node.setAttribute('elementtiming', 'test'); | |
observedElements.add(node); // Mark the element as processed | |
} | |
} | |
} | |
}).observe(document.documentElement, { childList: true, subtree: true }); | |
new PerformanceObserver((list) => { | |
list.getEntries().forEach((entry) => { | |
console.log(entry.intersectionRect); | |
console.log(entry); | |
}) | |
}).observe({type: "element", buferred: true}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment