0

我正在使用 android webview 我的目标是在渲染之前解析 html

为此,我在 pagefinish 事件中将以下 javascript 添加到 webview..

public void onPageFinished(WebView view, String url)
{
    view.loadUrl("javascript:varhtmlString=document.getElementsByTagName('html')[0].innerHTML;"
                 +"document.getElementsByTagName('html')[0].innerHTML=window.HTMLOUT.parseHTML(htmlString);");              
}

但问题是在解析 html 之前出现闪回(原始 html)

然后我监控日志,发现javascript在pageFinish(异步)之后执行以使其同步我使用等待通知机制并确保javasript在页面完成之前运行

但在解析之前仍然出现相同的问题原始html

有没有办法在渲染之前更改html????

4

1 回答 1

1

你可以这样做:

String content = getContentForUrl(url);
String manipulateHTML  = manipulate(content); // your function to adjust the content.

webView.loadDataWithBaseURL(url, manipulateHTML, "text/html","UTF-8", null);

public String getContentForUrl(String url) {

    BufferedReader in = null;

    String content = "";

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);

        in = new BufferedReader(new InputStreamReader(response.getEntity()
                .getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");

        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }

        in.close();
        content = sb.toString();

        Log.d("MyApp", "url content for " + url + " " + content);

    } catch (Exception e) {

        Log.d("MyApp",
                "url content for " + url + " Exception :" + e.toString());

    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return content;
}
于 2013-02-04T07:54:35.070 回答