Table of Contents

Property InjectJsHandler

Namespace
DotNetBrowser.Browser
Assembly
DotNetBrowser.dll

InjectJsHandler

Gets or sets a handler that is used when the document element has been created and a custom JavaScript can be injected into the document.

IHandler<InjectJsParameters> InjectJsHandler { get; set; }

Property Value

IHandler<InjectJsParameters>

Remarks

You can inject a custom JavaScript code using the ExecuteJavaScript(string, bool) method.This callback is intended to give you an opportunity to inject a .NET object into JavaScript code or inject a custom JavaScript code for further execution before any scripts are executed in the particular frame. For example:

Browser.InjectJsHandler = new Handler<InjectJsParameters>(args =>
    {
        IJsObject window = args.Frame.ExecuteJavaScript<IJsObject>("window").Result;
        window["myObject"] = new MyObject();
    });

The MyObject class may look like this:

public class MyObject
    {
        public string SayHelloTo(string firstName) => "Hello " + firstName + "!";
    }

When the property is set, you can call methods of the injected .NET object from JavaScript:

window.myObject.SayHelloTo('John');

This handler may be invoked several times for the same frame.

Important: you should avoid executing a JavaScript code that modifies the DOM tree of the web page being loaded.You must not use DotNetBrowser DOM API to remove the frame for which this callback is invoked, otherwise the render process will crash.

Important: the engine will be blocked until you return control from the callback.

Exceptions

ObjectDisposedException

The IBrowser has already been disposed.