Click or drag to resize

IBrowserInjectJsHandler Property

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.

Namespace:  DotNetBrowser.Browser
Assembly:  DotNetBrowser (in DotNetBrowser.dll) Version: 2.20.1
Syntax
C#
IHandler<InjectJsParameters> InjectJsHandler { get; set; }

Property Value

Type: IHandlerInjectJsParameters
Exceptions
ExceptionCondition
ObjectDisposedExceptionThe IBrowser has already been disposed.
Remarks

You can inject a custom JavaScript code using the ExecuteJavaScript(String, Boolean) 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.

See Also