Android WebView blocks ads in web pages

demand

There are often advertisements in some webpages loaded by WebView, and the advertisements need to be removed. You can use js to hide the corresponding advertisement blocks. The effect here is that only the content of the list is displayed.

effect

  • Original effect
    Before effect
  • Effect after shielding
    Insert picture description here

Code

  • Core JS code
document.querySelector('#siteWrapper > header').style.display="none";
  • WebView settings
    hidden code written in the WebView setWebViewClient methods below onProgressChanged inside
WebViewClient client=new WebViewClient(){
    
    
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    
    
            view.loadUrl(request.getUrl().toString());
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    
    
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
    
    
            String fun="javascript:" +
                    "function hideStyle(){" +
                    "document.querySelector('#siteWrapper > header').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > div.wrapper > section.boxItem.boxItemAdd').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > div.wrapper > section.boxItem.toggleBox.pairsWrapper > div').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > footer > div.footerBottom').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > footer > div:nth-child(4)').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > footer > div:nth-child(3)').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > footer > div:nth-child(2)').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > footer > div:nth-child(1)').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > section.boxItem.disclaimer.js-toggled-box.js-disclaimer-box').style.display=\"none\";" +
                    "document.querySelector('#div-gpt-ad-1333374405327-0').style.display=\"none\";" +
                    "document.querySelector('#div-gpt-ad-1338276709958-0').style.display=\"none\";" +
                    "document.querySelector('#siteWrapper > div.adDrawer').style.display=\"none\";" +
                    "}" +
                    "hideStyle();";
            view.loadUrl(fun);

        }
    };

step

Open the web page link address in the browser, press F12 to view the source code of the web page, select the copy selector on the div
gif
, and over

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/108201629