views:

216

answers:

4

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

+1  A: 

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
  }
Danny Chen
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
@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.
Danny Chen
do you mean i have to put the code in private void InitializeComponent() {HERE ??
robert
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
+2  A: 

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
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
It sounds like they don't want your application to access their content. Is this true?
Andrew
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 <script>alert('random stuff');</script> how you can block that popup
robert
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 `<head>`, but after any `<meta>` tags. Incidentally, what does the alert say?
Andrew
i don't know is not relevant, it could say hi
robert
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
A: 

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

Sheng Jiang 蒋晟
A: 

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