最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Stop alert javascript popup in webbrowser c# control - Stack Overflow

programmeradmin6浏览0评论

This website : .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??

This website : http://blog.joins./media/folderListSlide.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??

Share Improve this question edited Oct 3, 2010 at 5:53 Peter Ajtai 57.7k13 gold badges123 silver badges141 bronze badges asked Oct 3, 2010 at 4:41 robertrobert 3212 gold badges9 silver badges20 bronze badges 3
  • 4 spaces before a line formats it as code. To do this, select the lines and type ctr-k – Peter Ajtai Commented Oct 3, 2010 at 5:53
  • I'm confused as to why you can't just delete or ment out that line? – Peter Ajtai Commented Oct 3, 2010 at 5:58
  • I'm sorry to say this, but this question and @robert's previous questions are all vaguely bot-ish. Proxies; automated form-submission; suppressing browser alerts without sendkeys. Fishy. Very fishy. – Andrew Commented Oct 4, 2010 at 9:43
Add a ment  | 

6 Answers 6

Reset to default 5

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./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)

  1. 暂无评论