在webview android中注入javascript

时间:2017-02-03 06:22:04

标签: javascript android webview

我的js脚本在控制台浏览器中运行良好,但在android中它表示

  

未捕获的TypeError:无法读取属性' removeChild'未定义的来源

下面的

是我的代码段,我添加了两个脚本来查看差异:

webView.getSettings().setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {
   @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl(getScript2());
        }
    });

//this script is working well
private String getScript1(){
    return "javascript:(function() { " +
            "document.getElementsByTagName('frameset')[0].rows='0,*'; " +
            "document.getElementsByTagName('frameset')[1].cols='0,*'; " +
            "})();";
}

//this script has an error
private String getScript2(){
    return "javascript:(function() { " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].removeChild(document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[0]); " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].removeChild(document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[0]); " +
            "document.getElementsByTagName('frame')[2].contentDocument.documentElement.getElementsByTagName('tr')[1].getElementsByTagName('td')[1].rowSpan='0'; " +
            "})();";
}
webView.loadUrl("https://www.bancnetonline.com/apps/jsp/bancnet/allIPMainPage.jsp?bnkname=4");

2 个答案:

答案 0 :(得分:0)

您收到错误是因为Uncaught TypeError:无法读取属性' removeChild'为null,请再次检查您的脚本是否为null值。

如果您愿意,可以创建一个通过javascript调用的类

import org.apache.spark.sql.types._ val 
schema = StructType(
    StructField("count",LongType,true)::
    StructField("flowrate",LongType,true)::
    StructField("fluidlevel",StringType,true)::
    StructField("frequency",LongType,true)::
    StructField("hardness",LongType,true)::
    StructField("speed",LongType,true)::
    StructField("temperature",LongType,true)::
    StructField("ts",LongType,true)::
    StructField("voltage",LongType,true):: Nil)

val df = spark.readStream 
    .schema(schema)
    .format("org.apache.bahir.sql.streaming.mqtt.MQTTStreamSourceProvider")
    .option("username","<username>")
    .option("password","<password>")
    .option("clientId","a:vy0z2s:a-vy0z2s-zfzzckrnqf")
    .option("topic", "iot-2/type/WashingMachine/id/Washer02/evt/voltage/fmt/json")
    .load("tcp://vy0z2s.messaging.internetofthings.ibmcloud.com:1883")

注册javascript界面​​

class MyJavaScriptInterface
{
    @JavascriptInterface
    public void processHTML(String html)
    {
        //called by javascript
    }
}

示例注入javascript到页面,尝试这样

webview1.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");

答案 1 :(得分:0)

将文件保存为资产文件夹中的index.html(Src-&gt; main-&gt; assets)

byte[] buffer = new byte[0];
            try {
                InputStream is = getActivity().getAssets().open("index.html");
                int size = is.available();

                buffer = new byte[size];
                is.read(buffer);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String str = new String(buffer);

然后打电话给

ll_webview.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "utf-8", null);