using System; using System.Threading.Tasks; namespace WebViewControl { partial class WebView { /// /// Registers an object with the specified name in the window context of the browser /// /// Name of the object that will be available on the JS window context. /// .Net object instance that will be bound to the JS proxy object. /// Optional method that allows intercepting every call. /// True if the object was registered or false if the object was already registered before public bool RegisterJavascriptObject(string name, object objectToBind, Func, object> interceptCall = null) { if (interceptCall == null) { interceptCall = target => target(); } return InnerRegisterJavascriptObject(name, objectToBind, interceptCall); } /// /// Registers an object with the specified name in the window context of the browser. /// Use this overload with async interceptor methods. /// /// Name of the object that will be available on the JS window context. /// .Net object instance that will be bound to the JS proxy object. /// Async method that allows intercepting every call. /// True if the object was registered or false if the object was already registered before public bool RegisterJavascriptObject(string name, object objectToBind, Func, Task> interceptCall) { return InnerRegisterJavascriptObject(name, objectToBind, interceptCall); } private bool InnerRegisterJavascriptObject(string name, object objectToBind, Func, T> interceptCall) where T : class { if (chromium.IsJavascriptObjectRegistered(name)) { return false; } T CallTargetMethod(Func target) { if (isDisposing) { return default; } var isAsync = false; try { JavascriptPendingCalls.AddCount(); if (isDisposing) { // check again, to avoid concurrency problems with dispose return default; } var result = interceptCall(target); if (result is Task task) { task.ContinueWith(t => JavascriptPendingCalls.Signal()); isAsync = true; } return result; } finally { if (!isAsync) { JavascriptPendingCalls.Signal(); } } } chromium.RegisterJavascriptObject(objectToBind, name, CallTargetMethod); return true; } /// /// Unregisters an object with the specified name in the window context of the browser /// /// public void UnregisterJavascriptObject(string name) { chromium.UnregisterJavascriptObject(name); } public Task EvaluateScript(string script, string frameName = MainFrameName, TimeSpan? timeout = null) { var jsExecutor = GetJavascriptExecutor(frameName); if (jsExecutor != null) { return jsExecutor.EvaluateScript(script, timeout: timeout); } return Task.FromResult(default(T)); } public void ExecuteScript(string script, string frameName = MainFrameName) { GetJavascriptExecutor(frameName)?.ExecuteScript(script); } public void ExecuteScriptFunction(string functionName, params string[] args) { ExecuteScriptFunctionInFrame(functionName, MainFrameName, args); } public void ExecuteScriptFunctionInFrame(string functionName, string frameName, params string[] args) { GetJavascriptExecutor(frameName)?.ExecuteScriptFunction(functionName, false, args); } public Task EvaluateScriptFunction(string functionName, params string[] args) { return EvaluateScriptFunctionInFrame(functionName, MainFrameName, args); } public Task EvaluateScriptFunctionInFrame(string functionName, string frameName, params string[] args) { var jsExecutor = GetJavascriptExecutor(frameName); if (jsExecutor != null) { return jsExecutor.EvaluateScriptFunction(functionName, false, args); } return Task.FromResult(default(T)); } protected void ExecuteScriptFunctionWithSerializedParams(string functionName, params object[] args) { ExecuteScriptFunctionWithSerializedParamsInFrame(functionName, MainFrameName, args); } protected void ExecuteScriptFunctionWithSerializedParamsInFrame(string functionName, string frameName, params object[] args) { GetJavascriptExecutor(frameName)?.ExecuteScriptFunction(functionName, true, args); } protected Task EvaluateScriptFunctionWithSerializedParams(string functionName, params object[] args) { return EvaluateScriptFunctionWithSerializedParamsInFrame(functionName, MainFrameName, args); } protected Task EvaluateScriptFunctionWithSerializedParamsInFrame(string functionName, string frameName, params object[] args) { var jsExecutor = GetJavascriptExecutor(frameName); if (jsExecutor != null) { return jsExecutor.EvaluateScriptFunction(functionName, true, args); } return Task.FromResult(default(T)); } } }