简体   繁体   English

如何检测 JavaScript 警报来自何处?

[英]How to detect where JavaScript alert is coming from?

I am trying to debug a very complex IE Intr.net application.我正在尝试调试一个非常复杂的 IE Intr.net 应用程序。 I am getting an alert with a custom message stating that an exception has occurred.我收到一条带有自定义消息的警报,指出发生了异常。 I would like to find out more information about this exception as the message is not very helpful.我想了解有关此异常的更多信息,因为该消息不是很有帮助。

There is a master page which contains a lot of iFrames (and.htc files if that makes a difference) so I don't think that I can try and hijack window.alert .有一个包含大量 iFrame 的母版页(如果有区别的话还有 .htc 文件)所以我不认为我可以尝试劫持 window.alert My last resort will be to try my luck with a file search.我最后的选择是通过文件搜索试试运气。

Using IE 8, is there anyway I can detect where this alert is coming from?使用 IE 8,我是否可以检测到此警报的来源? The ideal solution would be to somehow create a "breakOnAlert" function which inserts a debbuger statement at the correct alert location.理想的解决方案是以某种方式创建一个“breakOnAlert”function,它在正确的警报位置插入调试器语句。

To clarify: The master page contains many iframes and I believe that the error+alert is coming from one of these.澄清一下:母版页包含许多 iframe,我相信错误+警报来自其中之一。 Each iframe is an aspx page (sometimes with dynamic html/javascript from the user) and contains inline and external JavaScript. Before posting I did try overriding alert in my page (a child page inside an iframe) but it didn't work.每个 iframe 都是一个 aspx 页面(有时来自用户的动态 html/javascript)并包含内联和外部 JavaScript。在发布之前,我确实尝试覆盖我的页面(iframe 内的子页面)中的警报,但它没有用。 I am assuming that It doesn't work as each iframe has their own window object so they each have their own version of alert . 我假设 它不起作用,因为每个 iframe 都有自己的 window object 所以他们每个人都有自己的警报版本。 For this to work I would need to find all iframes and override it for each one, something which I think would be very complicated to do.为此,我需要找到所有 iframe 并为每个 iframe 覆盖它,我认为这样做会非常复杂。 In the IE developer tools I can see a huge amount of script files (inline and external), so it would be very difficult to manually look for the alerts in there.在 IE 开发人员工具中,我可以看到大量脚本文件(内联和外部),因此很难手动在其中查找警报。

Since it's a real chore to do it in all iframes, I'd probably use Fiddler and programatically replace alert( with something like:由于在所有 iframe 中执行此操作确实很麻烦,因此我可能会使用Fiddler并以编程方式将alert(替换为类似以下内容:

(function(n){alert(n);debugger;})(

IE should support the debugger statement, so you'd have a call-stack IE 应该支持debugger语句,所以你会有一个调用堆栈

This page explains how to do a text-replace in Fiddler本页解释了如何在 Fiddler 中进行文本替换

Example Fiddler custom rule to add to OnBeforeResponse :要添加到OnBeforeResponse的示例 Fiddler 自定义规则:

if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html"))
{
    oSession.utilDecodeResponse();
    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
    oBody = oBody.replace(/alert\(/gi, "(function(n){alert(n);debugger;})(");
    oSession.utilSetResponseBody(oBody);
}

Ovveride alert function and set a breakpoint inside, then you can watch Stack Trace:) Ovveride alert function 并在里面设置一个断点,然后你可以看到 Stack Trace :)

 function alert(message) {
     var x = 'whatever';
 }


 $(function () {
     alert('test');
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM