All Projects → Kntt → vue-js-bridge

Kntt / vue-js-bridge

Licence: MIT license
vue-js-bridge for Vue.js

Programming Languages

javascript
184084 projects - #8 most used programming language
java
68154 projects - #9 most used programming language
HTML
75241 projects
Vue
7211 projects
shell
77523 projects

Projects that are alternatives of or similar to vue-js-bridge

QuickWebKit
A great & strong plugin based WebViewController. 一款基于插件的 WebView 视图控制器,您可以基于它设计您的浏览器插件,然后像积木一样来组装它们。
Stars: ✭ 29 (-29.27%)
Mutual labels:  webview, jsbridge
autojs-webView
autojs的webView实现,支持初始化脚本注入、jsBridge两端互调
Stars: ✭ 38 (-7.32%)
Mutual labels:  webview, jsbridge
animaris
Documentation and Mock for JSBridge base on ThinkJS & MongoDB & React & Antd.
Stars: ✭ 28 (-31.71%)
Mutual labels:  webview, jsbridge
Webviewane
WebView Adobe Air Native Extension for macOS 10.10+, Windows Desktop, iOS 9.0+ and Android 19+. This ANE provides access to a more modern webview from AIR.
Stars: ✭ 169 (+312.2%)
Mutual labels:  webview
Android Advancedwebview
Enhanced WebView component for Android that works as intended out of the box
Stars: ✭ 2,186 (+5231.71%)
Mutual labels:  webview
React Native Webview Invoke
Invoke functions between React Native and WebView
Stars: ✭ 211 (+414.63%)
Mutual labels:  webview
Google-Docs-for-Mac
Native Google Docs app for Mac
Stars: ✭ 33 (-19.51%)
Mutual labels:  webview
Easybridge
A design of easy js-bridge which provide the ability to communicate between java and javascript.It is based on the android webview's feature [addJavaScriptInterface]
Stars: ✭ 158 (+285.37%)
Mutual labels:  webview
Vue-JsBridge
JsBridge for Vue
Stars: ✭ 15 (-63.41%)
Mutual labels:  jsbridge
Tauri
Build smaller, faster, and more secure desktop applications with a web frontend.
Stars: ✭ 25,383 (+61809.76%)
Mutual labels:  webview
Android Youtube Player
YouTube Player library for Android and Chromecast, stable and customizable.
Stars: ✭ 2,510 (+6021.95%)
Mutual labels:  webview
React Native Turbolinks
React Native adapter for building hybrid apps with Turbolinks 5
Stars: ✭ 177 (+331.71%)
Mutual labels:  webview
Cargo
🚂🚋🚋 A browser with almost no UI.
Stars: ✭ 221 (+439.02%)
Mutual labels:  webview
Slimsocial For Facebook
Light version of Facebook. Light both in the weight and in the use.
Stars: ✭ 171 (+317.07%)
Mutual labels:  webview
JsBridge-WebViewJavascriptBridge-Sample
iOS JsBridge Solution by WebViewJavascriptBridge
Stars: ✭ 16 (-60.98%)
Mutual labels:  jsbridge
Hybridfoundation
混合应用基础架构 跨平台热更新方案 Js双向通信 基础WebView
Stars: ✭ 164 (+300%)
Mutual labels:  webview
qml-webchannel-websockets
QML examples for WebChannel and WebSockets.
Stars: ✭ 26 (-36.59%)
Mutual labels:  webview
Vscode Kanban
Kanban board for Visual Studio Code.
Stars: ✭ 191 (+365.85%)
Mutual labels:  webview
Os Fileup
Helper app to understand how to upload files and do basic image/video processing in hybrid android apps.
Stars: ✭ 190 (+363.41%)
Mutual labels:  webview
Recaptcha
[In]visible ReCaptcha v2 for iOS
Stars: ✭ 208 (+407.32%)
Mutual labels:  webview

vue-webview-js-bridge

GitHub GitHub package.json version

  • WebviewJavascriptBridge plugin for Vue.js
  • 基于WebViewJavascriptBridge(ios), JsBridge(android)开发
  • Promise封装,支持then或者async/await等方式

安装

yarn:

yarn add vue-webview-js-bridge

npm:

npm i vue-webview-js-bridge

Example

// main.js
import Vue from 'vue'
import VueJsBridge from 'vue-webview-js-bridge'

Vue.use(VueJsBridge, {
  debug: true,
  nativeHandlerName: 'testObjcCallback',
  mock: true,
  mockHandler (payload, next) {
    // mock by payload
    // call next(data) to mock data
  }
  // ...
})

// component.vue
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data () {
    return {
      code: ''
    }
  },
  mounted () {
    this.$bridge.registerHandler('testJavascriptHandler', (data, callback) => {
      this.code = data
      console.log('data from native:', data)
      const responseData = { 'Javascript Says':'Right back atcha!' }
			console.log('JS responding with', responseData)
      callback(responseData)
    })
  },
  methods: {
    async callNative () {
      try {
        let res = await this.$bridge.callHandler({
          type: 'optionType',
          data: {
            name: 'optionData'
          }
        })
        this.code = res
      } catch (error) {
        console.log('error', error)
      }
    }
  }
}

TypeScript 支持

main.ts

// ...
import VueJsBridge, { pluginOption } from 'vue-webview-js-bridge'

interface Payload<T = any> {
  type: string
  data?: T
}
interface Response<T = any> {
  code: number
  data?: T
}

const option:pluginOption<Payload, Response> = {
  debug: true,
  nativeHandlerName: 'testObjcCallback',
  mock: false,
  mockHandler (payload, next) {
    next(Object.assign({ code: 200 }, {
      data: 'code from native'
    }))
  }
}

Vue.use(VueJsBridge, option)
// ...

component.vue

import { Vue, Component, Prop } from 'vue-property-decorator'

interface Payload<T = any> {
  type: string
  data?: T
}

interface Response<T = any> {
  code: number
  data?: T
}
@Component
export default class HelloWorld extends Vue {
  @Prop({ default: '' }) private msg!: string
  code: string = ''
  mounted () {
    this.$bridge.registerHandler<string, object>('testJavascriptHandler', (data, callback) => {
      this.code = data
      console.log('data from native:', data)
      const responseData:object = { 'Javascript Says': 'Right back atcha!' }
      console.log('JS responding with', responseData)
      callback(responseData)
    })
  }
  private async callNative () {
    try {
      let res = await this.$bridge.callHandler<Payload<object>, Response<string>>({
        type: 'optionType',
        data: {
          name: 'optionData'
        }
      })
      this.code = res.data
    } catch (error) {
      console.log('error', error)
    }
  }
}

配置参数(Options)

debug

  • type: boolean
  • default: true
  • description: 输出调用信息

delay

  • type: number
  • default: 200
  • description: 由于birdge初始化需要时间导致的registerHandler失败的处理,延时调用时间,单位ms

native调用前端注册的方法最好也要延时处理,避免前端还未注册时候native调用导致的问题

nativeHandlerName

  • type: string, 必填项
  • default: 'nativeHandler'
  • description: 和原生开发人员协商的nativeHandlerName

mock

  • type: boolean
  • default: true
  • description: 开发阶段是否开启mock服务,需要配合mockHandler使用,两者都设置的情况下mock生效

mockHandler

  • type: Function
  • default: null
  • description: 开发阶段mockHandler服务,需要配合mock使用,两者都设置的情况下mock生效. 是一个函数,第一个参数接收payload, 第二个参数接受bridge回调函数
mockHandler (payload, next) {
  // mock by payload
  // switch(payload) {
  //  case 1:
  //    next(mockData)
  //    break
  //    ...
  // }
  // call next(data) to mock data
}

提供的方法(Methods)

registerHandler

  • description:注册一个bridge提供给原生开发者调用,第一个参数name(和原生开发者协商好的bridgeName),第二个参数callback函数,
  • callback: callback函数接收两个参数,第一个参数是native传来的数据data,第二个参数是原生传来的responseCallback,当需要时通知native我们的状态
this.$bridge.registerHandler('testJavascriptHandler', (data, callback) => {
  this.code = data
  console.log('data from native:', data)
  const responseData = { 'Javascript Says':'Right back atcha!' }
  console.log('JS responding with', responseData)
  callback(responseData)
})

callHandler

  • description: 接受一个参数payload(和原生开发人员协商格式)例如:
  this.$bridge.callHandler({
    type: 'optionType', // 标识调用native的功能
    data: { // 传到native的数据
      name: 'optionData'
    }
  })

Todo

  • 增加单元测试
  • 增加 TypeScript types 支持

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].