WebView拦截js请求
2022-11-24 22:34:00
michael007js
245
webView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view,String url) { WebResourceResponse response = null; if (url.contains("logo")) { try { InputStream localCopy = getAssets().open("michael007js.png"); response = new WebResourceResponse("image/png", "UTF-8", localCopy); } catch (IOException e) { e.printStackTrace(); } } return response; } }); setContentView(webView); webView.loadUrl("https://www.baidu.com");
其中WebResourceResponse需要设定三个属性,MIME类型,数据编码,数据(InputStream流形式)。
当然,直接new也行,例如:
//拦截百度APP阅读悬浮按钮js if (url.contains("mbdp02.bdstatic.com/static/landing-wise/js/share")) { return new WebResourceResponse(); }