diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml new file mode 100644 index 0000000..2884129 --- /dev/null +++ b/.github/workflows/android.yml @@ -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 diff --git a/library/build.gradle b/library/build.gradle index 135b2c3..500ecf2 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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()) diff --git a/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java b/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java index 480aac7..dee8584 100644 --- a/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java +++ b/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java @@ -8,7 +8,20 @@ import java.io.InputStream; import java.io.InputStreamReader; -class BridgeUtil { + +public class BridgeUtil { + final static String YY_OVERRIDE_SCHEMA = "yy://"; + 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 = "_"; diff --git a/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java b/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java index a72157a..4ec4f77 100644 --- a/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java +++ b/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java @@ -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; @@ -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()) {