1.
//注入的js
private static final String insertJavaScript = "javascript:$(\".news-banner-container-new-article,.recommendation-container-new-article-test,.download-text,.info\").remove();console.log(window.obj.loadSucces());";
//这个方法是让wevview调用原生的方法
@JavascriptInterface
public void loadSucces() {
Toast.makeText(this, "This method is called!", Toast.LENGTH_SHORT).show();
}
viewById.addJavascriptInterface(this, "obj");
//如果要想正常弹窗必须这个方法
viewById.setWebChromeClient(new WebChromeClient());
viewById.loadUrl("https://m.toutiao.com/i6507046999341138446/");
viewById.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
//注入
view.loadUrl(insertJavaScript);
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
2 注意内容
- 如果说我们只是修改网页的内容需要以下面格式书写
console.log(window.obj.loadSucces());
log方法中是你真正执行的js方法,该网站引入的所有第三方庫都能调用