Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
feat: 更新插件初始化,处理安卓的问题 #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Kntt committed Jun 18, 2019
1 parent ff1456c commit 4cd30c4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 15 deletions.
42 changes: 27 additions & 15 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* @author Kntt 20190216
*/
import MockBridge from './mock'

import { getDeviceInfo } from './utils'
const deviceInfo = getDeviceInfo()
export default class VueJsBridgePlugin {
constructor (options = {}) {
this.options = options
Expand All @@ -21,21 +22,32 @@ export default class VueJsBridgePlugin {
const bridge = new MockBridge(mockHandler)
return callback(bridge)
}
// 以下为[WebViewJavascriptBridge](https://github.com/marcuswestin/WebViewJavascriptBridge)源码
if (window.WebViewJavascriptBridge) {
return callback(window.WebViewJavascriptBridge)
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback)
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)
}
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调用的方法
Expand Down
62 changes: 62 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,65 @@ export const type = function (o) {
export const toCamelCaseVar = (variable) => {
return variable.replace(/\-(\w)/g, (_, letter) => letter.toUpperCase())
}

/**
* 获取设备信息
*/
export const 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
}

0 comments on commit 4cd30c4

Please sign in to comment.