我正在开发一个应用程序,在该应用程序的 Web View 中加载了一个 URL。通过 JavaScript,我切出网页的页眉和页脚并在 webview 中显示内容。我的问题是,需要花费一些时间来切出网页的页眉和页脚,大约 1-2 秒后页眉可见并立即隐藏。我正在附加我的 WebViewClient 类:
private class CustomWebClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
doGet(url);
return true; // super.shouldOverrideUrlLoading(view, url);
}
public void onLoadResource(WebView view, String url) {
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('header')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('footer')[0].style.display = 'none'; "
+ "})()");
webview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('section').search_again.style.display = 'none'; "
+ "})()");
}
}
在 onLoadResource()
方法上我正在注入(inject) JS。
任何人都知道我面临的问题,请帮助我克服这个问题。
最佳答案
您可以使用带有 android 媒体查询的 css 文件通过 css 隐藏页眉/页脚
关于javascript - 安卓 : Inject JavaScript before loading the URL in webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12576236/