(function() {
// Function to instrument a DOM element
function instrumentElement(element) {
if (!element) return;
// Wrap the original methods with instrumentation
const originalMethods = {};
for (const method in element) {
if (typeof element[method] === 'function') {
originalMethods[method] = element[method];
}
}
const instrumentedElement = Object.create(element); // Create a new object inheriting from the original
for (const method in originalMethods) {
if (originalMethods.hasOwnProperty(method)) {
const originalMethod = originalMethods[method];
instrumentedElement[method] = function(...args) {
// Log the method call with arguments
console.log(`Called ${method} on element:`, element.tagName);
console.log("Arguments:", args);
// Call the original method
const result = originalMethod.apply(this, args);
// Log the return value
console.log(`${method} returned:`, result);
return result;
};
}
}
// Replace the original element with the instrumented one.
element.parentNode.replaceChild(instrumentedElement, element);
}
// Find all DOM elements and instrument them
const elements = document.querySelectorAll('*');
elements.forEach(instrumentElement);
})();
Add your comment