5

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

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
robert
  • 321
  • 2
  • 9
  • 20
  • 4 spaces before a line formats it as code. To do this, select the lines and type `ctr-k` – Peter Ajtai Oct 03 '10 at 05:53
  • I'm confused as to why you can't just delete or comment out that line? – Peter Ajtai Oct 03 '10 at 05: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 Oct 04 '10 at 09:43

6 Answers6

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>
Andrew
  • 14,204
  • 15
  • 60
  • 104
  • I have to "clear" /avoid that popup from c#, i dont have access code to that website, is just a random website which block my entire app with the popup – robert Oct 03 '10 at 06:14
  • It sounds like they don't want your application to access their content. Is this true? – Andrew Oct 03 '10 at 06:22
  • nop is just a random page, i just discover I can't find and rewrite javascript when the javascriot is alone. if the source code it has only this code how you can block that popup – robert Oct 03 '10 at 06:37
  • You basically have only two options. 1) You modify the document by removing the alert statement; 2) You modify the DOM by inserting new code that overrides the alert statement. It sounds like option 1 is a no-go, if you truly can't target this particular site (if it's actually a "random site" like you say). So you have to go with option 2. You can do this by placing the "override" code above as high as you can in the ``, but after any `` tags. Incidentally, what does the alert say? – Andrew Oct 03 '10 at 07:10
  • i don't know is not relevant, it could say hi – robert Oct 04 '10 at 05:34
  • It could also say, 1) "Go away", or "Don't steal our content." Or it could say, 2) "You need to be logged in to post content." If it is #1, then what you are doing is probably unethical (and I shouldn't even be helping you). If it's #2, then you have a completely different technical issue with your C#. I've looked through the source code of the link you provided and it does look like maybe you are doing a POST rather than a GET, which could be why you are getting the popup in the first place. – Andrew Oct 04 '10 at 08:40
5

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.

luvieere
  • 37,065
  • 18
  • 127
  • 179
  • I'd recommend this [answer](http://stackoverflow.com/a/6222430/326608) for a managed-only solution for injecting javascript that doesn't require deploying a PIA like mshtml does. – user326608 May 14 '14 at 06:44
4

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

Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
2

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
1

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
  }
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • sorry I don't understant that, where i have to put that code?? to replace I could use WebBrowser1.DocumentText = WebBrowser1.DocumentText.Replace("alert", ""); – robert Oct 03 '10 at 05:28
  • @robert: this code is put in the constructor of the form. And you can't just replace `alert` with empty. because `alert("hello");` will be `("hello");`, which propably results in js errors. You must find a proper method to replace, it really depends. – Cheng Chen Oct 03 '10 at 05:37
  • do you mean i have to put the code in private void InitializeComponent() { HERE ?? – robert Oct 03 '10 at 05:45
  • I put that WebBrowser1.Navigated += (sender, args) => { var document = (sender as WebBrowser1).DocumentText; //find the alert scripts and remove/replace them }; I'm getting this error: .main.WebBrowser1' is a 'field' but is used like a 'type' the name of my webbrowser control is WebBrowser1 – robert Oct 03 '10 at 05:57
-1

You can disable all popups by setting

webBrowser.ScriptErrorsSuppressed = true;

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

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283