Android WebView:删除元素后页面出现乱码

Android WebView: Page garbled after element removal

我正在为网站开发 Android WebView。 我在loadUrl方法中输入10-12行元素删除后,页面看起来是乱码。是否有特定的元素删除或修改顺序?

这是我的代码:

MyWebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {

    webView.loadUrl("javascript: var ele=document.getElementById(\"nav\");\n" +
        "ele.parentNode.removeChild(ele); ele=document.getElementById(\"footer\");\n" +
        "ele.parentNode.removeChild(ele); ele=document.getElementById(\"header\");\n" +
        "ele.parentNode.removeChild(ele); ele=document.getElementById(\"main\");\n" +
        "ele.style.padding=\"0px 0px 0px 0px\"; ele=document.getElementById(\"main\");\n" +
        "document.body.style.margin=\"0px 0px 0px 0px\"; ele=document.getElementById(\"main\");\n" +
        "ele.style.margin=\"0px 0px 0px 0px\";  ele.style.padding=\"10px 15px 15px 15px\"; document.body.style.minWidth=\"350px\"; document.body.style.maxWidth=\"650px\"; " +
        "elements = document.getElementsByClassName(\"status-messages\")\n" +
        "        for (var i = 0; i < elements.length; i++) {\n" +
        "            elements[i].style.margin=\"0px 0px 0px 0px\";\n" +
        "            elements[i].style.padding=\"0px 0px 0px 0px\";\n" +
        "            elements[i].style.maxHeight=\"700px\";\n" +
        "            elements[i].style.minHeight=\"0px\";\n" +
        "            elements[i].style.maxWidth=\"650px\";\n" +
        "            elements[i].style.minWidth=\"0px\";\n" +
        "        } ele=document.getElementsByTagName(\"body\")[0];\n" +
        "ele.style.maxWidth=\"550px\";\n" +
        "ele.style.minWidth=\"0px\"; ele=document.getElementsByTagName(\"img\")[0];\n" +
        "ele.style.maxWidth=\"450px\";\n" +
        "ele.style.minWidth=\"0px\"; ele=document.getElementById(\"container\");\n" +
        "ele.style.maxWidth=\"550px\";\n" +
        "ele.style.minWidth=\"0px\"; ele=document.getElementById(\"enviroTable\");\n" +
        "ele.style.maxWidth=\"450px\"; elements = document.getElementsByClassName(\"right\");\n" +
        "    while(elements.length > 0)\n" +
        "        elements[0].parentNode.removeChild(elements[0]); ");
} });

我在 onPageFinished 方法中编写了这段代码。在这个地方做这个合适吗?

document.readyState 为我工作。

我是这样使用的:

 if(document.readyState === "complete") {
 /* my code */
 }

现在看起来与 Chrome 控制台上的完全一样。