Search code examples
c#androidxamarinwebviewanglesharp

How to get data from the page in WebView? Android Xamarin


There is a web page (not mine and without API) from which I want to take data. An example of such a page

https://warthunder.com/en/community/userinfo/?nick=Keofox

The necessary data is in the following blocks:

<ul class = "profile-stat__list-sb">
<li class = "profile-stat__list-item"> sb</li>
<li class = "profile-stat__list-item"> 93 </li>
<li class = "profile-stat__list-item"> 64 </li>
<li class = "profile-stat__list-item"> 5 </li>

Previously everything worked through AngleSharp but recently added DDoS protection by Cloudflare. Accordingly, the parser does not work. Delay, parallel loading in WebView was unsuccessful.

The only possible solution (in my opinion) is to extract HTML code from an already loaded page in WebView (in WebView, the page passes the Cloudflare check and loads without problems).

  1. How to call an event like an "OnPageFinishedLoading"?
  2. How can I extract HTML code from WebView and use it?

Solution

  • you could use Custom WebViewClient and AddJavascriptInterface to achive it:

    protected override void OnCreate(Bundle savedInstanceState)
        {      
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_other);           
            webView = FindViewById<WebView>(Resource.Id.webView1);
            webView.SetWebViewClient(new WebViewClientClass());
            WebSettings websettings = webView.Settings;
            websettings.JavaScriptEnabled = true;
            websettings.DomStorageEnabled = true;
            webView.AddJavascriptInterface(new Foo(this), "Foo");
            webView.LoadUrl("file:///android_asset/demo.html");
        }
    
    
    class WebViewClientClass : WebViewClient
        {
            public override void OnReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, string host, string realm)
            {
    
            }
            public override void OnPageFinished(WebView view, string url)
            {
                view.LoadUrl("javascript:window.Foo.showSource("
                             + "document.getElementsByTagName('html')[0].innerHTML);");
                base.OnPageFinished(view, url);
            }
    
        }
    
    class Foo : Java.Lang.Object
    {
        Context context;
    
        public Foo(Context context)
        {
            this.context = context;
        }
        [JavascriptInterface]
        [Export]
        public void showSource(string html)
        {
            Log.Error("content", html);//here html is the HTML code
        }
    }