评估Javascript是如何工作的? [英] How does evaluateJavascript work?

查看:21
本文介绍了评估Javascript是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的 evaluateJavascript 方法,但我得到的只是一个空结果:

I'm trying to use the new evaluateJavascript method in Android 4.4, but all I ever get back is a null result:

webView1.evaluateJavascript("return "test";", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Log is written, but s is always null
    }
});

如何向该方法返回结果?

How do I return a result to this method?

更新:更多信息:

  1. 我设置了 INTERNET 权限
  2. 我有 setJavascriptEnabled(true);
  3. 尝试撇号字符串:return 'test';,
  4. 尝试 JS 对象:return { test: 'this' }
  5. console.log('test'); 执行正常.
  6. 按照以下要求将 targetSdkVersion 设置为 19:如果您的应用使用网络视图
  1. I have the INTERNET permission set
  2. I have setJavascriptEnabled(true);
  3. Tried apostrophe string: return 'test';,
  4. Tried JS object: return { test: 'this' }
  5. console.log('test'); is being executed fine.
  6. Set targetSdkVersion to 19 as per: If your app uses WebView

设备:Nexus 7 和 Nexus 5(库存)

Devices: Both Nexus 7 and Nexus 5 (Stock)

推荐答案

此示例中使用了一个evaluateJavascript 方法的示例:

There is an example of the evaluateJavascript method being used in this sample:

https://github.com/GoogleChrome/chromium-webview-samples/tree/master/jsinterface-example

本质上,如果您在 WebView 中执行的 javascript 返回一个值,它将在回调中传递.

Essentially if the javascript you execute in the WebView returns a value it'll be passed in the callback.

需要注意的主要事项是 OnReceiveValue 中返回的字符串是 JSON 值、JSON 对象或 JSON 数组,具体取决于您返回的内容.

The main thing to note is that the String returned in OnReceiveValue is either a JSON Value, JSON Object or JSON Array depending on what you return.

需要注意的是,如果您返回单个值,则需要在 JSON 读取器上使用 setLenient(true) 才能使其工作.

Things to note about this is if you return a single value, you need to use setLenient(true) on a JSON reader for it to work.

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat+ you should use the evaluateJavascript method
        mWebView.evaluateJavascript(javascript, new ValueCallback<String>() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onReceiveValue(String s) {
                JsonReader reader = new JsonReader(new StringReader(s));

                // Must set lenient to parse single values
                reader.setLenient(true);

                try {
                    if(reader.peek() != JsonToken.NULL) {
                        if(reader.peek() == JsonToken.STRING) {
                            String msg = reader.nextString();
                            if(msg != null) {
                                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                } catch (IOException e) {
                    Log.e("TAG", "MainActivity: IOException", e);
                } finally {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // NOOP
                    }
                }
            }
        });
    }

您可能仍想对字符串响应使用解析器的原因是它被转换为 JSON 值,这意味着它将用引号括起来.

The reason you may still want to use a parser for a string response is it is converted to a JSON value which means it will be wrapped in quotes.

例如如果你去了:

mWebView.evaluateJavascript("(function() { return 'this'; })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Prints: "this"
    }
});

它将打印字符串 this,用双引号括起来:"this".

It would print the string this, wrapped in double quotes: "this".

其他值得注意的例子:

mWebView.evaluateJavascript("(function() { return null; })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Prints the string 'null' NOT Java null
    }
});

mWebView.evaluateJavascript("(function() { })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); //s is Java null
    }
});

mWebView.evaluateJavascript("(function() { return ''; })();", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
        Log.d("LogName", s); // Prints "" (Two double quotes)
    }
});

这篇关于评估Javascript是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆