Starting from DotNetBrowser 1.8.1 it is possible to simulate any DOM event for any particular node.


The following sample demonstrates how to simulate a custom event for a DOM node and handle it in DotNetBrowser.

C#

using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.DOM.Events;
using DotNetBrowser.Events;
using DotNetBrowser.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace DOMCreateEventSample
{
    class Program
    {
       public class WindowMain : Window
       {
           private WPFBrowserView browserView;
           public WindowMain()
           {
               Browser browser = BrowserFactory.Create();
               browserView = new WPFBrowserView(browser);
               browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
               {
                   if (e.IsMainFrame)
                   {
                       DOMDocument document = e.Browser.GetDocument();
                       var myEvent = browser.CreateEvent("MyEvent");
                       DOMNode root = document.GetElementById("root");
                       DOMEventHandler domEvent = delegate(object s, DOMEventArgs evt)
                       {
                           if (evt.EventType == myEvent.EventType)
                           {
                               DOMNode textNode = document.CreateTextNode("Some text");
                               DOMElement paragraph = document.CreateElement("p");
                               paragraph.AppendChild(textNode);
                               root.AppendChild(paragraph);
                           }
                       };
                       root.AddEventListener(myEvent, domEvent, false);
                       Thread.Sleep(3000);
                       root.DispatchEvent(myEvent);
                   }
               };
               Content = browserView;
               Width = 1024;
               Height = 768;
               this.Loaded += WindowMain_Loaded;
           }
           void WindowMain_Loaded(object sender, RoutedEventArgs e)
           {
               browserView.Browser.LoadHTML("<html><body><div id='root'></div></body></html>");
           }
           [STAThread]
           public static void Main()
           {
               Application app = new Application();
               WindowMain wnd = new WindowMain();
               app.Run(wnd);
               var browser = wnd.browserView.Browser;
               wnd.browserView.Dispose();
               browser.Dispose();
           }
       }
    }
}

VB.NET

Imports System.Threading
Imports System.Windows
Imports DotNetBrowser
Imports DotNetBrowser.DOM
Imports DotNetBrowser.DOM.Events
Imports DotNetBrowser.WPF

Module Module1
    Public Class WindowMain
        Inherits Window

        Public Dim browserView As WPFBrowserView

        Public Sub New()

            Dim browser As Browser = BrowserFactory.Create()
            browserView = New WPFBrowserView(browser)
            AddHandler browser.FinishLoadingFrameEvent, sub(o, e)

                If e.IsMainFrame Then
                    Dim document As DOMDocument = e.Browser.GetDocument()
                    Dim myEvent = browser.CreateEvent("MyEvent")
                    Dim root As DOMNode = document.GetElementById("root")

                    Dim domEvent As DOMEventHandler = Sub(s As Object, evt As DOMEventArgs)
                        If evt.EventType = myEvent.EventType Then
                            Dim textNode As DOMNode = document.CreateTextNode("Some text")
                            Dim paragraph As DOMElement = document.CreateElement("p")
                            paragraph.AppendChild(textNode)
                            root.AppendChild(paragraph)
                        End If
                    End Sub
                    root.AddEventListener(myEvent, domEvent, False)
                    Thread.Sleep(3000)
                    root.DispatchEvent(myEvent)
                End If
            End sub

            Content = browserView
            Width = 1024
            Height = 768
            AddHandler Me.Loaded, AddressOf WindowMain_Loaded
        End Sub

        Private Sub WindowMain_Loaded(sender As Object, e As RoutedEventArgs)
            browserView.Browser.LoadHTML("<html><body><div id='root'></div></body></html>")
        End Sub
    End Class

    <STAThread>
    Sub Main()
        Dim app As Application = New Application()

        Dim wnd As WindowMain = New WindowMain()
        app.Run(wnd)

        Dim browser = wnd.browserView.Browser
        wnd.browserView.Dispose()
        browser.Dispose()
    End Sub
End Module