Exemple #1
0
        async Task NavigationComplete(WKWebView webView, string filename, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource)
        {
            try
            {
                var widthString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetWidth");

                var width = double.Parse(widthString.ToString());

                var heightString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetHeight");

                var height = double.Parse(heightString.ToString());

                if (width < 1 || height < 1)
                {
                    taskCompletionSource.SetResult(new ToFileResult(true, "WebView has zero width or height"));
                    return;
                }

                webView.ClipsToBounds            = false;
                webView.ScrollView.ClipsToBounds = false;

                var bounds = webView.Bounds;
                webView.Bounds = new CGRect(0, 0, (nfloat)width, (nfloat)height);

                var scale = pageSize.Width / width;


                var snapshotConfig = new WKSnapshotConfiguration
                {
                    SnapshotWidth = pageSize.Width / Display.Scale
                };

                var image = await webView.TakeSnapshotAsync(snapshotConfig);

                if (image.AsPNG() is NSData data)
                {
                    var path = Path.Combine(ToPngService.FolderPath(), filename + ".png");
                    File.WriteAllBytes(path, data.ToArray());
                    taskCompletionSource.SetResult(new ToFileResult(false, path));
                    return;
                }
                webView.Bounds = bounds;
                taskCompletionSource.SetResult(new ToFileResult(true, "No data returned."));
            }
            catch (Exception e)
            {
                taskCompletionSource.SetResult(new ToFileResult(true, "Exception: " + e.Message + (e.InnerException != null
                    ? "Inner exception: " + e.InnerException.Message
                    : null)));
            }
            finally
            {
                webView.Dispose();
            }
        }
Exemple #2
0
    public async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
    {
        var content = await webView.EvaluateJavaScriptAsync("(function() { return ('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>'); })();");

        var html = FromObject(content);

        Console.WriteLine((html.ToString()).Substring(0, 40));
    }
Exemple #3
0
        /*
         * public void ToPdf(TaskCompletionSource<ToFileResult> taskCompletionSource, WebView xfWebView, string fileName)
         * {
         *  if (xfWebView.Effects.Any(e=>e is Forms9Patch.WebViewPrintEffect))
         *  {
         *
         *  }
         * }
         */

        async Task NavigationComplete(WKWebView webView, string filename, PageSize pageSize, PageMargin margin, TaskCompletionSource <ToFileResult> taskCompletionSource)
        {
            try
            {
                var widthString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetWidth");

                var width = double.Parse(widthString.ToString());

                var heightString = await webView.EvaluateJavaScriptAsync("document.documentElement.offsetHeight");

                var height = double.Parse(heightString.ToString());

                if (width < 1 || height < 1)
                {
                    taskCompletionSource.SetResult(new ToFileResult(true, "WebView has zero width or height"));
                    return;
                }

                webView.ClipsToBounds            = false;
                webView.ScrollView.ClipsToBounds = false;

                if (webView.CreatePdfFile(webView.ViewPrintFormatter, pageSize, margin) is NSMutableData data)
                {
                    var path = System.IO.Path.Combine(ToPngService.FolderPath(), filename + ".pdf");
                    System.IO.File.WriteAllBytes(path, data.ToArray());
                    taskCompletionSource.SetResult(new ToFileResult(false, path));
                    data.Dispose();
                    return;
                }
                taskCompletionSource.SetResult(new ToFileResult(true, "No data returned."));
            }
            catch (Exception e)
            {
                taskCompletionSource.SetResult(new ToFileResult(true, "Exception: " + e.Message + (e.InnerException != null
                    ? "Inner exception: " + e.InnerException.Message
                    : null)));
            }
            finally
            {
                webView.Dispose();
            }
        }
Exemple #4
0
        public Task <string> GetUserAgentAsync(CancellationToken cancellationToken)
        {
            if (!string.IsNullOrWhiteSpace(_userAgent))
            {
                return(Task.FromResult(_userAgent));
            }

            var taskCompletionSource = new TaskCompletionSource <string>();

            this.InvokeOnMainThreadIfNeeded(async() =>
            {
                try
                {
                    using (var webView = new WKWebView(CGRect.Empty, new WKWebViewConfiguration()))
                    {
                        webView.LoadHtmlString("<html></html>", null);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            taskCompletionSource.TrySetCanceled();

                            return;
                        }

                        var userAgent = await webView
                                        .EvaluateJavaScriptAsync("navigator.userAgent")
                                        .ConfigureAwait(false);

                        _userAgent = userAgent as NSString;

                        taskCompletionSource.TrySetResult(_userAgent);
                    }
                }
                catch (Exception exception)
                {
                    taskCompletionSource.TrySetException(exception);
                }
            });

            return(taskCompletionSource.Task);
        }
            public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
            {
                if (!_modelRef.TryGetTarget(out CustomAuthModel model))
                {
                    return;
                }

                if (!_closeRef.TryGetTarget(out var close))
                {
                    return;
                }

                if (!model.IsInitialized)
                {
                    model.IsInitialized = true;
                    model.IsBusy        = false;
                }

                var url = webView.Url;

                if (url == null)
                {
                    return;
                }

                if (!model.IsCallbackPath(url.Path))
                {
                    return;
                }

                var rawPageBody = await webView.EvaluateJavaScriptAsync("document.documentElement.outerText");

                var content = rawPageBody.ToString();

                RemoveAppleCookies();

                model.Login(content);

                close.Invoke();
            }
            async void HandleFormSubmission(WKWebView webView, Action <WKNavigationActionPolicy> decisionHandler)
            {
                var allInputsJson = await webView.EvaluateJavaScriptAsync(@"
					JSON.stringify(
						(new Array(...document.getElementsByTagName('input')))
							.map(i => ({k: i.name || i.id, v: i.value}))
							.filter(i => i.k))
				"                );

                var allInputs = Newtonsoft.Json.JsonConvert.DeserializeObject <List <KV> > (
                    allInputsJson.ToString());
                var currentAction = owner.eventsHandler.CurrentAction;

                if (currentAction?.Type == WebViewActionType.UploadForm)
                {
                    owner.eventsHandler.OnFormSubmitted(allInputs.Select(
                                                            i => new KeyValuePair <string, string>(i.k, i.v)).ToList());
                    decisionHandler(WKNavigationActionPolicy.Cancel);
                }
                else
                {
                    decisionHandler(WKNavigationActionPolicy.Allow);
                }
            }
Exemple #7
0
        static async Task <string> EvaluateJavaScript(WKWebView webView, string script)
        {
            var result = await webView.EvaluateJavaScriptAsync(script);

            return(result?.ToString() ?? "null");
        }
Exemple #8
0
 public static void Eval(this WKWebView platformWebView, IWebView webView, string script)
 {
     platformWebView.EvaluateJavaScriptAsync(script);
 }
Exemple #9
0
        public static async Task <string> ExecuteScriptAsync(WKWebView webview, string script)
        {
            var nsStringResult = await webview.EvaluateJavaScriptAsync(script);

            return(nsStringResult?.ToString());
        }
 public async override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
 {
     await webView.EvaluateJavaScriptAsync("addObservers();");
 }