Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 26, 2021. It is now read-only.

Commit 4cd30c4

Browse files
committed
feat: 更新插件初始化,处理安卓的问题 #12
1 parent ff1456c commit 4cd30c4

File tree

2 files changed

+89
-15
lines changed

2 files changed

+89
-15
lines changed

‎lib/plugin.js‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* @author Kntt 20190216
44
*/
55
import MockBridge from './mock'
6-
6+
import { getDeviceInfo } from './utils'
7+
const deviceInfo = getDeviceInfo()
78
export default class VueJsBridgePlugin {
89
constructor (options = {}) {
910
this.options = options
@@ -21,21 +22,32 @@ export default class VueJsBridgePlugin {
2122
const bridge = new MockBridge(mockHandler)
2223
return callback(bridge)
2324
}
24-
// 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码
25-
if (window.WebViewJavascriptBridge) {
26-
return callback(window.WebViewJavascriptBridge)
27-
}
28-
if (window.WVJBCallbacks) {
29-
return window.WVJBCallbacks.push(callback)
25+
if (deviceInfo.android) {
26+
// 以下为[JsBridge](https://github.com/lzyzsd/JsBridge)源码 -- android
27+
if (window.WebViewJavascriptBridge) {
28+
callback(window.WebViewJavascriptBridge)
29+
} else {
30+
document.addEventListener('WebViewJavascriptBridgeReady', function () {
31+
callback(window.WebViewJavascriptBridge)
32+
}, false)
33+
}
34+
} else {
35+
// 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码 -- ios
36+
if (window.WebViewJavascriptBridge) {
37+
return callback(window.WebViewJavascriptBridge)
38+
}
39+
if (window.WVJBCallbacks) {
40+
return window.WVJBCallbacks.push(callback)
41+
}
42+
window.WVJBCallbacks = [callback]
43+
var WVJBIframe = document.createElement('iframe')
44+
WVJBIframe.style.display = 'none'
45+
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
46+
document.documentElement.appendChild(WVJBIframe)
47+
setTimeout(function () {
48+
document.documentElement.removeChild(WVJBIframe)
49+
}, 0)
3050
}
31-
window.WVJBCallbacks = [callback]
32-
var WVJBIframe = document.createElement('iframe')
33-
WVJBIframe.style.display = 'none'
34-
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
35-
document.documentElement.appendChild(WVJBIframe)
36-
setTimeout(function () {
37-
document.documentElement.removeChild(WVJBIframe)
38-
}, 0)
3951
}
4052
/**
4153
* 注册提供native调用的方法

‎lib/utils.js‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,65 @@ export const type = function (o) {
2525
export const toCamelCaseVar = (variable) => {
2626
return variable.replace(/\-(\w)/g, (_, letter) => letter.toUpperCase())
2727
}
28+
29+
/**
30+
* 获取设备信息
31+
*/
32+
export const getDeviceInfo = () => {
33+
var device = {}
34+
var ua = navigator.userAgent
35+
36+
var windows = ua.match(/(WindowsPhone);?[\s\/]+([\d.]+)?/)
37+
var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/)
38+
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/)
39+
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/)
40+
var iphone = !ipad && ua.match(/(iPhone\sOS|iOS)\s([\d_]+)/)
41+
42+
device.ios = device.android = device.windows = device.iphone = device.ipod = device.ipad = device.androidChrome = false
43+
44+
// Windows
45+
if (windows) {
46+
device.os = 'windows'
47+
device.osVersion = windows[2]
48+
device.windows = true
49+
}
50+
// Android
51+
if (android && !windows) {
52+
device.os = 'android'
53+
device.osVersion = android[2]
54+
device.android = true
55+
device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0
56+
}
57+
if (ipad || iphone || ipod) {
58+
device.os = 'ios'
59+
device.ios = true
60+
}
61+
// iOS
62+
if (iphone && !ipod) {
63+
device.osVersion = iphone[2].replace(/_/g, '.')
64+
device.iphone = true
65+
}
66+
if (ipad) {
67+
device.osVersion = ipad[2].replace(/_/g, '.')
68+
device.ipad = true
69+
}
70+
if (ipod) {
71+
device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null
72+
device.iphone = true
73+
}
74+
// iOS 8+ changed UA
75+
if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
76+
if (device.osVersion.split('.')[0] === '10') {
77+
device.osVersion = ua
78+
.toLowerCase()
79+
.split('version/')[1]
80+
.split(' ')[0]
81+
}
82+
}
83+
device.iphonex = device.ios && screen.height === 812 && screen.width === 375
84+
// Webview
85+
device.webView =
86+
(iphone || ipad || ipod) && ua.match(/.*AppleWebKit(?!.*Safari)/i)
87+
88+
return device
89+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /