在浏览器控制台中监控所有 JavaScript 事件 [英] Monitor all JavaScript events in the browser console

查看:30
本文介绍了在浏览器控制台中监控所有 JavaScript 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以监听所有 javascript 事件?

Is it possible to listen to all javascript events?

我想猜测在 DOM 被 AJAX 请求修改后是否触发了一个事件.

I'm trying to guess if there's an event triggered after the DOM is modified by an AJAX request.

推荐答案

With firebug网络检查器 你可以使用monitorEvents:

With firebug or web inspector you can use monitorEvents:

monitorEvents(myDomElem);

这会将 myDomElem 发出的所有事件打印到控制台.使用 unmonitorEvents 停止监控事件​​.

This prints all events emitted by myDomElem to the console. Use unmonitorEvents to stop monitoring events.

如果您有兴趣在 DOM 被操作后获取事件,请查看 突变事件.

If you're interested in getting events after the DOM has been manipulated, take a look at Mutation Events.

编辑:

据我所知,没有简单的方法可以拦截来自所有 XMLHttpRequest 的所有 onreadystatechange 事件.我能想到的唯一解决方法是用您自己的实现覆盖本机 XMLHttpRequest 对象.例如:

As far as I know, there is no easy way to intercept all onreadystatechange events from all XMLHttpRequest. The only work-around I can think of is to override the native XMLHttpRequest object with you own implementation. For example:

(function() { // Overriding XMLHttpRequest
    var oldXHR = window.XMLHttpRequest;

    function newXHR() {
        var realXHR = new oldXHR();

        realXHR.addEventListener("readystatechange", function() { 
            console.log("an ajax request was made") 
        }, false);

        return realXHR;
    }

    window.XMLHttpRequest = newXHR;
})();

毋庸置疑,这是非常不明智的,而且通常是不明智的.

Needless to say this is extremely hacky and generally ill-advised.

这篇关于在浏览器控制台中监控所有 JavaScript 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆