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.

feat: 更新插件初始化,处理安卓的问题 #12 #13

Merged
Kntt merged 1 commit into master from dev_android
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions lib/plugin.js
View file Open in desktop
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
View file Open in desktop
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
}

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