1

I want to disable javascript alert on wpf(Windows Presentation Foundation.)

Javascript alert on wpf Wpf Image

But, I don't want to disable javascript alert on WebBrowser

Javascript allert on WebBrowser Webbrowser Image

WPF

private void ClickOKButton()
{
    IntPtr hwnd = FindWindow("#32770", "Web sayfasından ileti");
    hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
    uint message = 0xf5;
    SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero);
}

public MainWindow()
{
    InitializeComponent();

    wbBrowser.Navigate(new Uri("http://localhost/AlertWebProject"));
    wbBrowser.LoadCompleted += WbBrowserOnLoadCompleted;
    wbBrowser.Navigated += WbBrowser_Navigated;
}

private void WbBrowser_Navigated(object sender, NavigationEventArgs e)
{
    ClickOKButton();
}

private void WbBrowserOnLoadCompleted(object sender, NavigationEventArgs navigationEventArgs)
{
    ClickOKButton();
}

JavaScript

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
      <script>  
          function test() {
              alert("Test");
          }               
      </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  <button onclick="test();">Test</button>
    <p>dsadsadas</p>
    </div>
    </form>
</body>
</html>
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Ugur Senol
  • 13
  • 4
  • 3
    So you want to suppress alerts when they are in the WPF `WebBrowser` control, but not when they are in a regular web browser window (like Chrome)? Your best bet might be to inject some javascript in your WPF application to redefine `alert` – Matt Burland Jun 20 '17 at 13:07
  • @MattBurland That sounds like a good idea. I'd upvote it if you made it an answer ;) –  Jun 20 '17 at 14:09

1 Answers1

1

Your best bet might be to inject some javascript into the WebBrowser control to redefine alert. You can look at, for example, this question for how to inject javascript:

How to inject Javascript in WebBrowser control

Then all you need to do is inject a snippet like:

window.alert = function() {}

And now any call to alert will do nothing.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171