Android WeView interaction in js

Copyright:'ll flowering tree https://blog.csdn.net/qiaoshi96_bk/article/details/81369067

Foreword

To meet the project to share the contents of the page by native h5, but demand is relatively wonderful product manager:

如果当前页面存在分享方法则去调取页面分享的方法,如果没有则取页面的第一张图片为分享图片,description为分享内容

When the heart needs to get stupefied, FML this operation but there are adults in the product or find a way to reality.

Thinking

I was like, we can get through WebView title and URL of the page but the page has to share methods have their own get when demand for the product, and this is certainly related to js interaction, then go page first picture it? The first reaction is a grab bag, then think unrealistic, so both performance data captured data to process is not the best policy. Think Later, js usually can be controlled h5 logic, such as click events, so if I implanted a js code h5 page, and then find a trigger point is not to execute him could get what I want it? Facts have proved useful drop ponder:

js interaction

Our primary method js how to get it?
Here not say details:

settings.setJavaScriptEnabled(true);// 如果访问的页面中有Javascript,则webview必须设置支持Javascript。

This is a must have. Then we can

  mWebView.setWebViewClient(new WebViewClient()

This method to rewrite

  public void onPageFinished(WebView view, String url) {//通过view.getTitle方法拿到标题
  // 添加js接口,实现往浏览器添加进一个js对象,名称为:WebViewJavascriptBridge
        webview.addJavascriptInterface(new CTOJavaScriptCallback(context), "WebViewJavascriptBridge");

Then notes:

    @JavascriptInterface
    public void getShareInfo(String result) {
        results = result;
    }

This method can get what you want, the premise is

WebViewJavascriptBridge 以及 getShareInfo是你和js那边约定好的方法

H5 page execution method

How to get the value h5 page I want it?
In fact, just by

        //无图片时获取页面第一张图片作为分享图片
                mWebView.loadUrl("javascript:(function(){" +
                        "var objs = document.getElementsByTagName(\"img\"); " +
                        "        WebViewJavascriptBridge.Image(objs[0].src);  " +
                        "})()");
                //获取描述
                mWebView.loadUrl("javascript:(function(){" +
                        "var str = document.getElementsByTagName(\"meta\")[\"description\"].getAttribute(\"content\");" +
                        "WebViewJavascriptBridge.Javavalue(str);" +
                        "})()");

Js load this section even in onPageFinished and by onPageFinished conditions to trigger so that we can get the return value:

   //获取描述
    @JavascriptInterface
    public void Javavalue(String description) {
        WebviewActivity.getHtmlDesc(description);
    }
    //获取第一张图片
    @JavascriptInterface
    public static void Image(String image) {
        WebviewActivity. getImageView(image);
    }

In this way, it should be noted that: Java is by String style to load js here to go to our attention mosaic js to escape it.
WebViewJavascriptBridge: js is of a specific third-party libraries can be seen: WebViewJavascriptBridge Comments

Guess you like

Origin blog.csdn.net/qiaoshi96_bk/article/details/81369067