System.Windows.Controls.WebBrowser

interaction from javascript in the Document to external C# code

[ComVisible(true)]
public class ScriptProxy
{
    // This method can be called from JavaScript.
    public void AMethod(string message)
    {
        MessageBox.Show(message);
    }
}

this.webBrowser1.ObjectForScripting = new ScriptProxy();
<input type="button" value="Go Again!" onclick="window.external.AMethod('Hello');" />

subscriber to browser events thanks to this answer

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class EventListener
{
    [DispId(0)]
    public void NameDoesNotMatter(object data)
    {

        ; 
    }
}

this.webBrowser1.LoadCompleted += (sender, e) =>
    {
        var document = (mshtml.IHTMLDocument2)this.webBrowser1.Document;
        var window = (mshtml.IHTMLWindow3)document.parentWindow;
        window.attachEvent("onscroll", new EventListener());
    };

Leave a Reply