Custom script for DOM debugging
(function() {
const eventTypes = [
'click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout',
'keydown', 'keypress', 'keyup',
'focus', 'blur', 'change', 'input',
'submit', 'reset',
'focusin', 'focusout'
];
const logEvent = function(e) {
const timestamp = new Date().toISOString().slice(11, -1);
const target = e.target.tagName +
(e.target.id ? '#' + e.target.id : '') +
(e.target.className ? '.' + e.target.className.split(' ')[0] : '');
// Get value for input-like elements
let valueInfo = "";
if (e.target.value !== undefined) {
// Handle empty strings specially to make them visible
const displayValue = e.target.value === "" ? "<empty string>" : e.target.value;
valueInfo = ` Value: ${displayValue}`;
}
// Include key information for keyboard events
if (e.type.startsWith('key') && e.key) {
valueInfo += ` Key: ${e.key} (${e.keyCode})`;
}
console.log(
`%c${timestamp} %c${e.type} %con %c${target}%c${valueInfo}`,
'color: gray',
'color: blue; font-weight: bold',
'color: gray',
'color: green',
'color: purple; font-weight: bold'
);
// Log selected properties rather than the entire event object
console.log({
type: e.type,
target: e.target,
currentTarget: e.currentTarget,
value: e.target.value,
checked: e.target.checked,
selectedIndex: e.target.selectedIndex
});
};
// Setup listeners for all events
const stopFunctions = eventTypes.map(type => {
document.addEventListener(type, logEvent, true);
return () => document.removeEventListener(type, logEvent, true);
});
// Return a function to remove all listeners
return function stopLogging() {
console.log('Stopped logging DOM events');
stopFunctions.forEach(fn => fn());
};
})();
Comments
Post a Comment