Skip to content

Commit

Permalink
支持注入名自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Sep 15, 2014
1 parent 8f2d0b1 commit 40287a1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ Safe Java-JS WebView Bridge
抛弃使用高风险的WebView addjavascriptInterface方法,利用onJsPrompt解析纯JSON字符串,来实现网页JS层反射调用Java方法,同时通过对js层调用函数及回调函数的包装,支持方法参数传入所有已知的类型,包括number、string、boolean、object、function。

## 如何开始
初始化Webview WebSettings时允许js脚本执行,同时使用你的注入类实例化一个**InjectedChromeClient**对象,然后关联到你的Webview实例。如demo中的例子(指定的注入类为HostJsScope):
初始化Webview WebSettings时允许js脚本执行,同时使用你的注入名和注入类来实例化一个**InjectedChromeClient**对象,然后关联到你的Webview实例。如demo中的例子(页面中引用的对象名为HostApp,指定的注入类为HostJsScope):

WebView wv = new WebView(this);
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.setWebChromeClient(new InjectedChromeClient(HostJsScope.class));
wv.setWebChromeClient(
new InjectedChromeClient("HostApp", HostJsScope.class)
);
wv.loadUrl("file:///android_asset/test.html");
## 方法的定义
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class InjectedChromeClient extends WebChromeClient {
private JsCallJava mJsCallJava;
private boolean mIsInjectedJS;

public InjectedChromeClient (Class injectedCls) {
mJsCallJava = new JsCallJava(injectedCls);
public InjectedChromeClient (String injectedName, Class injectedCls) {
mJsCallJava = new JsCallJava(injectedName, injectedCls);
}

// 处理Alert事件
Expand Down
4 changes: 2 additions & 2 deletions src/cn/pedant/SafeWebViewBridge/bridge/JsCallJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class JsCallJava {
private final String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
private String mPreloadInterfaceJS;

public JsCallJava (Class injectedCls) {
public JsCallJava (String injectedName, Class injectedCls) {
try {
mMethodsMap = new HashMap<String, Method>();
//获取自身声明的所有方法(包括public private protected), getMethods会获得所有继承与非继承的方法
Expand All @@ -33,7 +33,7 @@ public JsCallJava (Class injectedCls) {
sb.append(String.format("a.%s=", method.getName()));
}

sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"HostApp call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"HostApp call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b.HostApp=a;console.log(\"HostApp initialization end\")})(window);");
sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"HostApp call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"HostApp call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b." + injectedName + "=a;console.log(\"HostApp initialization end\")})(window);");
mPreloadInterfaceJS = sb.toString();
} catch(Exception e){
Log.e(TAG, "init js error:" + e.getMessage());
Expand Down
4 changes: 3 additions & 1 deletion src/cn/pedant/SafeWebViewBridge/demo/WebActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(wv);
WebSettings ws = wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.setWebChromeClient(new InjectedChromeClient(HostJsScope.class));
wv.setWebChromeClient(
new InjectedChromeClient("HostApp", HostJsScope.class)
);
wv.loadUrl("file:///android_asset/test.html");
}
}

0 comments on commit 40287a1

Please sign in to comment.