This repository was archived by the owner on Jul 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvue-webview-js-bridge.esm.js
301 lines (269 loc) · 8.7 KB
/
vue-webview-js-bridge.esm.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* vue-webview-js-bridge v0.0.8
* (c) 2019 Kntt
* @license MIT
*/
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
/**
* 获取数据类型
* @param {*} o
* @returns {string}
*/
var type = function type(o) {
var types = {
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regexp',
'[object Object]': 'object',
'[object Error]': 'error'
};
if (o == null) {
return String(o);
}
return (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === 'object' ? types[Object.prototype.toString.call(o)] || 'object' : typeof o === 'undefined' ? 'undefined' : _typeof(o);
};
/**
* 获取设备信息
*/
var getDeviceInfo = function getDeviceInfo() {
var device = {};
var ua = navigator.userAgent;
var windows = ua.match(/(Windows Phone);?[\s\/]+([\d.]+)?/);
var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
var iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/);
device.ios = device.android = device.windows = device.iphone = device.ipod = device.ipad = device.androidChrome = false;
// Windows
if (windows) {
device.os = 'windows';
device.osVersion = windows[2];
device.windows = true;
}
// Android
if (android && !windows) {
device.os = 'android';
device.osVersion = android[2];
device.android = true;
device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0;
}
if (ipad || iphone || ipod) {
device.os = 'ios';
device.ios = true;
}
// iOS
if (iphone && !ipod) {
device.osVersion = iphone[2].replace(/_/g, '.');
device.iphone = true;
}
if (ipad) {
device.osVersion = ipad[2].replace(/_/g, '.');
device.ipad = true;
}
if (ipod) {
device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
device.iphone = true;
}
// iOS 8+ changed UA
if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
if (device.osVersion.split('.')[0] === '10') {
device.osVersion = ua.toLowerCase().split('version/')[1].split(' ')[0];
}
}
device.iphonex = device.ios && screen.height === 812 && screen.width === 375;
// Webview
device.webView = (iphone || ipad || ipod) && ua.match(/.*AppleWebKit(?!.*Safari)/i);
return device;
};
/**
* VueJsBridgePlugin
* @author Kntt 20190216
*/
var MockBridge = function () {
function MockBridge(mockHandler) {
classCallCheck(this, MockBridge);
if (Object.prototype.toString.call(mockHandler) !== '[object Function]') {
throw new Error('mockHandler ' + type(mockHandler) + ' is not a function');
}
this.mockHandler = mockHandler;
}
createClass(MockBridge, [{
key: 'registerHandler',
value: function registerHandler(name, fn) {}
}, {
key: 'callHandler',
value: function callHandler(nativeHandler, payload, callback) {
this.mockHandler(payload, callback);
}
}]);
return MockBridge;
}();
/**
* VueJsBridgePlugin
* @author Kntt 20190216
*/
var deviceInfo = getDeviceInfo();
var VueJsBridgePlugin = function () {
function VueJsBridgePlugin() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
classCallCheck(this, VueJsBridgePlugin);
this.options = options;
}
/**
* 初始化核心bridge插件
* @param {function} callback
* callback 回传 bridge插件实例
*/
createClass(VueJsBridgePlugin, [{
key: 'init',
value: function init(callback) {
var _options = this.options,
debug = _options.debug,
mock = _options.mock,
mockHandler = _options.mockHandler;
// 如果mock & mockHandler 同时存在,注册MockBridge,方便开发
if (mock && mockHandler) {
debug && console.log('[VueJsBridge] work in mock mode...');
var bridge = new MockBridge(mockHandler);
return callback(bridge);
}
if (deviceInfo.android) {
// 以下为[JsBridge](https://github.com/lzyzsd/JsBridge)源码 -- android
if (window.WebViewJavascriptBridge) {
callback(window.WebViewJavascriptBridge);
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function () {
callback(window.WebViewJavascriptBridge);
}, false);
}
} else {
// 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码 -- ios
if (window.WebViewJavascriptBridge) {
return callback(window.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);
}
}
/**
* 注册提供native调用的方法
* @param {string} name 方法名称
* @param {function} fn 回调方法, 两个参数 data, callback
* 第一个参数是native传递的数据 data
* 第二个参数是native提供的回调函数 callback,前端处理完成后可以通过 callback通知native
*/
}, {
key: 'registerHandler',
value: function registerHandler(name, fn) {
var _this = this;
var delay = this.options.delay;
// birdge初始化需要时间,延迟处理注册方法
setTimeout(function () {
_this.init(function (bridge) {
bridge.registerHandler(name, fn);
});
}, delay);
}
/**
* 前端调用native的方法
* @param {any} payload
* payload 参数通过和native协商定义
* @example
* {
* type: 'nativeMethodType',
* data: 'data from front'
* }
*/
}, {
key: 'callHandler',
value: function callHandler(payload) {
var _options2 = this.options,
debug = _options2.debug,
nativeHandlerName = _options2.nativeHandlerName;
var _resolve = void 0;
var _reject = void 0;
// 生成promise对象,保留resolve,reject
var readyPromise = new Promise(function (resolve, reject) {
_resolve = resolve;
_reject = reject;
});
debug && console.info('[VueJsBridge] Start calling NativeHandler with payload:', payload);
this.init(function (bridge) {
try {
bridge.callHandler(nativeHandlerName, payload, function (response) {
debug && console.info('[VueJsBridge] Success response:', response);
// 调用成功,使用保留的resolve改变返回的promise状态
_resolve(response);
});
} catch (e) {
debug && console.info('[VueJsBridge] Failed error:', e);
// 调用成功,使用保留的reject改变返回的promise状态
_reject(e);
}
});
return readyPromise;
}
}]);
return VueJsBridgePlugin;
}();
var defaultOptions = {
debug: true,
schemaName: 'yy',
delay: 200,
nativeHandlerName: 'nativeHandler',
mock: true,
mockHandler: null
};
var index = {
install: function install(Vue, options) {
var initConfig = _extends({}, defaultOptions, options);
Object.defineProperty(Vue.prototype, '$bridge', { value: new VueJsBridgePlugin(initConfig) });
},
version: '0.0.8'
};
export default index;