Android Studio:尝试删除webview中的HTML元素:空白页面

时间:2021-09-25 20:52:03

I am trying to remove an HTML element in webview. Here is my code. Everything compiles fine.

我正在尝试删除webview中的HTML元素。这是我的代码。一切都很好。

I have looked at other questions and none of the answers work for me.

我看了其他问题,但没有一个答案适合我。

// load web url
final WebView webView = (WebView) mRootView.findViewById(R.id.fragment_main_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('publicTabs')[0].style.display=\"none\"; " + "})()");
    }
});
webView.loadUrl(mUrl);

When I open the app on my phone, no webpage loads. Just blank. If I remove:

当我在手机上打开应用程序时,没有网页加载。只是空白。如果我删除:

webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript:(function() { " + "document.getElementsByTagName('publicTabs')[0].style.display=\"none\"; " + "})()");
    }
});

the webpage loads.

网页加载。

Also, can someone tell me how to remove multiple HTML elements at the same time?

另外,有人可以告诉我如何同时删除多个HTML元素吗?

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


0  

Heres the answers: Also check if the element is a class or Id in the website. Like the one below in my code.

下面是答案:还要检查元素是网站中的类还是Id。就像我的代码中的下面那个。

String url = "https://www.example.com";


        final WebView webView = (WebView) findViewById(R.id.fragment_main_webview);


        webView.getSettings().setJavaScriptEnabled(true);

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.getSettings().setSavePassword(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setLightTouchEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                webView.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('header home_header')[0].style.display='none'; " +
                        "document.getElementsByClassName('glyphicon glyphicon-menu-hamburger')[0].style.display='none'; " +
                        "document.getElementsByClassName('searchBottomLinks_index searchBottomLinks_internal')[0].style.display='none'; " +
                        "document.getElementsByClassName('list-unstyled list-inline')[0].style.display='none'; " +
                        "document.getElementsByClassName('row')[0].style.display='none'; "+
                        "document.getElementsByClassName('header home_header')[0].style.display='none'; " +
                        "document.getElementsById('header-inner')[0].style.display='none'; " +
                        "document.getElementsById('fixed-header')[0].style.display='none'; " +
                        "document.getElementsByClassName('collapse navbar-collapse')[0].style.display='none'; " +
                        "document.getElementsByClassName('navbar navbar-inverse sidebars')[0].style.display='none'; " +
                        "document.getElementsByClassName('footer')[0].style.display='none'; " +
                        "document.getElementsByClassName('footer2')[0].style.display='none'; " +
                        "document.getElementsByClassName('container')[0].style.display='none'; " +
                        "document.getElementsByClassName('acad-slider')[0].style.display='none'; " +



                        "})()");
            }
        });
        webView.loadUrl("https://www.example.com");

    }
}

#1


0  

Heres the answers: Also check if the element is a class or Id in the website. Like the one below in my code.

下面是答案:还要检查元素是网站中的类还是Id。就像我的代码中的下面那个。

String url = "https://www.example.com";


        final WebView webView = (WebView) findViewById(R.id.fragment_main_webview);


        webView.getSettings().setJavaScriptEnabled(true);

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.getSettings().setSavePassword(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setLightTouchEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                webView.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('header home_header')[0].style.display='none'; " +
                        "document.getElementsByClassName('glyphicon glyphicon-menu-hamburger')[0].style.display='none'; " +
                        "document.getElementsByClassName('searchBottomLinks_index searchBottomLinks_internal')[0].style.display='none'; " +
                        "document.getElementsByClassName('list-unstyled list-inline')[0].style.display='none'; " +
                        "document.getElementsByClassName('row')[0].style.display='none'; "+
                        "document.getElementsByClassName('header home_header')[0].style.display='none'; " +
                        "document.getElementsById('header-inner')[0].style.display='none'; " +
                        "document.getElementsById('fixed-header')[0].style.display='none'; " +
                        "document.getElementsByClassName('collapse navbar-collapse')[0].style.display='none'; " +
                        "document.getElementsByClassName('navbar navbar-inverse sidebars')[0].style.display='none'; " +
                        "document.getElementsByClassName('footer')[0].style.display='none'; " +
                        "document.getElementsByClassName('footer2')[0].style.display='none'; " +
                        "document.getElementsByClassName('container')[0].style.display='none'; " +
                        "document.getElementsByClassName('acad-slider')[0].style.display='none'; " +



                        "})()");
            }
        });
        webView.loadUrl("https://www.example.com");

    }
}