Skip to content

Commit

Permalink
Merge pull request #270 from thorjay/fixcustomview
Browse files Browse the repository at this point in the history
解决CustomWebview当前demo无法使用的问题
  • Loading branch information
uknownothingsnow authored May 13, 2024
2 parents 50ed361 + 09312e0 commit 124af30
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.github.lzyzsd.jsbridge.IWebView;
import com.github.lzyzsd.jsbridge.OnBridgeCallback;
import com.github.lzyzsd.jsbridge.WebViewJavascriptBridge;
import com.google.gson.Gson;

import java.util.Map;

/**
* 采用BridgeHelper集成JsBridge功能示例.定制WebView,可只添加实际需要的JsBridge接口.
Expand All @@ -24,6 +27,8 @@ public class CustomWebView extends WebView implements WebViewJavascriptBridge, I

private BridgeHelper bridgeHelper;

private Gson mGson;

public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
Expand Down Expand Up @@ -115,4 +120,22 @@ public void sendToWeb(String data, OnBridgeCallback responseCallback) {
public void sendToWeb(String function, Object... values) {
bridgeHelper.sendToWeb(function, values);
}

@Override
public void responseFromWeb(String data, String callbackId) {
bridgeHelper.responseFromWeb(data,callbackId);
}

public void setGson(Gson gson){
this.mGson = gson;
}

public Map<String, OnBridgeCallback> getCallbacks() {
return bridgeHelper.getCallbacks();
}

@Override
public WebView getWebView() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.OnBridgeCallback;
import com.github.lzyzsd.jsbridge.WebViewJavascriptBridge;

import java.util.Map;

Expand All @@ -15,9 +16,10 @@
*/
public class MainJavascriptInterface extends BridgeWebView.BaseJavascriptInterface {

private BridgeWebView mWebView;
//WebJSbridge
private WebViewJavascriptBridge mWebView;

public MainJavascriptInterface(Map<String, OnBridgeCallback> callbacks, BridgeWebView webView) {
public MainJavascriptInterface(Map<String, OnBridgeCallback> callbacks, WebViewJavascriptBridge webView) {
super(callbacks);
mWebView = webView;
}
Expand All @@ -35,6 +37,6 @@ public String send(String data) {
@JavascriptInterface
public void submitFromWeb(String data, String callbackId) {
Log.d("MainJavascriptInterface", data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
mWebView.sendResponse("submitFromWeb response", callbackId);
mWebView.responseFromWeb("submitFromWeb response", callbackId);
}
}
1 change: 1 addition & 0 deletions library/src/main/assets/WebViewJavascriptBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
WebViewJavascriptBridge.registerHandler = registerHandler;
WebViewJavascriptBridge.callHandler = callHandler;
WebViewJavascriptBridge._handleMessageFromNative = _handleMessageFromNative;
WebViewJavascriptBridge._fetchQueue = _fetchQueue;

var readyEvent = document.createEvent('Events');
var jobs = window.WVJBCallbacks || [];
Expand Down
41 changes: 34 additions & 7 deletions library/src/main/java/com/github/lzyzsd/jsbridge/BridgeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -113,12 +116,8 @@ private void queueMessage(Message m) {
private void dispatchMessage(Message m) {
String messageJson = m.toJson();
//escape special characters for json string 为json字符串转义特殊字符
messageJson = messageJson.replaceAll("(\\\\)([^utrn])", "\\\\\\\\$1$2");
messageJson = messageJson.replaceAll("(?<=[^\\\\])(\")", "\\\\\"");
messageJson = messageJson.replaceAll("(?<=[^\\\\])(\')", "\\\\\'");
messageJson = messageJson.replaceAll("%7B", URLEncoder.encode("%7B"));
messageJson = messageJson.replaceAll("%7D", URLEncoder.encode("%7D"));
messageJson = messageJson.replaceAll("%22", URLEncoder.encode("%22"));
//系统原生API做Json转义替换手动转义,解决js解析数据格式报错
messageJson = JSONObject.quote(messageJson);
String javascriptCommand = String.format(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson);
// 必须要找主线程才会将数据传递出去 --- 划重点
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
Expand Down Expand Up @@ -294,4 +293,32 @@ public void sendToWeb(String function, Object... values) {
loadUrl(jsCommand);
}
}

public void sendResponse(Object data, String callbackId) {
if (!TextUtils.isEmpty(callbackId)) {
final Message response = new Message();
response.responseId = callbackId;
response.responseData = data instanceof String ? (String) data : new Gson().toJson(data);
if (Thread.currentThread() == Looper.getMainLooper().getThread()){
dispatchMessage(response);
}else {
webView.getWebView().post(new Runnable() {
@Override
public void run() {
dispatchMessage(response);
}
});
}
}
}

@Override
public void responseFromWeb(String data, String callbackId) {
sendResponse(data,callbackId);
}

public Map<String, OnBridgeCallback> getCallbacks() {
return responseCallbacks;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public void sendToWeb(String function, Object... values) {
}
}

@Override
public void responseFromWeb(String data, String callbackId) {
sendResponse(data,callbackId);
}

/**
* 保存message到消息队列
*
Expand Down Expand Up @@ -222,7 +227,6 @@ public void run() {
}
});
}

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.lzyzsd.jsbridge;

import android.content.Context;
import android.webkit.WebView;

/**
* WebView功能接口.
Expand All @@ -13,4 +14,9 @@ public interface IWebView {
Context getContext();

void loadUrl(String url);

/**
* 获取当前Webview
*/
WebView getWebView();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ public interface WebViewJavascriptBridge {

void sendToWeb(String function, Object... values);

/**
* 处理从js返回的数据
* @param data 数据
* @param callbackId jsCallbackId
*/
void responseFromWeb(String data,String callbackId);

}

0 comments on commit 124af30

Please sign in to comment.