在 webbrowser c# 控件中停止警报 javascript 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3848632/
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-10-25 02:27:47  来源:igfitidea点击:

Stop alert javascript popup in webbrowser c# control

c#javascriptbrowseralert

提问by robert

This website : http://blog.joins.com/media/folderListSlide.asp?uid=ddatk&folder=3&list_id=9960150

本网站:http: //blog.joins.com/media/folderListSlide.asp?uid=ddatk&folder=3&list_id=9960150

has this code:

有这个代码:

<script>alert('??1ù?¥ ?ü??àì ??′?′?′ù.');</script>

So my web browser control show a popup, how can I bypass the popup without using sendkeys enter??

所以我的网络浏览器控件显示一个弹出窗口,如何在不使用发送键输入的情况下绕过弹出窗口?

回答by luvieere

In the ProgressChangedevent handler, you insert a script element that replaces the Javascript alertfunction with a function of your own, that does nothing:

ProgressChanged事件处理程序中,您插入一个脚本元素,用alert您自己的函数替换 Javascript函数,它什么都不做:

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            string alertBlocker = "window.alert = function () { }";
            element.text = alertBlocker;
            head.AppendChild(scriptEl);
        }
    }

For this to work, you need to add a reference to Microsoft.mshtmland use mshtml;in your form.

为此,您需要在表单中添加对Microsoft.mshtml和的引用use mshtml;

回答by Andrew

If you intend not to ever use the alert()function on your page, you can also just override it. E.g.:

如果您不打算alert()在您的页面上使用该功能,您也可以覆盖它。例如:

<script type="text/javascript">
alert = function(){}
</script>

If you do need to use JavaScript's alert function, you can 'overload' it:

如果您确实需要使用 JavaScript 的警报功能,您可以“重载”它:

<script type="text/javascript">
var fnAlert = alert;
alert = function(message,doshow) {
    if (doshow === true) {
        fnAlert(message);
    }
}
alert("You won't see this");
alert("You will see this",true);
</script>

回答by u109919

handle IDocHostShowUI::ShowMessage and return S_OK. Check http://www.codeproject.com/KB/miscctrl/csEXWB.aspxfor an example.

处理 IDocHostShowUI::ShowMessage 并返回 S_OK。以http://www.codeproject.com/KB/miscctrl/csEXWB.aspx为例。

回答by abobjects.com

solution given is wrong

给出的解决方案是错误的

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
    {
        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
        IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
        string alertBlocker = "window.alert = function () { }";
        element.text = alertBlocker;
        head.AppendChild(scriptEl);
    }
}

Seems handling a windows hook for message is solution

似乎处理消息的 Windows 挂钩是解决方案

回答by Cheng Chen

I think you are navigating a page within alert(xxx)in its javascript using WebBroswerin a WinForm application? You can try:

我认为您正在alert(xxx)使用WebBroswerWinForm 应用程序在其 javascript中导航页面?你可以试试:

broswer.Navigated += (sender, args) =>
  {
     var document = (sender as WebBrowser).DocumentText;
     //find the alert scripts and remove/replace them
  }

回答by BlueRaja - Danny Pflughoeft

You can disable all popups by setting

您可以通过设置禁用所有弹出窗口

webBrowser.ScriptErrorsSuppressed = true;

Despite the name, this settings actually blocks allpopups, including alert()

尽管名称如此,但此设置实际上会阻止所有弹出窗口,包括alert()