Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to log all DOM method calls

Is there a way (preferably Firefox or Chrome) to log all the DOM methods invoked/properties modified by a Web app?

I need this to understand some of the working of web apps whose code I don't have in non-minified version.

I understand that this won't give me the complete picture, but I am more interested in the web app's interaction with the browser for my purpose.

like image 831
Sunil Agrawal Avatar asked Sep 16 '13 06:09

Sunil Agrawal


People also ask

How do I view global variables in Google Chrome?

Store as global variable Luckily, Chrome makes it easy to inspect such objects with JavaScript. To do so, right click on an object in the console and press “store as global variable”. This stores the object as a global variable accessible in the console called temp1 which you can then work with using JavaScript.

Where is the console log output?

Steps to Open the Console Log in Google Chrome By default, the Inspect will open the "Elements" tab in the Developer Tools. Click on the "Console" tab which is to the right of "Elements". Now you can see the Console and any output that has been written to the Console log.

How do I see variable values in chrome console?

To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.

How do you show variables in inspect element?

You can inspect variables in two ways: by moving the mouse over the name of a variable in the Debug editor window (hovering) or by using the Variables view.


1 Answers

You can log all method calls for specific class of objects by wrapping all of its methods with a custom logging function:

var originalMethod = SomeObject.prototype.someMethod;
SomeObject.prototype.someMethod = function() {
    //log this call
    originalMethod.apply(this, arguments);
}

I've created a function that hooks up such wrappers to all (non-inherited) methods of given class and logs all calls to the console:

function logMethodCalls(className) {

    function wrapMethod(className, methodName, prototype) {
        var orgMethod = prototype[methodName];

        return function() {
                    window.console.debug('%c'+className+'::%c'+methodName, 'color: #FBB117; font-weight: bold', 'color: #6F4E37', {
                        details: {
                            scope: this,
                            arguments: arguments
                        }
                    });
                    return orgMethod.apply(this, arguments);
                };
    }

    if(!window[className] || typeof window[className] !== 'function') {
        window.console.error('Invalid class name.');
        return;
    }

    var prototype = window[className].prototype;

    for(var i in prototype) {
        if(prototype.hasOwnProperty(i)) {
            if(typeof prototype[i] === "function") {
                prototype[i] = wrapMethod(className, i, prototype);
            }
        }
    }
}

I'm running it like this:

["Document", "DocumentFragment", "Element", "Event", "HTMLElement", "HTMLDocument", "Node", "NodeList", "Window"].forEach(function(i){
    logMethodCalls(i);
});

You can customise the array above to track only classes that you are interested in.

The output looks like this:

DevTools console output

To be perfectly honest there is so much output that I don't think this type of debugging may be usable. You can try extending this solution even more by observing all properties (e.g. by defining getters and setters or proxies for all objects), but this will get even more messy.

like image 57
Konrad Dzwinel Avatar answered Sep 20 '22 23:09

Konrad Dzwinel