Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
uknownothingsnow authored Nov 5, 2021
2 parents ec401f5 + ce4d01c commit 11999d5
Show file tree
Hide file tree
Showing 26 changed files with 1,097 additions and 581 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Android CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Gradle
run: ./gradlew build
Binary file added JsBridge.zip
Binary file not shown.
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#JsBridge
# JsBridge

-----

Expand Down Expand Up @@ -129,8 +129,7 @@ will print 'JS got a message hello' and 'JS responding with' in webview console.
## Notice

This lib will inject a WebViewJavascriptBridge Object to window object.
So in your js, before use WebViewJavascriptBridge, you must detect if WebViewJavascriptBridge exist.
If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBridgeReady event, as the blow code shows:
You can listen to `WebViewJavascriptBridgeReady` event to ensure `window.WebViewJavascriptBridge` is exist, as the blow code shows:

```javascript

Expand All @@ -148,6 +147,38 @@ If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBri

```

Or put all JsBridge function call into `window.WVJBCallbacks` array if `window.WebViewJavascriptBridge` is undefined, this taks queue will be flushed when `WebViewJavascriptBridgeReady` event triggered.

Copy and paste setupWebViewJavascriptBridge into your JS:

```javascript
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
return callback(WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
}
```

Call `setupWebViewJavascriptBridge` and then use the bridge to register handlers or call Java handlers:

```javascript
setupWebViewJavascriptBridge(function(bridge) {
bridge.registerHandler('JS Echo', function(data, responseCallback) {
console.log("JS Echo called with:", data);
responseCallback(data);
});
bridge.callHandler('ObjC Echo', {'key':'value'}, function(responseData) {
console.log("JS received response:", responseData);
});
});
```

It same with https://github.com/marcuswestin/WebViewJavascriptBridge, that would be easier for you to define same behavior in different platform between Android and iOS. Meanwhile, writing concise code.

## License

This project is licensed under the terms of the MIT license.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -23,7 +23,7 @@ allprojects {
}
google()
}
tasks.withType(Javadoc) { // 这一段是为了消除gbk的错误
tasks.withType(Javadoc) { // 这一段是为了消除gbk的错�?
options{
encoding "UTF-8"
charSet 'UTF-8'
Expand Down
14 changes: 7 additions & 7 deletions example/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
compileSdkVersion 28
// buildToolsVersion "25.0.3"

defaultConfig {
applicationId "com.github.lzyzsd.jsbridge.example"
minSdkVersion 9
targetSdkVersion 25
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -20,8 +20,8 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.code.gson:gson:2.3.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':library')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.code.gson:gson:2.8.5'
}
79 changes: 40 additions & 39 deletions example/src/main/assets/demo.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>
js调用java
</title>
</head>
<body>
<p>
<xmp id="show">
</xmp>
</p>
<p>
<xmp id="init">
</xmp>
</p>
<p>
<input type="text" id="text1" value="用户名(username)" />
</p>
<p>
<input type="text" id="text2" value="password" />
</p>
<p>
<input type="button" id="enter" value="发消息给Native" onclick="testClick();"
/>
</p>
<p>
<input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"
/>
</p>
<p>
<input type="button" id="enter2" value="显示html" onclick="testDiv();" />
</p>
<p>
<input type="file" value="打开文件" />
</p>
</body>
<script>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<title>
js调用java
</title>
</head>

<body>
<p>
<xmp id="show">
</xmp>
</p>
<p>
<xmp id="init">
</xmp>
</p>
<p>
<input type="text" id="text1" value="用户名(username)"/>
</p>
<p>
<input type="text" id="text2" value="password"/>
</p>
<p>
<input type="button" id="enter" value="发消息给Native" onclick="testClick();"
/>
</p>
<p>
<input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"
/>
</p>
<p>
<input type="button" id="enter2" value="显示html" onclick="testDiv();"/>
</p>
<p>
<input type="file" value="打开文件"/>
</p>
</body>
<script>
function testDiv() {
document.getElementById("show").innerHTML = document.getElementsByTagName("html")[0].innerHTML;
}
Expand Down Expand Up @@ -109,7 +109,8 @@
}
});
})
</script>

</script>

</html>

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.github.lzyzsd.jsbridge.example;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeHelper;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.github.lzyzsd.jsbridge.IWebView;
import com.github.lzyzsd.jsbridge.WebViewJavascriptBridge;

/**
* 采用BridgeHelper集成JsBridge功能示例.定制WebView,可只添加实际需要的JsBridge接口.
*
* @author ZhengAn
* @date 2019-07-07
*/
@SuppressLint("SetJavaScriptEnabled")
public class CustomWebView extends WebView implements WebViewJavascriptBridge, IWebView {

private BridgeHelper bridgeHelper;

public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public CustomWebView(Context context) {
super(context);
init();
}

private void init() {
this.setVerticalScrollBarEnabled(false);
this.setHorizontalScrollBarEnabled(false);
this.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}

bridgeHelper = new BridgeHelper(this);
this.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView webView, String s) {
bridgeHelper.onPageFinished();
}

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
return bridgeHelper.shouldOverrideUrlLoading(s);
}
});
}

/**
* @param handler default handler,handle messages send by js without assigned handler name,
* if js message has handler name, it will be handled by named handlers registered by native
*/
public void setDefaultHandler(BridgeHandler handler) {
bridgeHelper.setDefaultHandler(handler);
}

@Override
public void send(String data) {
send(data, null);
}

@Override
public void send(String data, CallBackFunction responseCallback) {
bridgeHelper.send(data, responseCallback);
}


/**
* register handler,so that javascript can call it
* 注册处理程序,以便javascript调用它
*
* @param handlerName handlerName
* @param handler BridgeHandler
*/
public void registerHandler(String handlerName, BridgeHandler handler) {
bridgeHelper.registerHandler(handlerName, handler);
}

/**
* unregister handler
*
* @param handlerName
*/
public void unregisterHandler(String handlerName) {
bridgeHelper.unregisterHandler(handlerName);
}

/**
* call javascript registered handler
* 调用javascript处理程序注册
*
* @param handlerName handlerName
* @param data data
* @param callBack CallBackFunction
*/
public void callHandler(String handlerName, String data, CallBackFunction callBack) {
bridgeHelper.callHandler(handlerName, data, callBack);
}

}
Loading

0 comments on commit 11999d5

Please sign in to comment.