Android,Javascript - InnerHttp如何执行多行

时间:2017-04-29 09:46:14

标签: javascript android

有没有办法给 window.HTMLOUT.processHTML()提供多行代码  功能

  webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webview.loadUrl(
                    "javascript:window.HTMLOUT.processHTML(document.getElementsByTagName('html')[0].innerHTML = document.getElementsByTagName('a')[26].getAttributeNode('href').value);"
            );
        }
    });

我想添加一个从页面中选择一些数据的循环。

1 个答案:

答案 0 :(得分:0)

最后我自己找到了答案。我创建了一个javascript lamda函数,它返回所需的字符串..

public class MainActivity extends AppCompatActivity {
LinearLayout l;
WebView w;
TextView t;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    l = (LinearLayout) findViewById(R.id.l);
    t = (TextView) findViewById(R.id.xx);
    w = (WebView) findViewById(R.id.w);
    w.getSettings().setJavaScriptEnabled(true);
    w.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    w.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {

            w.loadUrl(
                    "javascript:window.HTMLOUT.processHTML(" +
                            "(function() {" +
                            "var val =  document.getElementsByTagName(\"a\")[20].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[23].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[26].getAttributeNode(\"href\").value ;" +
                            " val +=  document.getElementsByTagName(\"a\")[29].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[32].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[35].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[38].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[41].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[44].getAttributeNode(\"href\").value;" +
                            " val +=  document.getElementsByTagName(\"a\")[47].getAttributeNode(\"href\").value;" +
                            "return val;" +
                            "})()" +
                            ");"
            );
        }
    });
    w.loadUrl("https://website.com/search?q=card");
}

private class MyJavaScriptInterface {
    @JavascriptInterface
    @SuppressWarnings("unused")
    public void processHTML(final String html) {
        t.post(new Runnable() {
            @Override
            public void run() {
                t.setText(html);
            }
        });
        // process the html as needed by the app
    }
  }
}