x
Main Site login Signup

Ignoring Javascript dialog

Hello i am currently developping a program where i inject javascript into a website for passing commands, but the problem i got here is when there are so many products, it shows some errors that i don't care . So i want to just ignore all those dialogs. I was searching on web and i found this:

  private void OnJavascriptDialog( object sender,JavascriptDialogEventArgs e )
           {
                   e.Handled = true;
           }

But it doesn't work, i mean it doesn't ignore dialog. What can i do?

Update: I am using WPF.

asked Jul 08, 2015 at 09:43 PM avatar image TCGaming 0
more ▼
(comments are locked)
10|1100 characters needed characters left

1 answer: sort voted first

When JavaScript dialog appears, you're supposed to press "OK" button inside for closing it - such action is equivalent to pressing "Enter" key. Because the method InjectKeyboardEvent is protected, you need to inherit your WebControl:

 public class WebControlEx : WebControl
 {
     public WebControlEx()
     {
         base.ShowJavascriptDialog += (object sender, JavascriptDialogEventArgs e) =>
         {
             e.Cancel = false;
             e.Handled = true;
             WebKeyboardEvent wke = new WebKeyboardEvent();
             wke.Type = WebKeyboardEventType.KeyDown;
             wke.VirtualKeyCode = VirtualKey.RETURN;
             base.InjectKeyboardEvent(wke);
             wke.Type = WebKeyboardEventType.KeyUp;
             base.InjectKeyboardEvent(wke);
         };
     }
 }
 

Don't forget to set ViewType of WebControlEx to Offscreen:

 WebControl1.ViewType = WebViewType.Offscreen;
answered Jul 09, 2015 at 05:46 AM avatar image Zagavarr 100
more ▼
(comments are locked)
avatar image TCGaming Jul 09, 2015 at 08:05 AM

Hello, i just copied into the code but the dialog(alert for this example) still shows up, and i can't make wke null because it is a WebKeyboardEvent.

avatar image Zagavarr Jul 09, 2015 at 05:01 PM

You're right about wke, instead of wke = null; it must be wke = default(WebKeyboardEvent);

However, the whole code is working fine for me. I tested it on the page http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert and JavaScript dialogs won't show up. Are you sure you've created WebControlEx programmatically, during runtime? Also, to avoid compiler errors, recommended to set platform target to x86.

Could you specify the website, you're working with. May be that website has some features, although I doubt it – the method InjectKeyboardEvent must act universally. Anyway, here is full text of MainWindow.xaml.cs

http://www.googledrive.com/host/0BzR230N4UxiJMGNicmRjMjhWbjA

10|1100 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.