diff --git a/assetsJsBridge/WebViewJavascriptBridge.js b/assetsJsBridge/WebViewJavascriptBridge.js new file mode 100755 index 0000000..a92f183 --- /dev/null +++ b/assetsJsBridge/WebViewJavascriptBridge.js @@ -0,0 +1,132 @@ +//NOTES: the comments will be removed +(function(win,doc,PROTOCOL_SCHEME){ + if (win.WebViewJavascriptBridge) { + //in case of already init-ed. + return; + } + //o2s/s2o + function o2s(o){ + if(null==o)return "null"; + f=arguments.callee; + t=typeof o; + if('object'==t){if(Array==o.constructor)t='array';else if(RegExp==o.constructor)t='regexp';} + switch(t){ + case 'undefined':case 'unknown':return; + case 'function':return !('prototype' in o)?"function(){}":(""+o);break; + case 'boolean':case 'regexp':return o.toString(); break; + case 'number':return isFinite(o)?o.toString():'null';break; + case 'string':return '"'+o.replace(/(\\|\")/g,"\\$1").replace(/\n/g,"\\n").replace(/\r/g,"\\r")+'"';break; + case 'object':var r=[];try{for(var p in o){v=f(o[p]);if(v!==undefined)r.push('"'+p+'":'+v);}}catch(e){}; + return '{'+r.join(',')+'}';break; + case 'array':var r=[]; + if(o.length>=0){ + for(var i=0;i=0){ - for(var i=0;i responseCallbacks = new HashMap(); @@ -167,7 +168,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) { JsBridgeWebView webView = JsBridgeWebView.this; if (url.startsWith(JSB1_RETURN_DATA)) { - // java2js callback + // app2js callback webView.handlerReturnData(url); return true; } else if (url.startsWith(JSB1_OVERRIDE_SCHEMA)) { @@ -194,9 +195,11 @@ public void onPageFinished(WebView view, String url) { JsBridgeWebView webView = JsBridgeWebView.this; if (webView.getStartupJsb1Msg() != null) { + //if something is called before the page is loaded, do them now... for (Jsb1Msg m : webView.getStartupJsb1Msg()) { webView.dispatchMessage(m); } + //clear it webView.setStartupJsb1Msg(null); } } @@ -214,7 +217,7 @@ void handlerReturnData(String url) { } } - private void _java2js(String handlerName, String data, ICallBackFunction responseCallback) { + private void _app2js(String handlerName, String data, ICallBackFunction responseCallback) { Jsb1Msg m = new Jsb1Msg(); if (!TextUtils.isEmpty(data)) { m.setData(data); @@ -241,7 +244,7 @@ private void queueMessage(Jsb1Msg m) { void dispatchMessage(Jsb1Msg m) { String s = m.toJson(); - if ("".equals(s)) s = "null";//TMP HACK. + if ("".equals(s) || s == null) s = "null"; //NOTES: run the js in the main thread of browser: if (Thread.currentThread() == Looper.getMainLooper().getThread()) { @@ -290,20 +293,26 @@ public void onCallBack(String data) { queueMessage(responseMsg); } }; - } else { - responseFunction = new ICallBackFunction() { - @Override - public void onCallBack(String data) { - // do nothing - } - }; } + //else { + // responseFunction = new ICallBackFunction() { + // @Override + // public void onCallBack(String data) { + // // do nothing + // } + // }; + //} ICallBackHandler handler = null; if (!TextUtils.isEmpty(m.getHandlerName())) { handler = messageHandlers.get(m.getHandlerName()); - } - if (handler != null) { - handler.handler(m.getData(), responseFunction); + if (handler != null) { + //TODO 这里要有个 auth-mapping check!! + handler.handler(m.getData(), responseFunction); + } else { + //TODO debug下什么鬼 + } + } else { + //TODO debug下又什么鬼,连名字都没有,要干嘛? } } } @@ -323,9 +332,9 @@ public void registerHandler(String handlerName, ICallBackHandler handler) { } } - // from java call js + //from java call js public void callHandler(String handlerName, String data, ICallBackFunction cb) { - _java2js(handlerName, data, cb); + _app2js(handlerName, data, cb); } //prototol(java,js) diff --git a/lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/WebViewUi.java b/lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/SimpleWebViewUi.java similarity index 91% rename from lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/WebViewUi.java rename to lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/SimpleWebViewUi.java index b09be49..5563050 100644 --- a/lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/WebViewUi.java +++ b/lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/SimpleWebViewUi.java @@ -1,5 +1,7 @@ package szu.bdi.hybrid.core; +//SimpleWebViewUi is a HybridUi with a full size "JBridgeWebView" + import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; @@ -19,9 +21,9 @@ //@ref http://stackoverflow.com/questions/20138434/alternate-solution-for-setjavascriptenabledtrue @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"}) -public class WebViewUi extends HybridUi { +public class SimpleWebViewUi extends HybridUi { - final private static String LOGTAG = "WebViewUi"; + final private static String LOGTAG = "SimpleWebViewUi"; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override @@ -29,10 +31,9 @@ protected void onCreate(Bundle savedInstanceState) { Log.v(LOGTAG, ".onCreate()"); super.onCreate(savedInstanceState); - setTitle("TODO setTitle()"); + //setTitle("setTitle()"); final Context _ctx = this; - final Activity _activity = this; JsBridgeWebView _wv = new JsBridgeWebView(_ctx); @@ -66,7 +67,7 @@ public void onClick(DialogInterface dialog, int which) { } }); - String address = HybridTools.optString(getUiData("address")); + String address = HybridTools.optString(this.getUiData("address")); String url = ""; if (address == null || "".equals(address)) { url = "file://" + HybridTools.localWebRoot + "error.htm"; @@ -80,7 +81,6 @@ public void onClick(DialogInterface dialog, int which) { } } - //IMPORTANT: HybridTools.bindWebViewApi(_wv, this); setContentView(_wv); @@ -112,7 +112,9 @@ public void onBackPressed() { Intent rtIntent = new Intent(); rtIntent.putExtra("rt", o.toString()); - setResult(1, rtIntent);//@ref onActivityResult() + + //@ref onActivityResult() + setResult(1, rtIntent); finish(); } diff --git a/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridgeBase.m b/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridgeBase.m old mode 100644 new mode 100755 index e1b1e39..f21ec13 --- a/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridgeBase.m +++ b/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridgeBase.m @@ -170,18 +170,11 @@ - (void)_queueMessage:(WVJBMessage*)message { } - (void)_dispatchMessage:(WVJBMessage*)message { - NSString *messageJSON = [self _serializeMessage:message pretty:NO]; - [self _log:@"SEND" json:messageJSON]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"]; - messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"]; - - NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]; + NSString *json_string = [self _serializeMessage:message pretty:NO]; + + //TODO if "" or null change to "null"... + + NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._app2c(%@);", json_string]; if ([[NSThread currentThread] isMainThread]) { [self _evaluateJavascript:javascriptCommand]; @@ -212,4 +205,4 @@ - (void)_log:(NSString *)action json:(id)json { } } -@end \ No newline at end of file +@end diff --git a/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridge_JS.m b/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridge_JS.m old mode 100644 new mode 100755 index 3a494dd..123da45 --- a/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridge_JS.m +++ b/lib-ios/szu-bdi-hybrid-core/JavascriptBridge/WebViewJavascriptBridge_JS.m @@ -13,6 +13,7 @@ NSString * WebViewJavascriptBridge_js() { #define __wvjb_js_func__(x) #x + //TODO 不要这样写在oc里面,又不灵活又不好改,要从 assets 读进来.. // BEGIN preprocessorJSCode static NSString * preprocessorJSCode = @__wvjb_js_func__( ;(function() { @@ -23,7 +24,7 @@ registerHandler: registerHandler, callHandler: callHandler, _fetchQueue: _fetchQueue, - _handleMessageFromObjC: _handleMessageFromObjC + _app2js: _app2js }; var messagingIframe; @@ -45,10 +46,10 @@ function callHandler(handlerName, data, responseCallback) { responseCallback = data; data = null; } - _doSend({ handlerName:handlerName, data:data }, responseCallback); + _js2app({ handlerName:handlerName, data:data }, responseCallback); } - function _doSend(message, responseCallback) { + function _js2app(message, responseCallback) { if (responseCallback) { var callbackId = 'cb_'+(uniqueId++)+'_'+new Date().getTime(); responseCallbacks[callbackId] = responseCallback; @@ -64,9 +65,8 @@ function _fetchQueue() { return messageQueueString; } - function _dispatchMessageFromObjC(messageJSON) { + function _app2js(message) { setTimeout(function _timeoutDispatchMessageFromObjC() { - var message = JSON.parse(messageJSON); var messageHandler; var responseCallback; @@ -81,7 +81,7 @@ function _dispatchMessageFromObjC(messageJSON) { if (message.callbackId) { var callbackResponseId = message.callbackId; responseCallback = function(responseData) { - _doSend({ responseId:callbackResponseId, responseData:responseData }); + _js2app({ responseId:callbackResponseId, responseData:responseData }); }; } @@ -98,16 +98,12 @@ function _dispatchMessageFromObjC(messageJSON) { }); } - function _handleMessageFromObjC(messageJSON) { - _dispatchMessageFromObjC(messageJSON); - } - messagingIframe = document.createElement('iframe'); messagingIframe.style.display = 'none'; messagingIframe.src = CUSTOM_PROTOCOL_SCHEME + '://' + QUEUE_HAS_MESSAGE; document.documentElement.appendChild(messagingIframe); - setTimeout(_callWVJBCallbacks, 0); + setTimeout(_callWVJBCallbacks, 1); function _callWVJBCallbacks() { var callbacks = window.WVJBCallbacks; delete window.WVJBCallbacks; @@ -120,4 +116,4 @@ function _callWVJBCallbacks() { #undef __wvjb_js_func__ return preprocessorJSCode; -}; \ No newline at end of file +};