提交 0a8d32b4 编写于 作者: M Martin Zikmund

chore: Provide separate implementation of legacy InvokeScriptAsync behavior

上级 151754ff
......@@ -99,6 +99,16 @@ public partial class CoreWebView2
return _nativeWebView.ExecuteScriptAsync(javaScript, ct);
});
internal async Task<string?> InvokeScriptAsync(string script, string[]? arguments, CancellationToken ct)
{
if (_nativeWebView is null)
{
return null;
}
return await _nativeWebView.InvokeScriptAsync(script, arguments, ct);
}
internal void OnOwnerApplyTemplate()
{
_nativeWebView = GetNativeWebViewFromTemplate();
......
......@@ -160,8 +160,6 @@ internal class NativeWebViewWrapper : INativeWebView
_webView.LoadDataWithBaseURL(null, html, "text/html; charset=utf-8", "utf-8", null);
}
//_owner should be IAsyncOperation<string> instead of Task<string> but we use an extension method to enable the same signature in Win.
//IAsyncOperation is not available in Xamarin.
async Task<string> INativeWebView.ExecuteScriptAsync(string script, CancellationToken token)
{
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
......@@ -175,6 +173,20 @@ internal class NativeWebViewWrapper : INativeWebView
return await tcs.Task;
}
async Task<string> INativeWebView.InvokeScriptAsync(string script, string[] arguments, CancellationToken ct)
{
var argumentString = Windows.UI.Xaml.Controls.WebView.ConcatenateJavascriptArguments(arguments);
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
ct.Register(() => tcs.TrySetCanceled());
_webView.EvaluateJavascript(
string.Format(CultureInfo.InvariantCulture, "javascript:{0}(\"{1}\");", script, argumentString),
new ScriptResponse(value => tcs.SetResult(value)));
return await tcs.Task;
}
// On Windows, the WebView ignores "about:blank" entries from its navigation history.
// Because Android doesn't let you modify the navigation history,
// we need CanGoBack, CanGoForward, GoBack and GoForward to take the above condition into consideration.
......
......@@ -25,5 +25,7 @@ internal partial interface INativeWebView
Task<string?> ExecuteScriptAsync(string script, CancellationToken token);
Task<string?> InvokeScriptAsync(string script, string[]? arguments, CancellationToken token);
void SetScrollingEnabled(bool isScrollingEnabled);
}
......@@ -644,6 +644,36 @@ public partial class UnoWKWebView : WKWebView, INativeWebView, IWKScriptMessageH
return await tcs.Task;
}
async Task<string> INativeWebView.InvokeScriptAsync(string script, string[] arguments, CancellationToken ct)
{
var argumentString = Windows.UI.Xaml.Controls.WebView.ConcatenateJavascriptArguments(arguments);
var javascript = string.Format(CultureInfo.InvariantCulture, "javascript:{0}(\"{1}\")", script, argumentString);
if (this.Log().IsEnabled(Uno.Foundation.Logging.LogLevel.Debug))
{
this.Log().Debug($"EvaluateJavascriptAsync: {javascript}");
}
var tcs = new TaskCompletionSource<string>();
using (ct.Register(() => tcs.TrySetCanceled()))
{
EvaluateJavaScript(javascript, (result, error) =>
{
if (error != null)
{
tcs.TrySetException(new InvalidOperationException($"Failed to execute javascript {error.LocalizedDescription}, {error.LocalizedFailureReason}, {error.LocalizedRecoverySuggestion}"));
}
else
{
tcs.TrySetResult(result as NSString);
}
});
return await tcs.Task;
}
}
private void ProcessNSUrlRequest(NSUrlRequest request)
{
if (request == null)
......
......@@ -59,16 +59,23 @@ public partial class WebView : Control, IWebView
public IAsyncOperation<string?> InvokeScriptAsync(string scriptName, IEnumerable<string> arguments) =>
AsyncOperation.FromTask(ct => InvokeScriptAsync(ct, scriptName, arguments?.ToArray()));
public async Task<string?> InvokeScriptAsync(CancellationToken ct, string script, string[]? arguments)
{
var argumentString = ConcatenateJavascriptArguments(arguments);
var javaScript = string.Format(CultureInfo.InvariantCulture, "{0}(\"{1}\")", script, argumentString);
return AdjustInvokeScriptResult(await CoreWebView2.ExecuteScriptAsync(javaScript));
}
public async Task<string?> InvokeScriptAsync(CancellationToken ct, string script, string[]? arguments) =>
await CoreWebView2.InvokeScriptAsync(script, arguments, ct);
public void NavigateWithHttpRequestMessage(global::System.Net.Http.HttpRequestMessage requestMessage) =>
CoreWebView2.NavigateWithHttpRequestMessage(requestMessage);
internal static string ConcatenateJavascriptArguments(string[]? arguments)
{
var argument = string.Empty;
if (arguments != null && arguments.Length > 0)
{
argument = string.Join(",", arguments);
}
return argument;
}
private void CoreWebView2_DocumentTitleChanged(CoreWebView2 sender, object args) =>
DocumentTitle = sender.DocumentTitle;
......@@ -114,30 +121,4 @@ public partial class WebView : Control, IWebView
private void CoreWebView2_UnsupportedUriSchemeIdentified(CoreWebView2 sender, WebViewUnsupportedUriSchemeIdentifiedEventArgs args) =>
UnsupportedUriSchemeIdentified?.Invoke(this, args);
private static string? AdjustInvokeScriptResult(string result)
{
if (result is null)
{
return null;
}
if (result.StartsWith("\"", StringComparison.Ordinal) && result.EndsWith("\"", StringComparison.Ordinal))
{
return result.Substring(1, result.Length - 2);
}
return "";
}
private static string ConcatenateJavascriptArguments(string[]? arguments)
{
var argument = string.Empty;
if (arguments != null && arguments.Length > 0)
{
argument = string.Join(",", arguments);
}
return argument;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册