Android WebView hides part of H5 content in onPageFinished is invalid

Reason: the webView has not been drawn successfully, and the id, class, label, etc. corresponding to H5 cannot be obtained by calling js.

Solution: Use js to modify H5 after the webView is drawn.

xml:

<WebView
    android:id="@+id/webView"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

java:

//1. Load H5 
webView.loadUrl("https://www.fifa.com/football-development/fifa-forward/impact-map"); 
//2. Monitor screen drawing, after the WebView drawing is completed, use JS modify H5 
webView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        //Modify String here 
        javascript = "javascript:function hide() { " + 
                "document.getElementsByTagName(' header')[1].style.display='none';"+ 
                "document.getElementsByTagName('footer')[0].style.display='none';"+ 
                "}"; 
        webView.loadUrl(javascript) ; 
        webView.loadUrl("javascript:hide()");
    }
});

end...

Guess you like

Origin blog.csdn.net/qq_41873558/article/details/127633073