简体   繁体   English

在Android WebView中删除页眉和页脚

[英]removing headers and footers in android webview

I am trying to load a responsive webpage in a webview component. 我正在尝试在Webview组件中加载响应式网页。 But I would like to strip the header and footer of the webpage and load the webview with just the body of the webpage. 但是我想剥离网页的页眉和页脚,并仅用网页正文加载webview。 How can this be achieved in an android webview. 如何在android webview中实现这一点。

<div class="header-container">
<header class="header clearfix" ng-include="'/modules/core/homepage/header-partial.html'"></header>
</div>

I was able to remove the header and footer and load the webpage using loadDataWithBaseURL() method 我能够删除页眉和页脚并使用loadDataWithBaseURL()方法加载网页

Document document = Jsoup.connect(mUrl).get();
document.getElementsByClass("header-container").remove();
document.getElementsByClass("footer").remove();
WebSettings ws = mWebView.getSettings();
ws.setJavaScriptEnabled(true);
//mWebView.loadData(document.toString(),"text/html","utf-8");
mWebView.loadDataWithBaseURL(mUrl,document.toString(),"text/html","utf-8","");

As per the developer docs : 根据开发人员文档:

Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. 请注意,JavaScript的相同来源策略意味着在使用此方法加载的页面中运行的脚本将无法访问使用“数据”以外的任何方案(包括“ http”)加载的内容。 To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL. 为避免此限制,请使用loadDataWithBaseURL()和适当的基本URL。

http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String , java.lang.String, java.lang.String) http://developer.android.com/reference/android/webkit/WebView.html#loadData(java.lang.String,java.lang.String,java.lang.String

If you want to edit html, i'd recommend using a html parser like jsoup. 如果您想编辑html,我建议您使用诸如jsoup之类的html解析器。 Them remove header and footer, and lastly load the data into a WebView 他们删除页眉和页脚,最后将数据加载到WebView

try {

    // Load the html into jsoup
    Document doc = Jsoup.connect("http://your-site.com/").get();

    // find and remove header
    Element header = doc.getElementById("your-header");
    header.remove();

    // find and remove footer
    Element footer = doc.getElementById("your-footer");
    footer.remove();

    // Load data into a WebView
    WebView wv = (WebView) findViewById(R.id.webView);
    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    wv.loadData(doc.toString(), "text/html", "utf-8");

} catch (IOException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM