我正在使用 WebView 实现一个应用程序。对于 webview 中加载的 url,我需要对 url 中加载的 html 代码执行替换。
我怎样才能有效地做到这一点?
说明:我需要从源码中替换一个特定的脚本脚本:
例如:我想
<html>
<script> SCRIPT A</script>
<p>Hello World</p>
</html>
我想给用户显示这个其他的
<html>
<script> SCRIPT B</script>
<p>Hello World</p>
</html>
谢谢
最佳答案
您必须覆盖 WebViewClient
的 shouldInterceptRequest
。请参阅文档 here .
一般形式是这样的(未经测试):
webview.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
if (you_want_to_intercept) {
/*return custom WebresourceResponse here*/
} else {
/*call superclass*/
return super.shouldInterceptRequest(view, url);
}
}
}
我希望这对您有所帮助,如果没有请告诉我。
关于java - 如何对 Webview 中加载的网页进行替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41760184/