Created
August 30, 2023 16:27
-
-
Save mmocny/5914f883142429ab7df9c0197fe131ed to your computer and use it in GitHub Desktop.
Save Event Targets
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
const interestingEventTypes = [ 'pointerdown', 'pointerup', 'click', 'keydown', 'keypress', 'keyup']; | |
const mapOfTargets = Object.fromEntries(interestingEventTypes.map(type => [type, {}])); | |
interestingEventTypes.forEach(type => | |
document.addEventListener(type, (event) => { | |
// TODO: Improve this | |
const nodeType = event.target.nodeName.toLowerCase(); | |
const nodeId = event.target.id; | |
const nodeClasses = event.target.className.replace(/\s/g, '.'); | |
const targetSelector = `${nodeType}#${nodeId}.${nodeClasses}`; | |
mapOfTargets[type][event.timeStamp] = targetSelector; | |
}) | |
); | |
new PerformanceObserver(list => { | |
for (let entry of list.getEntries()) { | |
if (!interestingEventTypes.includes(entry.name)) continue; | |
const targetSelector = mapOfTargets[entry.name][entry.startTime]; | |
console.log(targetSelector, entry); | |
} | |
}).observe({ | |
type: 'event', | |
durationThreshold: 0 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment