Skip to content

Commit

Permalink
Merge pull request #134 from guofeng007/master
Browse files Browse the repository at this point in the history
对 json 的转义建议是用系统原生已有 api
  • Loading branch information
uknownothingsnow authored Jul 30, 2021
2 parents ce44f19 + 5c72d26 commit ce4d01c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 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
23 changes: 23 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}


def siteUrl = 'https://github.com/lzyzsd/JsBridge'
def gitUrl = 'https://github.com/lzyzsd/JsBridge.git'
// apply plugin: 'com.github.dcendents.android-maven'
group = "com.github.lzyzsd.jsbridge"
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
15 changes: 14 additions & 1 deletion library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@
import java.io.InputStream;
import java.io.InputStreamReader;

class BridgeUtil {

public class BridgeUtil {
final static String YY_OVERRIDE_SCHEMA = "yy://";

This comment has been minimized.

Copy link
@Jayin

Jayin Oct 30, 2021

这是多余的@guofeng007 @lzyzsd

This comment has been minimized.

Copy link
@Jayin

Jayin Oct 30, 2021

YY_XXXX

final static String YY_RETURN_DATA = YY_OVERRIDE_SCHEMA + "return/";//格式为 yy://return/{function}/returncontent
final static String YY_FETCH_QUEUE = YY_RETURN_DATA + "_fetchQueue/";
final static String EMPTY_STR = "";
final static String UNDERLINE_STR = "_";
final static String SPLIT_MARK = "/";

final static String CALLBACK_ID_FORMAT = "JAVA_CB_%s";
final static String JS_HANDLE_MESSAGE_FROM_JAVA = "javascript:WebViewJavascriptBridge._handleMessageFromNative(%s);";
final static String JS_FETCH_QUEUE_FROM_JAVA = "javascript:WebViewJavascriptBridge._fetchQueue();";
public final static String JAVASCRIPT_STR = "javascript:";


public static final String JAVA_SCRIPT = "WebViewJavascriptBridge.js";
public final static String UNDERLINE_STR = "_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import com.github.lzyzsd.library.BuildConfig;
import com.google.gson.Gson;


import org.json.JSONObject;

import java.net.URLEncoder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -189,12 +193,10 @@ private void dispatchMessage(Object message) {
}
String messageJson = mGson.toJson(message);
//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转义,没必要自己正则替换,而且替换不一定完整
messageJson = JSONObject.quote(messageJson);

String javascriptCommand = String.format(BridgeUtil.JS_HANDLE_MESSAGE_FROM_JAVA, messageJson);
// 必须要找主线程才会将数据传递出去 --- 划重点
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
Expand Down

0 comments on commit ce4d01c

Please sign in to comment.