-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJSBridge.js
More file actions
101 lines (95 loc) · 2.81 KB
/
JSBridge.js
File metadata and controls
101 lines (95 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;
(function(global) {
function JSBridge() {
this.name = 'JSBridge';
this.reset = true;
};
JSBridge.prototype.device = function() {
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
return isAndroid;
};
/**
* @param null
* JSBridge初始化
*/
JSBridge.prototype._init = function(callback) {
if (window.WebViewJavascriptBridge) {
return callback(WebViewJavascriptBridge);
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function(WebViewJavascriptBridge) {
return callback(WebViewJavascriptBridge);
}, false);
}
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
};
/**
* @param null
* JSBridge 安卓初始化回调接口
*/
JSBridge.prototype.__init__ = function(bridge) {
var isAn = this.device();
if (this.reset && isAn) {
this.reset = false;
bridge.init(function(message, responseCallback) {
console.log('JS got a message', message);
var data = {
'Javascript Responds': '测试中文!'
};
console.log('JS responding with', data);
responseCallback(data);
});
}
};
/**
* @param data-> null
* 获取用户信息 返回用户信息 token ,refreshToken 等等
*/
JSBridge.prototype.getUserInfo = function(data, callback) {
this._init(function(bridge) {
this.__init__(bridge);
bridge.callHandler('getUserInfo', data, function(response) {
if(typeof response === 'object'){
callback(response)
}else{
callback(JSON.parse(response));
}
})
}.bind(this))
};
/**
* 根据自己需求添加方法
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
if (typeof module !== 'undefined' && module.exports) {
module.exports = JSBridge;
} else if (typeof define === 'function' && (define.amd || define.cmd)) {
define(function() {
return JSBridge;
});
} else {
global.JSBridge = JSBridge;
};
})(this);