Web browser control: How to capture document events?

35,112

Solution 1

Ok Answer found - tested and it works:

  • Add a reference from the COM tab called: Microsoft HTML Object Library

The following is an example code:

You will need two components: WebBrowser (webBrowser1) and a TextBox (textBox1)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        webBrowser1.LoadCompleted += new LoadCompletedEventHandler(webBrowser1_LoadCompleted);
    }

    private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
    {
        mshtml.HTMLDocument doc;
        doc = (mshtml.HTMLDocument)webBrowser1.Document;
        mshtml.HTMLDocumentEvents2_Event iEvent;
        iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
        iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
    }

    private bool ClickEventHandler(mshtml.IHTMLEventObj e)
    {
        textBox1.Text = "Item Clicked";
        return true;
    }
}

Solution 2

Here is another example.

I was trying to inject a remote javascript file and execute some code when ready by adding a DOM element <script src="{path to remote file}" /> to the header, essentially the same idea as jQuery.getScript(url, callback)..

The code below works fine.

HtmlElementCollection head = browser.Document.GetElementsByTagName("head");
if (head != null)
{
    HtmlElement scriptEl = browser.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    element.src = url;
    element.type = "text/javascript";
    head[0].AppendChild(scriptEl);
    // Listen for readyState changes
    ((mshtml.HTMLScriptEvents2_Event)element).onreadystatechange += delegate
    {
        if (element.readyState == "complete" || element.readyState == "loaded")
        {
            Callback.execute(callbackId);
        }
    };
}
Share:
35,112
Bassem
Author by

Bassem

Updated on October 11, 2020

Comments

  • Bassem
    Bassem over 3 years

    I am using WPF's WebBrowser control to load a simple web page. On this page I have an anchor or a button. I want to capture the click event of that button in my application's code behind (i.e. in C#).

    Is there are a way for the WebBrowser control to capture click events on the loaded page's elements?

    In addition, is it possible to communicate event triggered data between the page and the WebBrowser? All of the above should be possible am I right?

    • Edit: Probable solution:

    I have found the following link that might be a solution. I haven't tested it yet but it's worth the shot. Will update this question depending on my test results.

    http://support.microsoft.com/kb/312777

  • Bassem
    Bassem about 12 years
    Since no other answers were given, I'm gonna select this as the answer. Although I found a better way to solve this issue. The method is described in codegain.com/articles/wpf/ieprogramming/… and it uses a class called ObjectForScriptingHelper. Very easy to implement and works like a charm.
  • Nick Thissen
    Nick Thissen over 8 years
    It seems to work only 20% of the time for me, most of the time it fails to call the "ClickEventHandler" even though the LoadCompleted method finishes completely with no exceptions that I can see... Any ideas? I posted about it here: stackoverflow.com/questions/35058874/…
  • Jim Simson
    Jim Simson about 7 years
    The codegain article referenced by @Bassem can now be found here: dotnetfunda.com/articles/show/840/…