|
| 1 | +// Uses Node, AMD or browser globals to create a module. This example creates |
| 2 | +// a global even when AMD is used. This is useful if you have some scripts |
| 3 | +// that are loaded by an AMD loader, but they still want access to globals. |
| 4 | +// If you do not need to export a global for the AMD case, |
| 5 | +// see returnExports.js. |
| 6 | + |
| 7 | +// If you want something that will work in other stricter CommonJS environments, |
| 8 | +// or if you need to create a circular dependency, see commonJsStrictGlobal.js |
| 9 | + |
| 10 | +// Defines a module "returnExportsGlobal" that depends another module called |
| 11 | +// "b". Note that the name of the module is implied by the file name. It is |
| 12 | +// best if the file name and the exported global have matching names. |
| 13 | + |
| 14 | +// If the 'b' module also uses this type of boilerplate, then |
| 15 | +// in the browser, it will create a global .b that is used below. |
| 16 | + |
| 17 | + |
| 18 | +//document.write('<script src=""></script>'); |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +(function (root, factory) { |
| 24 | + root.JsBridge = factory(root); |
| 25 | +}(typeof window == "undefined" ? this : window, function (win) { |
| 26 | + if (!win.document) { return {};} |
| 27 | + |
| 28 | + |
| 29 | + var doc = win.document, |
| 30 | + title = doc.title, |
| 31 | + ua = navigator.userAgent.toLowerCase(), |
| 32 | + platform = navigator.platform.toLowerCase(), |
| 33 | + isMacorWin = !(!platform.match("mac") && !platform.match("win")), |
| 34 | + isandroid = -1 != ua.indexOf("android"), |
| 35 | + isphoneorpad = -1 != ua.indexOf("iphone") || -1 != ua.indexOf("ipad"), |
| 36 | + JsBridge = { |
| 37 | + usable: false, |
| 38 | + init: function (bridge) { |
| 39 | + /*注册提供给native的接口*/ |
| 40 | + bridge.registerHandler("exam",function(message, responseCallback){ |
| 41 | + |
| 42 | + var result=document.getElementById("result"); |
| 43 | + result.innerHTML = 'native传递的数据:'+JSON.stringify(message); |
| 44 | + responseCallback({ |
| 45 | + status: "1", |
| 46 | + msg: "ok", |
| 47 | + values:{ |
| 48 | + msg: "js回调native" |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + bridge.registerHandler("exam1",function(message, responseCallback){ |
| 54 | + |
| 55 | + var result=document.getElementById("result"); |
| 56 | + result.innerHTML = 'native传递的数据:'+JSON.stringify(message); |
| 57 | + responseCallback({ |
| 58 | + status: "1", |
| 59 | + msg: "ok", |
| 60 | + values:{ |
| 61 | + cityName:message.cityName, |
| 62 | + cityProvince: message.cityProvince |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + bridge.registerHandler("exam2",function(message, responseCallback){ |
| 68 | + |
| 69 | + var result=document.getElementById("result"); |
| 70 | + result.innerHTML = 'native传递的数据:'+JSON.stringify(message); |
| 71 | + responseCallback({ |
| 72 | + status: "1", |
| 73 | + msg: "ok", |
| 74 | + values:{ |
| 75 | + city:{ |
| 76 | + cityName:message.cityName, |
| 77 | + cityProvince: message.cityProvince |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + bridge.registerHandler("exam3",function(message, responseCallback){ |
| 85 | + |
| 86 | + var result=document.getElementById("result"); |
| 87 | + result.innerHTML = 'native传递的数据:'+JSON.stringify(message); |
| 88 | + responseCallback({ |
| 89 | + status: "1", |
| 90 | + msg: "ok", |
| 91 | + values:{ |
| 92 | + cityName:'北京', |
| 93 | + cityProvince: '北京' |
| 94 | + |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + bridge.registerHandler("exam4",function(message, responseCallback){ |
| 100 | + |
| 101 | + var result=document.getElementById("result"); |
| 102 | + result.innerHTML = 'native传递的数据:'+JSON.stringify(message); |
| 103 | + responseCallback({ |
| 104 | + status: "1", |
| 105 | + msg: "ok", |
| 106 | + values:{ |
| 107 | + |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + return this; |
| 113 | + }, |
| 114 | + checkUsable: function (methodName, params, cb) { |
| 115 | + var _this = this; |
| 116 | + if (!window._JSNativeBridge) { |
| 117 | + //JS not be injected success |
| 118 | + cb({ |
| 119 | + status: "-1", |
| 120 | + msg: "window._JSNativeBridge is undefined" |
| 121 | + }, {}); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + try { |
| 126 | + window._JSNativeBridge._doSendRequest(methodName, params, cb); |
| 127 | + } catch (e) { |
| 128 | + cb({status: "-1", msg: e},{}); |
| 129 | + } |
| 130 | + }, |
| 131 | + |
| 132 | + test: function(params,cb){ |
| 133 | + this.checkUsable("test",{ |
| 134 | + "msg": "js发送的请求" |
| 135 | + },cb); |
| 136 | + }, |
| 137 | + |
| 138 | + test1: function(params,cb){ |
| 139 | + this.checkUsable("test1",{ |
| 140 | + "age": 10, |
| 141 | + "name":"wangwu" |
| 142 | + },cb); |
| 143 | + }, |
| 144 | + |
| 145 | + test2: function(params,cb){ |
| 146 | + this.checkUsable("test2",{ |
| 147 | + person:{ |
| 148 | + "age": 10, |
| 149 | + "name":"wangwu" |
| 150 | + } |
| 151 | + |
| 152 | + },cb); |
| 153 | + }, |
| 154 | + |
| 155 | + test3: function(params,cb){ |
| 156 | + this.checkUsable("test3",{ |
| 157 | + "jiguan":"北京", |
| 158 | + person:{ |
| 159 | + "age": 10, |
| 160 | + "name":"wangwu" |
| 161 | + } |
| 162 | + |
| 163 | + },cb); |
| 164 | + }, |
| 165 | + test4: function(params,cb){ |
| 166 | + this.checkUsable("test4",{ |
| 167 | + |
| 168 | + },cb); |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + }; |
| 173 | + |
| 174 | + if (window._JSNativeBridge) { |
| 175 | + console.log("native injection js success!"); |
| 176 | + window._JSNativeBridge.protocol.scheme = 'niu'; |
| 177 | + window._JSNativeBridge.protocol.host = 'receive_msg'; |
| 178 | + window._JSNativeBridge.debug = true; |
| 179 | + JsBridge.init(window._JSNativeBridge); |
| 180 | + } else { |
| 181 | + console.log("native injection js wrong!"); |
| 182 | + document.addEventListener( |
| 183 | + 'JsBridgeInit', |
| 184 | + function(event) { |
| 185 | + console.log('------------------bridge'); |
| 186 | + window._JSNativeBridge.protocol.scheme = 'niu'; |
| 187 | + window._JSNativeBridge.protocol.host = 'receive_msg'; |
| 188 | + event.bridge.debug = true; |
| 189 | + JsBridge.init(event.bridge); |
| 190 | + }, |
| 191 | + false |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + return JsBridge; |
| 196 | +})); |
0 commit comments