Skip to content

Commit 004006e

Browse files
committed
修复自定义注入命回调Bug
1 parent 69ceeee commit 004006e

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Safe Java-JS WebView Bridge
1515
<dependency>
1616
<groupId>cn.pedant.safewebviewbridge</groupId>
1717
<artifactId>library</artifactId>
18-
<version>1.2</version>
18+
<version>1.3</version>
1919
<type>aar</type>
2020
</dependency>
2121

@@ -26,7 +26,7 @@ Safe Java-JS WebView Bridge
2626
}
2727

2828
dependencies {
29-
compile 'cn.pedant.safewebviewbridge:library:1.2'
29+
compile 'cn.pedant.safewebviewbridge:library:1.3'
3030
}
3131

3232
## Sample

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
VERSION_NAME=1.2
2-
VERSION_CODE=3
1+
VERSION_NAME=1.3
2+
VERSION_CODE=4
33
GROUP=cn.pedant.safewebviewbridge
44

55
POM_DESCRIPTION=Provide a safe and reliable scheme for Java and JavaScript in Android WebView.

library/src/main/java/cn/pedant/SafeWebViewBridge/JsCallJava.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ public class JsCallJava {
1717
private final static String TAG = "JsCallJava";
1818
private final static String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
1919
private HashMap<String, Method> mMethodsMap;
20+
private String mInjectedName;
2021
private String mPreloadInterfaceJS;
2122
private Gson mGson;
2223

2324
public JsCallJava (String injectedName, Class injectedCls) {
2425
try {
26+
if (TextUtils.isEmpty(injectedName)) {
27+
throw new Exception("injected name can not be null");
28+
}
29+
mInjectedName = injectedName;
2530
mMethodsMap = new HashMap<String, Method>();
2631
//获取自身声明的所有方法(包括public private protected), getMethods会获得所有继承与非继承的方法
2732
Method[] methods = injectedCls.getDeclaredMethods();
28-
StringBuilder sb = new StringBuilder("javascript:(function(b){console.log(\"HostApp initialization begin\");var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};");
29-
33+
StringBuilder sb = new StringBuilder("javascript:(function(b){console.log(\"");
34+
sb.append(mInjectedName);
35+
sb.append(" initialization begin\");var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};");
3036
for (Method method : methods) {
3137
String sign;
3238
if (method.getModifiers() != (Modifier.PUBLIC | Modifier.STATIC) || (sign = genJavaMethodSign(method)) == null) {
@@ -36,9 +42,15 @@ public JsCallJava (String injectedName, Class injectedCls) {
3642
sb.append(String.format("a.%s=", method.getName()));
3743
}
3844

39-
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.");
40-
sb.append(injectedName);
41-
sb.append("=a;console.log(\"HostApp initialization end\")})(window);");
45+
sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"");
46+
sb.append(mInjectedName);
47+
sb.append(" 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\"");
48+
sb.append(mInjectedName);
49+
sb.append(" 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.");
50+
sb.append(mInjectedName);
51+
sb.append("=a;console.log(\"");
52+
sb.append(mInjectedName);
53+
sb.append(" initialization end\")})(window);");
4254
mPreloadInterfaceJS = sb.toString();
4355
} catch(Exception e){
4456
Log.e(TAG, "init js error:" + e.getMessage());
@@ -110,7 +122,7 @@ public String call(WebView webView, String jsonStr) {
110122
values[k + 1] = argsVals.isNull(k) ? null : argsVals.getJSONObject(k);
111123
} else if ("function".equals(currType)) {
112124
sign += "_F";
113-
values[k + 1] = new JsCallback(webView, argsVals.getInt(k));
125+
values[k + 1] = new JsCallback(webView, mInjectedName, argsVals.getInt(k));
114126
} else {
115127
sign += "_P";
116128
}
@@ -176,7 +188,7 @@ private String getReturn (String reqJson, int stateCode, Object result) {
176188
insertRes = String.valueOf(result);
177189
}
178190
String resStr = String.format(RETURN_RESULT_FORMAT, stateCode, insertRes);
179-
Log.d(TAG, "HostApp call json: " + reqJson + " result:" + resStr);
191+
Log.d(TAG, mInjectedName + " call json: " + reqJson + " result:" + resStr);
180192
return resStr;
181193
}
182194
}

library/src/main/java/cn/pedant/SafeWebViewBridge/JsCallback.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
import android.util.Log;
1313

1414
public class JsCallback {
15-
private static final String CALLBACK_JS_FORMAT = "javascript:HostApp.callback(%d, %d %s);";
16-
private int index;
17-
private WebView webView;
18-
private int isPermanent;
15+
private static final String CALLBACK_JS_FORMAT = "javascript:%s.callback(%d, %d %s);";
16+
private int mIndex;
17+
private WebView mWebView;
18+
private int mIsPermanent;
19+
private String mInjectedName;
1920

20-
public JsCallback (WebView view, int index) {
21-
this.index = index;
22-
this.webView = view;
21+
public JsCallback (WebView view, String injectedName, int index) {
22+
mWebView = view;
23+
mInjectedName = injectedName;
24+
mIndex = index;
2325
}
2426

2527
public void apply (Object... args) {
@@ -35,12 +37,12 @@ public void apply (Object... args) {
3537
sb.append("\"");
3638
}
3739
}
38-
String execJs = String.format(CALLBACK_JS_FORMAT, index, isPermanent, sb.toString());
40+
String execJs = String.format(CALLBACK_JS_FORMAT, mInjectedName, mIndex, mIsPermanent, sb.toString());
3941
Log.d("JsCallBack", execJs);
40-
webView.loadUrl(execJs);
42+
mWebView.loadUrl(execJs);
4143
}
4244

4345
public void setPermanent (boolean value) {
44-
isPermanent = value ? 1 : 0;
46+
mIsPermanent = value ? 1 : 0;
4547
}
4648
}

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ android {
2828
}
2929

3030
dependencies {
31-
//compile 'cn.pedant.safewebviewbridge:library:1.2'
31+
//compile 'cn.pedant.safewebviewbridge:library:1.3'
3232
compile project(':library')
3333
}

0 commit comments

Comments
 (0)