-
Notifications
You must be signed in to change notification settings - Fork 15
/
vue-bridge-webview.js
155 lines (133 loc) · 4.26 KB
/
vue-bridge-webview.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Vue Bridge Webview v1.1.0
* https://github.com/cmp-cc/vue-bridge-webview
*
* Copyright 2016, cmp-cc
* Released under the MIT license
*/
(function() {
/**
* vue-bridge-webview config
*
* @type {{bridgeWebViewDelay: number}}
*/
var bridgeConfig = {
bridgeWebViewDelay : 0,
callHandle : {}, // bridge android / ios
silent : false
}
var $bridge = {
registerHandler : function (name,callback) {
if(bridgeConfig.silent){
console.log(name,' register handler failure')
}
},
callHandler : function (name, params, callback) {
if(bridgeConfig.silent){
console.log(name,' call handler webView failure')
}
}
};
// ============ device init operation start ===========
/* setup WebView Javascript Bridge for ios , ios 初始化 */
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
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)
}
/* 用于创建桥接对象的函数 , android 初始化 */
function connectWebViewJavascriptBridge(callback) {
//如果桥接对象已存在,则直接调用callback函数
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
}
//否则添加一个监听器来执行callback函数
else {
document.addEventListener('WebViewJavascriptBridgeReady', function () {
callback(WebViewJavascriptBridge)
}, false)
}
}
/* device detect for ios/android */
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
setupWebViewJavascriptBridge(function(bridge){
$bridge = bridge
})
}else if(/(Android)/i.test(navigator.userAgent)) {
connectWebViewJavascriptBridge(function(bridge){
$bridge = bridge
})
}
// ==============device init operation end ============
var VueBridgeWebView = {
install: function(Vue) {
Vue.prototype.$bridge = this
Vue.bridge = this
// config
bridgeConfig.silent = Vue.config.silent
},
config : function(bridgeWebViewDelay,silent) {
if(bridgeWebViewDelay) {
bridgeConfig.bridgeWebViewDelay = bridgeWebViewDelay;
}
if(silent) {
bridgeConfig.silent = silent;
}
},
/**
* Android / IOS 调用JS,需要明确调用的`function名称` .
*
*
* @param name `function name`
* @param registerCallback 回调的响应事件
*/
registerHandler : function(name, registerCallback){
if($bridge['registerHandler']){
setTimeout(function(){
$bridge.registerHandler(name,registerCallback)
},bridgeConfig.bridgeWebViewDelay)
}else{
console.log("don't built-in WebView invoking ",name,'{registerHandler}')
}
},
/**
* JS 调用 Android / IOS
*
* name: 回调名称, android/ios 名称 ,eg: 'getUserInfo'
* params 请求参数, eg: { 'userId' : 1}
* callback: response(响应函数)
*
* eg: this.$bridge.callHandler('getUserInfo',{'userId':1},function(data){...})
*
*/
callHandler: function(name,params,callback){
if($bridge['callHandler']){
$bridge.callHandler(name,params,function(data){
if(typeof callback == 'function'){
/* 解决部分系统加载延迟导致 ios/android 不响应问题 */
setTimeout(function(){
callback(data);
},bridgeConfig.bridgeWebViewDelay)
}
})
}else {
console.log("don't built-in WebView invoking ",name,'{callHandler}')
}
}
}
if (typeof exports == "object") {
module.exports = VueBridgeWebView;
} else if (typeof define == "function" && define.amd) {
define([], function(){ return VueBridgeWebView; })
} else if (window.Vue) {
window.$bridge = VueBridgeWebView
Vue.use(VueBridgeWebView);
}else{
window.$bridge = VueBridgeWebView
}
})()