All Projects → hcanyz → ZJsBridge

hcanyz / ZJsBridge

Licence: MIT license
一套完整的native-bridge-web协议与实现,清晰规范的开发Hybrid App

Programming Languages

kotlin
9241 projects
javascript
184084 projects - #8 most used programming language
java
68154 projects - #9 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ZJsBridge

SwiftJSBridge
SwiftJSBridge is a handy JavaScript Bridge, written in Swift, support WKWebView and UIWebView
Stars: ✭ 29 (+38.1%)
Mutual labels:  jsbridge, jsbridge-webview
autojs-webView
autojs的webView实现,支持初始化脚本注入、jsBridge两端互调
Stars: ✭ 38 (+80.95%)
Mutual labels:  jsbridge
QuickWebKit
A great & strong plugin based WebViewController. 一款基于插件的 WebView 视图控制器,您可以基于它设计您的浏览器插件,然后像积木一样来组装它们。
Stars: ✭ 29 (+38.1%)
Mutual labels:  jsbridge
AppHostExample
a web-based project embeded AppHost
Stars: ✭ 16 (-23.81%)
Mutual labels:  jsbridge
WKBridge
Bridge for WKWebView and JavaScript
Stars: ✭ 70 (+233.33%)
Mutual labels:  jsbridge
vue-js-bridge
vue-js-bridge for Vue.js
Stars: ✭ 41 (+95.24%)
Mutual labels:  jsbridge
FMWebViewJavascriptBridge
FMWebViewJavascriptBridge inspired by WebViewJavascripBridge and react native
Stars: ✭ 29 (+38.1%)
Mutual labels:  jsbridge
JsBridge-WebViewJavascriptBridge-Sample
iOS JsBridge Solution by WebViewJavascriptBridge
Stars: ✭ 16 (-23.81%)
Mutual labels:  jsbridge
Vue-JsBridge
JsBridge for Vue
Stars: ✭ 15 (-28.57%)
Mutual labels:  jsbridge
Jspatch
JSPatch bridge Objective-C and Javascript using the Objective-C runtime. You can call any Objective-C class and method in JavaScript by just including a small engine. JSPatch is generally used to hotfix iOS App.
Stars: ✭ 11,337 (+53885.71%)
Mutual labels:  jsbridge
Dsbridge Ios
🌏 A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
Stars: ✭ 1,673 (+7866.67%)
Mutual labels:  jsbridge
Dsbridge Android
🌎 A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
Stars: ✭ 3,239 (+15323.81%)
Mutual labels:  jsbridge
JSBridge
JSBridge与Native通信,对Web端调用进行封装
Stars: ✭ 19 (-9.52%)
Mutual labels:  jsbridge
animaris
Documentation and Mock for JSBridge base on ThinkJS & MongoDB & React & Antd.
Stars: ✭ 28 (+33.33%)
Mutual labels:  jsbridge

ZJsBridge

参考微信jsBridge的一套完整的native-bridge-web协议与实现,清晰规范的开发Hybrid App

Support API v19+
Support androidx

js实现: zfjs-sdk库

ZJsBridge能做什么

  • 对web端提供js-sdk,形成sdk概念,统一app对外api,统一的api管理(权限、版本兼容)
  • 让native端api具备组件化能力,不在需要将所有api写个一个modlue中
  • 保障native-web js交互的数据一致性、安全

什么场景下需要使用ZJsBridge

  • 项目中有较多的web与native交互,需要native统一提供对外api
  • native组件化,需要在不同模块中实现api逻辑

标准api:zfjs-sdk-api

bridge协议:Native-Bridge协议

虚拟资源协议:nativeResourceUrl协议

如何使用((推荐)详见本项目demo)

添加依赖

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.hcanyz:ZJsBridge:$version'
}

需要修改的类

  • Webview(android、x5...)
  • WebViewClient(添加一些方法调用,协助zjs感知webview生命周期)
  • activity|fragment
    • 注册api实现类
    • 添加一些方法,协助zjs感知容器生命周期
WebView implements IZWebView
class WebView : WebView, IZWebView {
    
    private val zWebHelper: ZWebHelper by lazy { ZWebHelper(this) }

    override fun getCurUrl(): String {
        return url
    }

    override fun getCurContext(): Context {
        return context
    }

    override fun getCurZWebHelper(): ZWebHelper {
        return zWebHelper
    }

    override fun execJs(methodName: String, params: String?, valueCallback: ValueCallback<String>?) {
        val js: String = if (params.isNullOrBlank()) {
            String.format("%s()", methodName)
        } else {
            String.format("%s('%s')", methodName, params)
        }
        execJs(js, valueCallback)
    }

    override fun execJs(sourceJs: String, valueCallback: ValueCallback<String>?) {
        if (ZJsBridge.ZJS_DEBUG) ZJsBridge.log("evaluateJavascript:javascript:$sourceJs")
        runOnMainThread(Runnable {
            evaluateJavascript("javascript:$sourceJs") { valueCallback?.onReceiveValue(it) }
        })
    }

    override fun runOnMainThread(runnable: Runnable) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            runnable.run()
            return
        }
        post(runnable)
    }
}
Webview addJavascriptInterface
addJavascriptInterface(ZJavascriptInterface(this), ZJavascriptInterface.INTERFACE_NAME)
WebViewClient
private inner class InnerCustomWebViewClient : WebViewClient() {
    override fun onPageFinished(webView: WebView?, s: String?) {
        super.onPageFinished(webView, s)
        zWebHelper.injectCoreJs()
    }

    override fun doUpdateVisitedHistory(p0: WebView?, p1: String?, p2: Boolean) {
        super.doUpdateVisitedHistory(p0, p1, p2)
        zWebHelper.injectCoreJs()
    }
}
registeredJsApiHandler
web_test.getCurZWebHelper().registeredJsApiHandler(this, ZCommonJsHandler::class.java)
web_test.getCurZWebHelper().registeredJsApiHandler(this, ImageJsHandler::class.java)
activity|fragment容器 implements IZWebViewContainer
override fun closeWindow() {
    finish()
}

override fun updateTitle(title: String) {
    tv_test_tile.text = title
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    web_test.getCurZWebHelper().dispatchContainerResult(requestCode, resultCode, data)
}

override fun onDetachedFromWindow() {
    super.onDetachedFromWindow()
    web_test.getCurZWebHelper().dispatchContainerDestroy()
}

override fun onBackPressed() {
    if (web_test.canGoBack()) {
        web_test.goBack()
    } else {
        super.onBackPressed()
    }
}

api测试

这个库提供了一个h5的api测试页面

本项目中已集成一个打包后的产物,可以自行编译替换(ZJsBridge\app\src\main\assets)

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].