【问题标题】:Understanding Android webview javascript interface了解Android webview javascript界面
【发布时间】:2016-04-26 12:21:48
【问题描述】:

我创建了一个android WebView,并使用addJavascriptInterface(mObject, "jsinterface") 注入了javascript 接口。在我使用 new 运算符在 JavaScript 中创建具有相同名称(jsinterface)的对象之前,它工作正常。

我的 Java 代码:

WebView mWebView = findViewById(R.id.myWebView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new MyWebChromeClient((Activity)mContext));
mWebView.addJavascriptInterface(new testClass(), "jsinterface");
mWebView.loadUrl("UrlToLoad");

testClass.java

public class testClass{
    public testClass() {
    }

    @JavascriptInterface
    public String testNativeMethod() {
        return "Java method called!!";
    }
}

我的 Java 脚本代码

test.js

function test(msg){
    this.message = msg;

    this.testJSMethod = function(){
        return this.message;
    }
}

alert(jsinterface.testNativeMethod()); // prints Java method called!!
jsinterface= new test("JS method called...");
alert(jsinterface.testJSMethod()); // prints JS method called...
alert(jsinterface.testNativeMethod()); // errors "NPMethod called on non- NPObject"

问题:

javascript 对象是否可以同时访问两者,即javascript 方法和本机JAVA 方法(通过javascriptinterface 暴露给它)?是否有可能将任何属性设置为 webview 或执行任何 JS script 来完成这项工作?

【问题讨论】:

    标签: javascript java android webview


    【解决方案1】:

    想想 javascript 中的document。当您在 Web 浏览器中时,这是一个您可以随时访问的全局对象。如果您将自己的new var 称为document,那么您将无法访问全局document

    当你执行这一行时:

        mWebView.addJavascriptInterface(new testClass(), "jsinterface");
    

    您正在添加一个名为jsinterface 的全局对象。这与document 的情况相同。如果创建同名的 var,它将覆盖现有的全局引用。

    将 javascript 接口添加到 WebView 后,您无需创建对该接口的 new 引用。 addJavascriptInterface 已经为您完成了。

    【讨论】:

      【解决方案2】:

      试试

      您可以尝试创建另一个对象,该对象将重新转换对javascript接口的调用。在WebViewClient中实现onPageStarted方法,并在onPageStarted方法中注入javascript,方法如下。

       mWebView.setWebViewClient(new WebViewClient() {
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  view.loadUrl(url);
                  return true;
              }
              @Override
              public void onPageStarted (WebView view, String url, Bitmap favicon){
                  String jsScript= "javascript:var functions_array = ['testNativeMethod'];";
                         jsScript+="var jsinterface = {};"
                         jsScript+="functions_array.map(function(id){"
                         jsScript+="jsinterface[id]= function() {"
                         jsScript+="try{return temp_obj[id].apply(temp_obj, arguments);}"
                         jsScript+="catch(e) { console.log('ERROR: ' + e + ', method ' + id);"
                         jsScript+="return false;}}})"
                  view.loadUrl(jsScript);
              }
          });
      

      希望这会有所帮助:-)

      【讨论】:

        猜你喜欢
        • 2015-02-18
        • 2011-07-20
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 2014-04-08
        • 2013-01-07
        • 2013-02-25
        相关资源
        最近更新 更多