【问题标题】:Android WebView JavaScriptInterfaceAndroid WebView JavaScript接口
【发布时间】:2014-01-27 10:18:33
【问题描述】:

我将 JavaScriptInterface 用于 Web 视图并尝试检索带有标签“a”的元素数量。但是当我尝试使用谷歌页面时,我没有得到任何结果。各位帮帮我。

我用过的以下sn-p。

    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new WebViewJSObject(), "jsInjectedObject");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            if(url.equalsIgnoreCase("http://www.google.com")){
            view.loadUrl("javascript:(function(){var b = document.getElementsByTagName('a');jsInjectedObject.printInfo(b.length);})()");
            }
        }
    });
    webView.loadUrl("http://www.google.com");


    class WebViewJSObject{

        @JavascriptInterface
        public void printInfo(String count){
            Log.e(TAG, "entered PrintInfo");
            Log.e(TAG, "A tags count :" + count);
        }
    }

【问题讨论】:

    标签: android webview javascript-injection


    【解决方案1】:
        data = data.replace("%", "%25"); //Data here is the javascript or html data
        data = data.replace("\\", "%27");
        data = data.replace("?", "%3f");
        WebSettings browserSettings = webView.getSettings();
        browserSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        browserSettings.setJavaScriptEnabled(true);
        browserSettings.setSupportMultipleWindows(true);
        browserSettings.setBuiltInZoomControls(true);
        browserSettings.setSupportZoom(true); 
    

    我希望这会有所帮助。另请参考https://stackoverflow.com/a/19341180/2106338

    【讨论】:

      【解决方案2】:

      这可能会帮助你.....在创建做dis

      super.onCreate(savedInstanceState);
          super.init();
      
               JavaScriptInterface myJavaScriptInterface  = new  JavaScriptInterface(this,appView);         
      
              appView.addJavascriptInterface(myJavaScriptInterface, "keyword");
      
              super.loadUrl("file:///....../index.html", 1000);
      

      并在创建后创建一个方法

      public class JavaScriptInterface {
                  private WebView mAppView;
      
                   private DroidGap mGap;
      
                      public JavaScriptInterface (DroidGap gap, WebView view)
                      {
                          mAppView = view;
                          mGap = gap;
                      }
       public void methodname(){
      }
      }
      

      并通过使用上述关键字在html中调用来完成methodname方法中的功能

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多