在浏览器控制台中监控所有 JavaScript 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3489433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:51:54  来源:igfitidea点击:

Monitor all JavaScript events in the browser console

javascriptevents

提问by knoopx

Is it possible to listen to all javascript events?

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

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

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

回答by Xavi

With firebugor web inspectoryou can use monitorEvents:

使用firebugweb 检查器,您可以使用monitorEvents

monitorEvents(myDomElem);

This prints all events emitted by myDomElemto the console. Use unmonitorEventsto stop monitoring events.

这将打印由myDomElem控制台发出的所有事件。使用unmonitorEvents停止监视事件。

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

如果您对在操作 DOM 后获取事件感兴趣,请查看Mutation Events

Edit:

编辑

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

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

(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.

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

回答by Malenx

To piggy back off Xavi's answer of monitorEvents(myDomElem), if you wanted everything but the mouse events, you could then enter unmonitorEvents(myDomElem, 'mouse').

为了支持 Xavi 的回答monitorEvents(myDomElem),如果您想要除鼠标事件之外的所有内容,则可以输入unmonitorEvents(myDomElem, 'mouse')

http://www.briangrinstead.com/blog/chrome-developer-tools-monitoreventshas a good article in regards to using chrome monitor events.

http://www.briangrinstead.com/blog/chrome-developer-tools-monitorevents有一篇关于使用 chrome 监视器事件的好文章。