在webview android上显示网页的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12257929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display a part of the webpage on the webview android
提问by Anupam
I want to make an app which loads the content from the webpage into webview. I want to show only a particular thing in the entire webview, not the whole content of the webpage.
我想制作一个将网页内容加载到 webview 的应用程序。我只想在整个 webview 中显示特定的东西,而不是网页的全部内容。
Here is an example. If I use: http://us.m.yahoo.com/w/search%3B_ylt=A2KL8xs0vUBQMg0AwAkp89w4?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&p=digital+cameras&x=0&y=0as the URL for the webview, it loads all the contents of the page on the webview. But I want to remove the banner of the page and show it on the webview of my application.
这是一个例子。如果我使用:http: //us.m.yahoo.com/w/search%3B_ylt=A2KL8xs0vUBQMg0AwAkp89w4?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&p=digital+cameras&x=0&y= 0作为 webview 的 URL,它将页面的所有内容加载到 webview 上。但我想删除页面的横幅并将其显示在我的应用程序的 webview 上。
I have tried using adblocker using CSS tags, but that is not helping me. Please give me some idea for overcoming this problem.
我曾尝试使用 CSS 标签使用 adblocker,但这对我没有帮助。请给我一些克服这个问题的想法。
Thanks.
谢谢。
回答by Anupam
Thank You for the answer Zyber. I have solved it using injection of JavaScript in the code for WebView in android.
感谢您的回答 Zyber。我已经通过在 android 中的 WebView 代码中注入 JavaScript 解决了这个问题。
final WebView webview = (WebView)findViewById(R.id.browser);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('header')[0].style.display="none"; " +
"})()");
}
});
webview.loadUrl("http://code.google.com/android");
This solved my purpose and it is easy to use to.
这解决了我的目的,而且很容易使用。
回答by Roga Men
I got the solution to add this:
我得到了添加这个的解决方案:
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:(function() { " +
"var head = document.getElementsByClassName('header')[0].style.display='none'; " +
"var head = document.getElementsByClassName('blog-sidebar')[0].style.display='none'; " +
"var head = document.getElementsByClassName('footer-container')[0].style.display='none'; " +
"})()");
}
});
view.loadUrl("your url");
Adding (var head =) looks like to hide my class in webview.
添加 (var head =) 看起来像是在 webview 中隐藏我的类。
I hope this will be helpful for someone.
我希望这对某人有帮助。
回答by Zyber
check Jsoupit provides an library which gives an easy way of extracting Html elements from a webpage
检查Jsoup它提供了一个库,它提供了一种从网页中提取 Html 元素的简单方法
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url.toURI());
HttpResponse resp = client.execute(get);
String content = EntityUtils.toString(resp.getEntity());
Document doc = Jsoup.parse(content);
Elements ele = doc.select("div.classname");
This example executes an Http GET and then extracts an Div element with the class "classname" which you can then load in your webview
此示例执行 Http GET,然后提取具有类“classname”的 Div 元素,然后您可以将其加载到您的 web 视图中