听从Java javascript函数调用 - 机器人 [英] Listen to javascript function invocation from java - Android

查看:133
本文介绍了听从Java javascript函数调用 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管服务器上的某些HTML5 / JavaScript文件。当点击HTML5网页上的按钮,一个JavaScript函数被调用。我要听被调用函数时,并获得该函数返回的JSON。 previously我是在设备上打开一个端口,但似乎并没有工作在Android 3.0。我听说,你可以使用外部接口听JavaScript的电话,但我不知道如何实现这一点。

I have some HTML5/javascript files hosted on a server. When a button is clicked on the HTML5 page a javascript function is invoked. I want to listen for when a function is invoked and get the json that the function returns. Previously I was opening a port on the device but that doesn't seem to work on Android 3.0. I have heard that you can use external interface to listen to javascript calls but I don't know how to implement that.

推荐答案

要使用你需要运行的WebView你的网站的外部接口。还下述溶液所暗示的,什么

To use external interfaces you need to run your site in WebView. Also the following solution implies, what

  1. 您知道所有的函数名,要拦截,并...
  2. 在这些功能都是在JS code中的公众范围。

要注册界面,您需要调用<一href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29"相对=nofollow>在的WebView 实例addJavascriptInterface 方法。你需要选择一个名字,并创建一个实现。你的接口应该截获函数调用和报告他们的结果,所以,让我们来形容了......

To register your interface you need to call the addJavascriptInterface method on your WebView instance. You need to select a name for it and create an implementation. Your interface should intercept function calls and report their results, so, let's describe it...

class FunctionCallInterceptor {
    public void reportCall(String functionName, String result) {
      // some code, handling interception
    }
}

和注册它...

mWebView.addJavascriptInterface(new FunctionCallInterceptor(), 'Interceptor');

有关的JavaScript接口的更多信息,请参阅绑定的JavaScript

For more information on JavaScript interface see Binding JavaScript

然后,你需要转移功能,结果你的界面......在这里,你需要一些JavaScript code。

Then you need to "transfer" function results to your interface... Here you need some JavaScript code.

function wrapFunc(name) {
  if(typeof window[name] == 'function') {  // If target is accessible
    var original = window['__' + name] = window[name];  // remember original one
    window[name] = function() {  // and replace with wrapper
        var result = original.apply(this, arguments); // call original                
        Interceptor.reportCall(name, result.toString()); // report to interceptor
        return result;  // return result as usual
    }
  }
}

要包装你的函数中使用这个code

To wrap your function use this code

wrapFunc('myFunction'); // wraps myFunction in the source

另外,不要忘了启用JavaScript在所有

Also, don't forget to enable JavaScript at all

mWebView.getSettings().setJavaScriptEnabled(true);

当,如何嵌入这个code到外部网页(我暗示你是不是有什么访问外部JS code)...在网页环境下,你可以执行任意code使用<一个href="http://developer.android.com/reference/android/webkit/WebView.html#loadUrl%28java.lang.String%29"相对=nofollow> 的WebView的使用loadURL 方法

When, how to embed this code to your external page (I imply what you are not having access to external JS code)... To execute arbitrary code in the page context you can use loadUrl method of WebView

mWebView.loadUrl('javascript:some... js... code...');

这将不会触发页面重载,只是JavaScript的将被执行。请注意你需要执行完全加载页面此之后。

This will not trigger page reload, just JavaScript will be executed. Note what you need to execute this after page is fully loaded. You can obtain this by code below:

mWebView.setWebViewClient(new WebViewClient {
    @Override
    public void onPageFinished (WebView view, String url) {
        // here page is loaded
    }
});

请参阅<一href="http://developer.android.com/reference/android/webkit/WebView.html#setWebViewClient%28android.webkit.WebViewClient%29"相对=nofollow> setWebViewClient 和<一href="http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished%28android.webkit.WebView,%20java.lang.String%29"相对=nofollow> onPageFinished 了解详细信息

另外请注意,什么code,通过使用loadURL 电话转到页,一定不能包含换行符。因此,你需要(通过与string.replace左右),以摆脱他们。

Also note, what code, transferred to page via loadUrl call, must not contain line breaks. So you need to get rid of them (by String.replace or so).

添加 所以,最终的解决方案是:

ADD So, the final solution is:

String wrapFuncCode = "function wrapFunc ...... "; // or maybe place it in resources?
mWebView.addJavascriptInterface(new FunctionCallInterceptor(), 'Interceptor');
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient {
    @Override
    public void onPageFinished (WebView view, String url) {
       // inject wrapper
       // don't forget to remove newline chars
       mWebView.loadUrl("javascript:" + wrapFuncCode.replace('\n', ''));

       // wrap all the functions needed
       String[] funcToWrap = new String[] { 'myFunc1', ... };
       for(String f : funcToWrap) {
           mWebView.loadUrl("javascript:wrapFunc('" + f + "myFunction');");  
       }  
    }
});

这篇关于听从Java javascript函数调用 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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