2019-09-21
Listen to all events in Chrome browser
stackoverflow
Question

I want to listen all the events that happen in my site. I use the monitorEvents() command, but this command only prints the event to the console, and I want to set a callback function for it to do some manipulations on that data without printing it to the console.

Is there any smart way to do this?

Answer
1

I found the best way to do it. This code is based on the original monitorEvents() command. With this script its possible to listen all the Chrome events and handle them with custom function… you just need edit the "eventHandler" function. For try it just copy to the Chrome console and then use: startMonitorEvents(window, eventHandler);

/**
* @param {Event} event
*/
function eventHandler(event)
{
    // Do whatever you want here... 

    console.log("my custom handler… ", event.type, event);
}

/**
* @param {Object} object
* @param {Function} callback
*/
function startMonitorEvents(object, callback)
{
    if (!object || !object.addEventListener || !object.removeEventListener)
                    return;
    var types = getEventsForMonitor();
    for (var i = 0; i < types.length; ++i) {
                    object.removeEventListener(types[i], callback, false);
                    object.addEventListener(types[i], callback, false);
    }
}

function getEventsForMonitor()
{
    var result = [];

    // mouse
    result.splice(0, 0, "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel");
    // key
    result.splice(0, 0, "keydown", "keyup", "keypress", "textInput");
    // touch
    result.splice(0, 0, "touchstart", "touchmove", "touchend", "touchcancel");
    // control
    result.splice(0, 0, "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset");
    // others
    result.splice(0, 0, "load", "unload", "abort", "error", "select", "change", "submit", "reset", "focus", "blur", "resize", "scroll", "search", "devicemotion", "deviceorientation");

    return result;
}

// Usage: startMonitorEvents(window, eventHandler);
Listen to all events in Chrome browser
See more ...