IBrowserInjectJsHandler Property |
Namespace: DotNetBrowser.Browser
IHandler<InjectJsParameters> InjectJsHandler { get; set; }
Exception | Condition |
---|---|
ObjectDisposedException | The IBrowser has already been disposed. |
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.