2013-03-21 82 views
0

我想在WebView中显示网页之前,根据它们的id从网页的html代码中删除一些元素。 我知道如何用Javascript做到这一点,但对于我的应用来说,禁用Javascript的WebView非常重要。在没有Javascript的情况下删除android webview中的html元素

我写了关于像jsoup这样的html pasers,但我无法完全知道如何将它们用于我的特定问题。有什么建议么?

编辑: OK,这里是我到目前为止有:

我的HTML加载到一个字符串,并删除不需要的元素。

String HTMLResult=""; 
     String urlText = "http://www.google.com"; 
    BufferedReader in = null; 
    try { 
     URL url = new URL(urlText); 
     in = new BufferedReader(new InputStreamReader(url.openStream())); 


     String inputLine; 
     while ((inputLine = in.readLine()) != null) { 
      HTMLResult += '\n'+ inputLine; 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) { 
     try { 
      in.close(); 
       HTMLResult=HTMLResult.replace("ExampleElement", ""); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
     } 

这部分工作正常,但速度很慢。

我试图将HTML的代码加载到的WebView与

webview.loadData(HTMLResult, "text/html", null); 

,但我只得到在网页视图窗口中显示在textform代码。

谢谢你, 帕斯卡尔

+0

却没有你的代码示例任何建议。更加详细一些! – 2013-03-21 14:28:45

回答

1

您应该从网页视图以外获得的HTML源文件,然后解析它作为原始数据,修改你想要什么,并给web视图您处理的HTML。

0

使用Jsoup库来实现这一点。

https://jsoup.org/apidocs/org/jsoup/select/Selector.html

Document doc = Jsoup.connect("your url").ignoreContentType(true).get(); 
Elements ele = doc.select("class to remove").remove(); 
webView.loadData(doc.toString(),"text/html; charset=UTF-8", null); 
相关问题