Skip to content

Instantly share code, notes, and snippets.

@mmocny
Created August 30, 2023 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmocny/5914f883142429ab7df9c0197fe131ed to your computer and use it in GitHub Desktop.
Save mmocny/5914f883142429ab7df9c0197fe131ed to your computer and use it in GitHub Desktop.
Save Event Targets
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