1. (function() {
  2. // Function to instrument a DOM element
  3. function instrumentElement(element) {
  4. if (!element) return;
  5. // Wrap the original methods with instrumentation
  6. const originalMethods = {};
  7. for (const method in element) {
  8. if (typeof element[method] === 'function') {
  9. originalMethods[method] = element[method];
  10. }
  11. }
  12. const instrumentedElement = Object.create(element); // Create a new object inheriting from the original
  13. for (const method in originalMethods) {
  14. if (originalMethods.hasOwnProperty(method)) {
  15. const originalMethod = originalMethods[method];
  16. instrumentedElement[method] = function(...args) {
  17. // Log the method call with arguments
  18. console.log(`Called ${method} on element:`, element.tagName);
  19. console.log("Arguments:", args);
  20. // Call the original method
  21. const result = originalMethod.apply(this, args);
  22. // Log the return value
  23. console.log(`${method} returned:`, result);
  24. return result;
  25. };
  26. }
  27. }
  28. // Replace the original element with the instrumented one.
  29. element.parentNode.replaceChild(instrumentedElement, element);
  30. }
  31. // Find all DOM elements and instrument them
  32. const elements = document.querySelectorAll('*');
  33. elements.forEach(instrumentElement);
  34. })();

Add your comment