简体   繁体   English

在Webbrowser控件中捕获Javascript警报

[英]Capturing Javascript Alert in Webbrowser Control

Using the webbrowswer control to cruise a site. 使用webbrowswer控件浏览站点。 Sometimes errors occur which involve a javascript popup box. 有时会发生涉及javascript弹出框的错误。 I would like to do a couple of things when this happens. 发生这种情况时,我想做几件事。

  1. Know when a javascript alert popups up. 知道何时弹出JavaScript警报。

I have used the LostFocus event with some success but anytime it losses focus that code runs which is annoying. 我已经成功地使用了LostFocus事件,但是每当它失去对代码运行的关注时,这很烦人。

  1. I would like to know the exact text that the alert box says. 我想知道警报框显示的确切文本。

I am not sure where to find the alert box object or cast it for use in C#. 我不确定在哪里可以找到警报框对象或将其强制转换为在C#中使用。 I looked all over the internet and couldn't find it. 我在互联网上四处张望,却找不到。

Any one any clue? 有任何线索吗?

If you're looking to only trap script error dialogs that appear, I would recommend trapping the window.onerror DOM event. 如果您只想捕获出现的脚本错误对话框,则建议捕获window.onerror DOM事件。 If you assign a handler for this event, the message, (script) file name and line number are passed as arguments, those are all things shown in the error dialog that pops up. 如果为该事件分配处理程序,则消息,(脚本)文件名和行号将作为参数传递,所有这些都将在弹出的错误对话框中显示。 Note that most users have scripting error dialogs switched off by default so it would be wise to honour this if the intended purpose is for a large audience. 请注意,大多数用户默认情况下都关闭了脚本错误对话框,因此,如果预期的目的是针对大量受众,则明智的做法是兑现这一点。

I'm not sure if there's an easier way, I've only worked with the old COM WebBrowser component. 我不确定是否有更简单的方法,我只使用了旧的COM WebBrowser组件。

Just do this: 只要这样做:

window.alert = function(txt) {
   // Do something
}

This will allow you to do a callback or anything else you want with the alert text. 这将使您可以对警报文本执行回调或其他任何所需的操作。

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

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