How I get page source from WebView?

后端 未结 3 1916
[愿得一人]
[愿得一人] 2020-12-08 12:21

How I get the web page\'s source from WebView?

I want to only enter www.google.com in my webview and When I entered this site, I want to get the source for example <

相关标签:
3条回答
  • 2020-12-08 12:58

    I am not sure how far this is going to be helpful. But I have used the below snippet to fetch a small html page's data. I hope it helps you.

    Create a class like the one below,

      class MyJavaScriptInterface
      {
          @SuppressWarnings("unused")
          public void processHTML(final String html)
          {
              Log.i("processed html",html);
    
                Thread OauthFetcher=new Thread(new Runnable() { 
    
                    @Override
                    public void run() {
    
                        String oAuthDetails=null;
                          oAuthDetails=Html.fromHtml(html).toString();
                          Log.i("oAuthDetails",oAuthDetails);
    
                    }
                });OauthFetcher.start();
            }
          } 
    

    Now in your onCreate(),

     webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    
         webview.setWebViewClient(new WebViewClient(){
    
                @Override
                public void onPageFinished(WebView view, final String url) {
    
    
                    String oAuthUrl=getString("www.google.com");
    
                    if(url.contains(oAuthUrl))
                    {
                        Log.i("Contains","Auth URL");
    
                        twitter_webview.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
                    }
                }
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
                    progressDialog.show();
                }
          });
    

    And now what happens is that, when your page finishes loading, the JavaScript class will be called, which would retrieve the page source and store it in a String as your requirement.

    0 讨论(0)
  • 2020-12-08 13:01

    And For API 17

    import android.webkit.JavascriptInterface;
    
    public class MainActivity extends Activity {
    
    final Context myApp = this;
    
    @JavascriptInterface
    public void processHTML(String html) {
        if (html == null)
            return;
    
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WebView browser = (WebView) findViewById(R.id.webview);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.addJavascriptInterface(this, "HTMLOUT");
        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });
        browser.loadUrl("http://www.google.co.il");
    }
    
    }
    
    0 讨论(0)
  • 2020-12-08 13:17

    If your minSdkVersion is 19 or greater you can use the following.

    
    override fun onPageFinished(view: WebView?, url: String?) {
        super.onPageFinished(view, url)
        view?.evaluateJavascript("""(function() {
            return "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>";
        })()""".trimMargin()) {
            console.log(it)
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题