问题描述
我有一个网络视图
WebView wv;
wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://example.com/");
简单地说.
在:
onPageFinished
我有:
wv.loadUrl("javascript:(function() { " + "document.getElementsByClassName('centered leaderboard_container')[0].style.display = 'none'; " + "document.getElementsByClassName('n')[0].style.display = 'none'; " + "document.getElementsByClassName('paginator')[0].style.display = 'none'; " + "document.getElementsByTagName('ul')[0].style.display = 'none'; " + "document.getElementsByTagName('tr')[0].style.display = 'none'; " + "})()");
我已将 webview 可见性设置为 INVISIBLE
I've set webview visibility to INVISIBLE
如何在 JavaScript 完成后将可见性设置为 VISIBLE?
How can I set visibility to VISIBLE after the JavaScript is done?
现在你可以看到整个页面,然后 JavaScript 就完成了..
Now you get to see the whole page for a second and than the JavaScript is done..
有人吗?
ps.该网站不是我的,它是第 3 方网站
ps. The website is not mine, its a 3rd party website
推荐答案
在 API 17 模拟器上测试过,可以正常工作.
Tested on API 17 emulator and it works.
您可以将 Java 中的 javascript 注入网络.
You can inject javascript from Java to the web.
反之亦然,一旦加载了 url,就从 javascript 调用我们的 Java 代码上的函数,然后执行 setVisibility()
.为此,您将添加一个 JS 接口.
And do vice-versa, once the url is loaded call from javascript to a function on our Java code, and execute the setVisibility()
. For that purpose you are going to add a JS interface.
代码如下:
private final static String HOST = "stackoverflow.com";
private WebView wb;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
wb = (WebView) findViewById(R.id.home_webview);
//Make the webview invisible
wb.setVisibility(View.INVISIBLE);
WebSettings webSettings = wb.getSettings();
webSettings.setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
//Inject javascript code to the url given
//Not display the element
wb.loadUrl("javascript:(function(){"+"document.getElementById('Id').style.display ='none';"+"})()");
//Call to a function defined on my myJavaScriptInterface
wb.loadUrl("javascript: window.CallToAnAndroidFunction.setVisible()");
}
});
//Add a JavaScriptInterface, so I can make calls from the web to Java methods
wb.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
wb.loadUrl("http://"+HOST);
}
public class myJavaScriptInterface {
@JavascriptInterface
public void setVisible(){
runOnUiThread(new Runnable() {
@Override
public void run() {
wb.setVisibility(View.VISIBLE);
}
});
}
}
此功能将针对每个页面执行.进入第 3 方服务器后,您必须管理如何处理每个请求,webClient.shouldOverrideUrlLoading()
可以帮助您.
This functionality is going to be executed for every page. Once on the 3rd party server you have to manage what to do with every request, webClient.shouldOverrideUrlLoading()
can help you.
更新答案:
我可以按照你的评论复制它,对于我们应该做的最后一个版本:
I could reproduce it as you commented, for the last version we should do:
从 Android 4.2 开始,您现在必须使用 @JavascriptInterface 显式注释公共方法,以便从托管的 JavaScript 访问它们.请注意,这也只有在您将应用的 minSdkVersion 或 targetSdkVersion 设置为 17 或更高时才会生效.
我添加并导入了android.webkit.JavascriptInterface
参考:WebViews 中的 JavascriptInterface 方法现在必须被注释
这篇关于隐藏 WebView 直到 JavaScript 完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!