2013-03-21 29 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); 
相關問題