├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ ├── activity_js_call_native.xml │ │ │ │ ├── activity_wrok_with_flyio_test.xml │ │ │ │ ├── activity_main.xml │ │ │ │ └── activity_call_javascript.xml │ │ ├── java │ │ │ └── wendu │ │ │ │ └── jsbdemo │ │ │ │ ├── JsEchoApi.java │ │ │ │ ├── JavascriptCallNativeActivity.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── WrokWithFlyioTestActivity.java │ │ │ │ ├── JsApi.java │ │ │ │ ├── NetUtils.java │ │ │ │ ├── AjaxHandler.java │ │ │ │ └── CallJavascriptActivity.java │ │ ├── AndroidManifest.xml │ │ └── assets │ │ │ ├── native-call-js.html │ │ │ ├── fly.html │ │ │ ├── js-call-native.html │ │ │ └── dsbridge.js │ ├── test │ │ └── java │ │ │ └── wendu │ │ │ └── jsbdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── wendu │ │ └── jsbdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── dsbridge ├── .gitignore ├── npm │ ├── .npmignore │ ├── test.ts │ ├── index.d.ts │ ├── package.json │ ├── dist │ │ └── dsbridge.js │ └── index.js ├── src │ └── main │ │ ├── java │ │ └── wendu │ │ │ └── dsbridge │ │ │ ├── OnReturnValue.java │ │ │ ├── CompletionHandler.java │ │ │ └── DWebView.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── .idea ├── vcs.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml └── misc.xml ├── .travis.yml ├── gradle.properties ├── changelist.md ├── gradlew.bat ├── gradlew ├── readme-chs.md └── readme.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /dsbridge/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /dsbridge/npm/.npmignore: -------------------------------------------------------------------------------- 1 | test.ts -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':dsbridge' 2 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DSBridge Demo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wendux/DSBridge-Android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /dsbridge/src/main/java/wendu/dsbridge/OnReturnValue.java: -------------------------------------------------------------------------------- 1 | package wendu.dsbridge; 2 | 3 | /** 4 | * Created by du on 16/12/31. 5 | */ 6 | 7 | public interface OnReturnValue { 8 | void onValue( T retValue); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3a4854 4 | #3a4854 5 | #e4e4e4 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Aug 19 10:25:16 CST 2017 2 | 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 8 | -------------------------------------------------------------------------------- /dsbridge/src/main/java/wendu/dsbridge/CompletionHandler.java: -------------------------------------------------------------------------------- 1 | package wendu.dsbridge; 2 | 3 | /** 4 | * Created by du on 16/12/31. 5 | */ 6 | 7 | public interface CompletionHandler { 8 | void complete(T retValue); 9 | void complete(); 10 | void setProgressData(T value); 11 | } 12 | -------------------------------------------------------------------------------- /dsbridge/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /dsbridge/npm/test.ts: -------------------------------------------------------------------------------- 1 | import bridge from "./index" 2 | bridge.call("test",function (retValue) { 3 | alert(retValue); 4 | }) 5 | var t=bridge.call("xx"); 6 | 7 | bridge.register("addVlue",function (l,r) { 8 | return l+r; 9 | }) 10 | 11 | bridge.register("echo",{ 12 | a:5 13 | }) 14 | 15 | bridge.registerAsyn("echo",{ 16 | b:6 17 | }) 18 | 19 | bridge.hasNativeMethod("test","asyn") 20 | 21 | bridge.disableJavascriptDialogBlock(true) -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/wendu/jsbdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/wendu/jsbdemo/JsEchoApi.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.webkit.JavascriptInterface; 4 | 5 | import org.json.JSONException; 6 | 7 | import wendu.dsbridge.CompletionHandler; 8 | 9 | /** 10 | * Created by du on 16/12/31. 11 | */ 12 | 13 | public class JsEchoApi { 14 | 15 | @JavascriptInterface 16 | public Object syn(Object args) throws JSONException { 17 | return args; 18 | } 19 | 20 | @JavascriptInterface 21 | public void asyn(Object args,CompletionHandler handler){ 22 | handler.complete(args); 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_js_call_native.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: oraclejdk8 3 | android: 4 | components: 5 | - platform-tools 6 | - tools 7 | 8 | # The BuildTools version used by your project 9 | - build-tools-25.0.0 10 | 11 | 12 | # The SDK version used to compile your project 13 | - android-23 14 | 15 | # Support library 16 | - extra-android-support 17 | - extra-android-m2repository 18 | - sys-img-x86_64-android-22 19 | licenses: 20 | - 'android-sdk-preview-license-52d11cd2' 21 | - 'android-sdk-license-.+' 22 | - 'google-gdk-license-.+' 23 | 24 | script: 25 | - ./gradlew build 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_wrok_with_flyio_test.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/du/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /dsbridge/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/du/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /dsbridge/npm/index.d.ts: -------------------------------------------------------------------------------- 1 | interface DSBridge { 2 | call (handlerName: string, args?: any, responseCallback?: (retValue: any) => void): any; 3 | call (handlerName: string, args?: T, responseCallback?: (retValue: R) => void): R; 4 | 5 | register (handlerName: string, handler: object | (() => any), async?: boolean): void; 6 | register (handlerName: string, handler: F, async?: boolean): void; 7 | 8 | registerAsyn (handlerName: string, handler: object | (() => void)): void; 9 | registerAsyn (handlerName: string, handler: F): void; 10 | 11 | hasNativeMethod (handlerName: string, type?: ('all' | 'asyn' | 'syn')): boolean; 12 | disableJavascriptDialogBlock (disable?: boolean): void; 13 | } 14 | 15 | declare const bridge: DSBridge; 16 | 17 | export default bridge; 18 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /dsbridge/npm/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dsbridge", 3 | "version": "3.1.4", 4 | "main": "index.js", 5 | "typings": "./index.d.ts", 6 | "author": { 7 | "name": "wendux", 8 | "email": "824783146@qq.com", 9 | "url": "https://github.com/wendux/" 10 | }, 11 | "license": "MIT", 12 | "description": "Javascript Initialization code of DSBridge ", 13 | "keywords": [ 14 | "DSBridge" 15 | ], 16 | "respository": { 17 | "type": "git", 18 | "url": "https://github.com/wendux/DSBridge-Android", 19 | "url2": "https://github.com/wendux/DSBridge-IOS" 20 | }, 21 | "homepage": "https://github.com/wendux", 22 | "bugs": { 23 | "url": "https://github.com/wendux/DSBridge-Android/issues", 24 | "url2": "https://github.com/wendux/DSBridge-IOS/issues" 25 | }, 26 | "devDependencies": { 27 | }, 28 | "dependencies": { 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/wendu/jsbdemo/JavascriptCallNativeActivity.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | import wendu.dsbridge.DWebView; 7 | 8 | public class JavascriptCallNativeActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_js_call_native); 14 | final DWebView dwebView= (DWebView) findViewById(R.id.webview); 15 | // set debug mode 16 | DWebView.setWebContentsDebuggingEnabled(true); 17 | dwebView.addJavascriptObject(new JsApi(), null); 18 | dwebView.addJavascriptObject(new JsEchoApi(),"echo"); 19 | dwebView.loadUrl("file:///android_asset/js-call-native.html"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/wendu/jsbdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("wendu.jsbdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /dsbridge/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion '25.0.0' 6 | 7 | defaultConfig { 8 | minSdkVersion 11 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | lintOptions { 23 | abortOnError false 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 30 | exclude group: 'com.android.support', module: 'support-annotations' 31 | }) 32 | compile 'com.android.support:appcompat-v7:23.4.0' 33 | testCompile 'junit:junit:4.12' 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion '25.0.0' 6 | defaultConfig { 7 | applicationId "wendu.jsbdemo" 8 | minSdkVersion 11 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | compile project(path: ':dsbridge') 31 | compile 'com.android.support:appcompat-v7:23.4.0' 32 | compile 'com.squareup.okhttp3:okhttp:3.3.0' 33 | testCompile 'junit:junit:4.12' 34 | } 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/java/wendu/jsbdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | findViewById(R.id.callJs).setOnClickListener(new View.OnClickListener() { 16 | @Override 17 | public void onClick(View v) { 18 | startActivity(new Intent(MainActivity.this,CallJavascriptActivity.class)); 19 | } 20 | }); 21 | findViewById(R.id.callNative).setOnClickListener(new View.OnClickListener() { 22 | @Override 23 | public void onClick(View v) { 24 | startActivity(new Intent(MainActivity.this,JavascriptCallNativeActivity.class)); 25 | } 26 | }); 27 | findViewById(R.id.fly).setOnClickListener(new View.OnClickListener() { 28 | @Override 29 | public void onClick(View v) { 30 | startActivity(new Intent(MainActivity.this,WrokWithFlyioTestActivity.class)); 31 | } 32 | }); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/wendu/jsbdemo/WrokWithFlyioTestActivity.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.webkit.JavascriptInterface; 6 | 7 | import org.json.JSONObject; 8 | 9 | import wendu.dsbridge.CompletionHandler; 10 | import wendu.dsbridge.DWebView; 11 | 12 | public class WrokWithFlyioTestActivity extends AppCompatActivity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_wrok_with_flyio_test); 18 | DWebView dWebView= (DWebView) findViewById(R.id.webview); 19 | dWebView.addJavascriptObject(new Object(){ 20 | 21 | /** 22 | * Note: This method is for Fly.js 23 | * In browser, Ajax requests are sent by browser, but Fly can 24 | * redirect requests to native, more about Fly see https://github.com/wendux/fly 25 | * @param requestData passed by fly.js, more detail reference https://wendux.github.io/dist/#/doc/flyio-en/native 26 | * @param handler 27 | */ 28 | @JavascriptInterface 29 | public void onAjaxRequest(Object requestData, CompletionHandler handler){ 30 | // Handle ajax request redirected by Fly 31 | AjaxHandler.onAjaxRequest((JSONObject)requestData,handler); 32 | } 33 | 34 | },null); 35 | 36 | dWebView.loadUrl("file:///android_asset/fly.html"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /dsbridge/npm/dist/dsbridge.js: -------------------------------------------------------------------------------- 1 | var bridge={default:this,call:function(b,a,c){var e="";"function"==typeof a&&(c=a,a={});a={data:void 0===a?null:a};if("function"==typeof c){var g="dscb"+window.dscb++;window[g]=c;a._dscbstub=g}a=JSON.stringify(a);if(window._dsbridge)e=_dsbridge.call(b,a);else if(window._dswk||-1!=navigator.userAgent.indexOf("_dsbridge"))e=prompt("_dsbridge="+b,a);return JSON.parse(e||"{}").data},register:function(b,a,c){c=c?window._dsaf:window._dsf;window._dsInit||(window._dsInit=!0,setTimeout(function(){bridge.call("_dsb.dsinit")}, 2 | 0));"object"==typeof a?c._obs[b]=a:c[b]=a},registerAsyn:function(b,a){this.register(b,a,!0)},hasNativeMethod:function(b,a){return this.call("_dsb.hasNativeMethod",{name:b,type:a||"all"})},disableJavascriptDialogBlock:function(b){this.call("_dsb.disableJavascriptDialogBlock",{disable:!1!==b})}}; 3 | !function(){if(!window._dsf){var b={_dsf:{_obs:{}},_dsaf:{_obs:{}},dscb:0,dsBridge:bridge,close:function(){bridge.call("_dsb.closePage")},_handleMessageFromNative:function(a){var e=JSON.parse(a.data),b={id:a.callbackId,complete:!0},c=this._dsf[a.method],d=this._dsaf[a.method],h=function(a,c){b.data=a.apply(c,e);bridge.call("_dsb.returnValue",b)},k=function(a,c){e.push(function(a,c){b.data=a;b.complete=!1!==c;bridge.call("_dsb.returnValue",b)});a.apply(c,e)};if(c)h(c,this._dsf);else if(d)k(d,this._dsaf); 4 | else if(c=a.method.split("."),!(2>c.length)){a=c.pop();var c=c.join("."),d=this._dsf._obs,d=d[c]||{},f=d[a];f&&"function"==typeof f?h(f,d):(d=this._dsaf._obs,d=d[c]||{},(f=d[a])&&"function"==typeof f&&k(f,d))}}},a;for(a in b)window[a]=b[a];bridge.register("_hasJavascriptMethod",function(a,b){b=a.split(".");if(2>b.length)return!(!_dsf[b]&&!_dsaf[b]);a=b.pop();b=b.join(".");return(b=_dsf._obs[b]||_dsaf._obs[b])&&!!b[a]})}}(); -------------------------------------------------------------------------------- /app/src/main/java/wendu/jsbdemo/JsApi.java: -------------------------------------------------------------------------------- 1 | package wendu.jsbdemo; 2 | 3 | import android.os.CountDownTimer; 4 | import android.webkit.JavascriptInterface; 5 | 6 | import org.json.JSONException; 7 | import org.json.JSONObject; 8 | 9 | import wendu.dsbridge.CompletionHandler; 10 | 11 | /** 12 | * Created by du on 16/12/31. 13 | */ 14 | 15 | public class JsApi{ 16 | @JavascriptInterface 17 | public String testSyn(Object msg) { 18 | return msg + "[syn call]"; 19 | } 20 | 21 | @JavascriptInterface 22 | public void testAsyn(Object msg, CompletionHandler handler){ 23 | handler.complete(msg+" [ asyn call]"); 24 | } 25 | 26 | @JavascriptInterface 27 | public String testNoArgSyn(Object arg) throws JSONException { 28 | return "testNoArgSyn called [ syn call]"; 29 | } 30 | 31 | @JavascriptInterface 32 | public void testNoArgAsyn(Object arg,CompletionHandler handler) { 33 | handler.complete( "testNoArgAsyn called [ asyn call]"); 34 | } 35 | 36 | 37 | //@JavascriptInterface 38 | //without @JavascriptInterface annotation can't be called 39 | public String testNever(Object arg) throws JSONException { 40 | JSONObject jsonObject= (JSONObject) arg; 41 | return jsonObject.getString("msg") + "[ never call]"; 42 | } 43 | 44 | @JavascriptInterface 45 | public void callProgress(Object args, final CompletionHandler handler) { 46 | 47 | new CountDownTimer(11000, 1000) { 48 | int i=10; 49 | @Override 50 | public void onTick(long millisUntilFinished) { 51 | //setProgressData can be called many times util complete be called. 52 | handler.setProgressData((i--)); 53 | 54 | } 55 | @Override 56 | public void onFinish() { 57 | //complete the js invocation with data; handler will be invalid when complete is called 58 | handler.complete(0); 59 | 60 | } 61 | }.start(); 62 | } 63 | } -------------------------------------------------------------------------------- /app/src/main/assets/native-call-js.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DSBridge Test 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 31 | 32 | 33 | 34 | 35 | 36 | 38 | -------------------------------------------------------------------------------- /changelist.md: -------------------------------------------------------------------------------- 1 | # DSBridge v3.0 change list 2 | 3 | DSBridge v3.0 is a milestone, Compared with v2.0.X, we have made a lot of changes. Note that V3.0 is **incompatible** with V2.0, but v2.0 will continue to maintain. If you are a new user, use >=v3.0 4 | 5 | #### In Java 6 | 7 | 1. **Deprecated**:~~setJavascriptInterface~~ , use `addJavascriptObject` instead. 8 | 9 | 2. **Deprecated**:~~setJavascriptContextInitedListener~~ ,`callHandler` can be called at any time. 10 | 11 | 3. **Deprecated**:~~DUIWebView~~ , `UIWebView` will not be supported ever. 12 | 13 | 4. **New**: `addJavascriptObject:(id) object namespace:(NSString *) namespace` 14 | 15 | 5. **New**: `removeJavascriptObject:NSString * namespace` 16 | 17 | 6. **New**: `disableJavascriptDialogBlock:(bool) disable` 18 | 19 | 7. **New**: `hasJavascriptMethod:(NSString *) handlerName methodExistCallback:(void(^ )(bool exist))callback` 20 | 21 | 8. **New**: ` setJavascriptCloseWindowListener:(void(^)(void))callback` 22 | 23 | 9. **New**: `setDebugMode:(bool) debug` 24 | 25 | 10. **New feature**: Support namespace 26 | 27 | 11. **New feature**: Can add multiple API object 28 | 29 | 12. **Changed**: Object-c API signature changed 30 | 31 | 13. **Changed**: `callHandler` can be called at any time. 32 | 33 | ​ 34 | 35 | 36 | #### In Javascript 37 | 38 | 1. **New**: `hasNativeMethod(handlerName,[type])` 39 | 2. **New**: `disableJavascriptDialogBlock(disable)` 40 | 3. **New**: `registerAsyn(methodName|namespace,function|asyApiObject)` 41 | 4. **Changed**: `register(methodName|namespace,function|synApiObject)` 42 | 5. **New feature**: Support namespace 43 | 44 | # Why Only Support WKWebView? 45 | 46 | ### Advantages of WKWebView 47 | 48 | It is well known that **WKWebView loads web pages faster and more efficiently than UIWebView**, and also **doesn't have as much memory overhead** for you. 49 | 50 | Under the current timeline, most iOS apps only support iOS 9.0+. 51 | 52 | ### UIWebView Cross-Domain Access Vulnerability 53 | 54 | The reason for the iOS platform cross-domain access vulnerability is due to UIWebView turning on the WebKitAllowUniversalAccessFromFileURLs and WebKitAllowFileAccessFromFileURLs options. 55 | 56 | **WKWebView default allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs option is false.** -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 14 | 20 | 28 |