开发者

Stop alert javascript popup in webbrowser c# control

This website : http://blog.joins.com/media/folderList开发者_JAVA百科Slide.asp?uid=ddatk&folder=3&list_id=9960150

has this code:

<script>alert('¿Ã¹Ù¸¥ Çü½ÄÀÌ ¾Æ´Õ´Ï´Ù.');</script>

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


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

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

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

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


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

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.mshtml and use mshtml; in your form.


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


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


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

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


You can disable all popups by setting

webBrowser.ScriptErrorsSuppressed = true;

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜