Accessing html response content in Xamarin browser control

前端 未结 1 1678
轻奢々
轻奢々 2021-01-15 01:14

I have what seems to be an unusual requirement that my colleagues and I haven\'t been able to implement in our Xamarin project. What we are trying to do is dynamically proce

1条回答
  •  -上瘾入骨i
    2021-01-15 02:10

    Both these device-dependent techniques use JavaScript evaluation/execution after the HTML page has finished loading in order for to receive the entire html contents as a JavaScript result (edit the JavaScript in the examples if your requirements to not require the entire page to be return).

    Xamarin.Android

    You can capture the loaded html within a WebView by assigning a subclassed WebViewClient that implements IValueCallback. The OnReceiveValue method will contain the html content after the page has finished loading:

    Example WebViewClient Class:

    public class EmbeddedWebViewClient : WebViewClient, IValueCallback
    {
        public EmbeddedWebViewClient(WebView view)
        {
            view.Settings.JavaScriptEnabled = true;
        }
    
        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
            Log.Info("SO", $"Page loaded from: {url}");
            view.EvaluateJavascript("(function() { return (''+document.getElementsByTagName('html')[0].innerHTML+''); })();", this);
        }
    
        public void OnReceiveValue(Java.Lang.Object value)
        {
            // "value" holds the html contents of the loaded page
            Log.Debug("SO", ((string)value).Substring(0, 40));
        }
    }
    

    Example WebViewClient usage:

    WebView webview = FindViewById(Resource.Id.webview);
    webview.SetWebViewClient(new EmbeddedWebViewClient(webview));
    webview.LoadUrl("https://xamarin.com");
    

    Xamarin.iOS

    You can capture the loaded html within a WKWebView by assigning a IWKNavigationDelegate to it. Either implement it within its own class or the controller that contains the WKWebView

    Example IWKNavigationDelegate Implementation:

    public class NavigationDelegate : NSObject, IWKNavigationDelegate
    {
        [Export("webView:didFinishNavigation:")]
        public async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            var content = await webView.EvaluateJavaScriptAsync("(function() { return (''+document.getElementsByTagName('html')[0].innerHTML+''); })();");
            var html = FromObject(content);
            Console.WriteLine((html.ToString()).Substring(0, 40));
        }
    }
    

    Example WKWebView usage:

    wkwebview = new WKWebView(UIScreen.MainScreen.Bounds, new WKWebViewConfiguration());
    wkwebview.NavigationDelegate = new NavigationDelegate();
    Add(wkwebview);
    wkwebview.LoadRequest(new NSUrlRequest(new Uri("https://xamarin.com")));
    

    Note: This same basic logic can be done with a UIWebView, but WKWebView is so much faster that if you do not need to support iOS 7 clients, I would personally focus on using WKWebView

    0 讨论(0)
提交回复
热议问题