简体   繁体   中英

Capture a custom Javascript event in C# WPF mshtml WebBrowser control

I'm unable to capture a custom Javascript event in a C# WPF mshtml WebBrowser control. I've created the example code below. A button click raises a custom event. I realise that there are easy ways to capture a button click event. I need to use a custom event. I've used a button just for this example and testing. What am I doing wrong?

XAML file:

<Window x:Class="WebEvent2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WebBrowser Name="webBrowser1" Loaded="webBrowser1_Loaded" LoadCompleted="webBrowser1_LoadCompleted"></WebBrowser>
    </Grid>
</Window>

C# file:

namespace WebEvent2{
   public partial class MainWindow : Window{
      public MainWindow(){
         InitializeComponent();
      }

      private void webBrowser1_Loaded(object sender, RoutedEventArgs e{
         webBrowser1.Navigate(@"file:///D:/test.html");
      }

      private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e){
         try{
            var evtListener = new EventListener();
            var window = ((IHTMLDocument2)webBrowser1.Document).parentWindow as IHTMLWindow3;
            window.attachEvent("MyCustomEvent", evtListener);

            // Also not working:
            // ((HTMLDocument)webBrowser1.Document).attachEvent("MyCustomEvent", evtListener);
         }catch (UnauthorizedAccessException err){
            Console.WriteLine("OOPS: " + err);
         }
      }

      [ComVisible(true)]
      [ClassInterface(ClassInterfaceType.AutoDispatch)]
      public class EventListener{
         [DispId(0)]
         public void handler(IHTMLEventObj evt){
            MessageBox.Show("message received");
         }
      }
   }
}

HTML file:

<html>
<head>
  <title>Test page</title>
  <script>
    function sendCustomEvent() {
      var event;
      if (typeof(Event) === 'function') {
          event = new Event('MyCustomEvent');
      } else {
          event = document.createEvent('HTMLEvents');
          event.initEvent('MyCustomEvent', true, false);
      }

      document.dispatchEvent(event);
    }
  </script>
</head>
<body>
  <button onclick="sendCustomEvent()">Send custom event</button>
</body>
</html>

I was able to achieve the required functionality by using the WebBrowser controls 'ObjectForScripting' property. Nice and simple.

XAML remains the same as above.

WebInterface.cs:

namespace WebEvent2
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class WebInterface
    {
        public void callMe()
        {
            MessageBox.Show("hello");
        }
    }
}

MainWindow.xaml.cs:

namespace WebEvent2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        WebInterface wi = new WebInterface();

        private void webBrowser1_Loaded(object sender, RoutedEventArgs e)
        {
            webBrowser1.ObjectForScripting = wi;
            webBrowser1.Navigate(@"file:///D:/test.html");
        }
    }
}

test.html:

<html>
<head>
  <title>Test page</title>
</head>
<body>
  <button onclick="window.external.callMe()">Send custom event</button>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM