All Projects → pengwei1024 → Jsbridge

pengwei1024 / Jsbridge

Licence: apache-2.0
A simpler, extendable bidirectional communication Frame between Android WebView and Javascript

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Jsbridge

Swiftwebvc
A drop-in inline browser for your Swift iOS app.
Stars: ✭ 307 (-49.84%)
Mutual labels:  webview
Android Smartwebview
A webview integrated w/ native features to help create most advanced hybrid applications.
Stars: ✭ 357 (-41.67%)
Mutual labels:  webview
Aachartcore
📈📊☕️☕️☕️An elegant modern declarative data visualization chart framework for Android. Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types.极其精美而又强大的 Android 数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图等各种类型的多达几十种的信息图图表,完全满足工作所需.
Stars: ✭ 424 (-30.72%)
Mutual labels:  webview
Android Inline Youtube View
Utility library around using YouTube inside your android app.
Stars: ✭ 313 (-48.86%)
Mutual labels:  webview
Customactionwebview
自定义webview长按文本弹出选项,并且点击后返回选项与所选中的文本,你的webview不再只支持系统的复制等功能了,长按web文本实现文本一键收藏、分享,就是这么简单。Demo中附带对webView的详细使用:api详解,配置详解,js多种通信方式详解。
Stars: ✭ 338 (-44.77%)
Mutual labels:  webview
Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (-38.89%)
Mutual labels:  webview
Sketch Module Web View
A sketch module for creating an complex UI with a webview
Stars: ✭ 270 (-55.88%)
Mutual labels:  webview
Cloudreader
🗡️ 云阅:一款基于网易云音乐UI,使用玩Android Api,Retrofit2 + RxJava2 + Room + MVVM-databinding架构开发的Android客户端
Stars: ✭ 4,611 (+653.43%)
Mutual labels:  webview
Electron Tabs
Simple tabs for Electron applications
Stars: ✭ 350 (-42.81%)
Mutual labels:  webview
Cordova Plugin Ionic Webview
Web View plugin for Cordova, specialized for Ionic apps.
Stars: ✭ 419 (-31.54%)
Mutual labels:  webview
Injuredandroid
A vulnerable Android application that shows simple examples of vulnerabilities in a ctf style.
Stars: ✭ 317 (-48.2%)
Mutual labels:  webview
Aachartcore Kotlin
📈📊⛰⛰⛰An elegant modern declarative data visualization chart framework for Android . Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types.极其精美而又强大的 Android 数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图等各种类型的多达几十种的信息图图表,完全满足工作所需.
Stars: ✭ 332 (-45.75%)
Mutual labels:  webview
React Native Autoheight Webview
An auto height webview for React Native
Stars: ✭ 385 (-37.09%)
Mutual labels:  webview
Jsqwebviewcontroller
[Deprecated] A lightweight Swift WebKit view controller for iOS
Stars: ✭ 307 (-49.84%)
Mutual labels:  webview
Smartyoutubetv
Watch YouTube videos on your TV and set-top-box with comfort
Stars: ✭ 4,384 (+616.34%)
Mutual labels:  webview
Crazydaily
[开源项目] 一款程序员日常放松的App,基于Material Design + MVP-Clean + Weex + Flutter + RxJava2 + Retrofit + Dagger2 + Glide + Okhttp + MTRVA + BRVAH + 炫酷控件 + 炫酷动画
Stars: ✭ 294 (-51.96%)
Mutual labels:  webview
Youtube player flutter
Flutter plugin for playing or streaming YouTube videos inline using the official iFrame Player API. Supports both Android and iOS platforms.
Stars: ✭ 366 (-40.2%)
Mutual labels:  webview
Android Reports And Resources
A big list of Android Hackerone disclosed reports and other resources.
Stars: ✭ 590 (-3.59%)
Mutual labels:  webview
Aachartkit
📈📊🚀🚀🚀An elegant modern declarative data visualization chart framework for iOS, iPadOS and macOS. Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types. 极其精美而又强大的跨平台数据可视化图表框架,支持柱状图、条形图、折…
Stars: ✭ 4,358 (+612.09%)
Mutual labels:  webview
Imgresize
移动端H5图片压缩
Stars: ✭ 391 (-36.11%)
Mutual labels:  webview

English | 简体中文

JsBridge

A simpler, extendable bidirectional communication Frame between Android WebView and Javascript

Features

  • supports parsing and callback for JS primitive types
  • Modular management
  • support System WebView and Custom WebView
  • permission authentication is implemented by Native, JS do not need to depend other file
  • support Android API 8+, avoid addJavascriptInterface Vulnerability
  • compatible with iOS WebViewJavascriptBridge

Getting Started

Download the latest JAR or Gradle:

compile 'com.apkfuns.jsbridge:jsbridge:2.1.1'

The library dependen on support-annotations, if your project already exists, please exclude

compile('com.apkfuns.jsbridge:jsbridge:2.1.1') {
    exclude module: 'support-annotations'
}

Examples

We use JS to call the original module to achieve ajax cross-domain request to briefly introduce the use of the library

1.Create Module

Create a module that needs to inherit JsModule and implement thegetModuleNamemethod, the module naming request is the same as the java variable naming, must not be empty, only allow underline(_) LettersNumber, if is static Module (not contain module name),need to inherit JsStaticModule, the following creates a Native module

public class NativeModule extends JsModule {
    @Override
    public String getModuleName() {
        return "native";
    }
}

2.Create Native Method

Module inside the creation method requires the use of annotations @ JSBridgeMethod, by default Java method name is JS call method name, also specify the name of the calling method by @JSBridgeMethod (methodName = "xx")。Method can not be static orabstract, method can contain the return type, if the return type is the object, default return string to JS, the method parameters for the following types, you can directly map to their corresponding JS type

Java Types Mapping the JS type
Boolean / boolean Bool
Integer/ int Number
Float / float Number
Double / double Number
Long / long Number
String String
JBCallback function
JBMap Object
JBArray Array

for more convenient to call, we define the method parameters as ajax, we look at the ajax request structure

$.ajax({
    type:'GET',
    url:'xxx.com',
    dataType:'text'
    data:{a:1, b:'xx'},
    success:function(data){
    },
    error:function(err){
    }
})

Ajax method parameter is a JS object, the object contains type, url, dataType three string parameters, data parameter is an object, success and error is JS callback method, let's define the Java method.

@JSBridgeMethod
public void ajax(JBMap dataMap) {
        String type = dataMap.getString("type");
        String url = dataMap.getString("url");
        JBMap data = dataMap.getJBMap("data");
        JBCallback successCallback = dataMap.getCallback("success");
        JBCallback errorCallback = dataMap.getCallback("error");
        // Omit the request code
        if (request success) {
              successCallback.apply("success");
        } else {
              errorCallback.apply("failure");
        }
}

must add annotations @JSBridgeMethod,the parameter is JBMap

JBCallback.apply callback JS callback method,variable parameter,support for Java basic types,Array (WritableJBArray),and Object(WritableJBMap), if for other objects, the default converted to string

3.Register Module

There are two ways to register a Module, Default registration, Dynamic registration

JsBridgeConfig.getSetting().registerDefaultModule(NativeModule.class);

// or

JsBridge.loadModule(NativeModule.class)

JsBridgeConfig parameter:

Method Type Description Default
setProtocol string The name of the object that JS calls JsBridge
setLoadReadyMethod string Load the completion of the callback function onJsBridgeReady
registerDefaultModule JsModule Common module, default load None
debugMode bool In debug mode, the output TAG is the JsBridgeDebug log false

4.WebView inject method & listen callback

public class WebViewActivity extends BaseActivity {
   private JsBridge jsBridge;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
         ...
       jsBridge = JsBridge.loadModule();
       ...
       webView.setWebChromeClient(new WebChromeClient() {
           @Override
           public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
               // listen callback
               jsBridge.callJsPrompt(message, result);
               return true;
           }
       });

       webView.setWebViewClient(new WebViewClient() {
           @Override
           public void onPageFinished(WebView view, String url) {
               super.onPageFinished(view, url);
               // inject JS
               jsBridge.injectJs(view);
           }
       });
   }

   @Override
   protected void onDestroy() {
       // Avoid memory leaks
       jsBridge.release();
       super.onDestroy();
   }
}

Now, in JS code can call this method:

JsBridge.native.ajax({
    type:'GET',
    url:'xxx.com',
    dataType:'text'
    data:{a:1, b:'xx'},
    success:function(data){
    },
    error:function(err){
    }
})

If it is like calling JsBridge.ajax({...}), change the parent class from JsModule to JsStaticModule

There are some important information, because JS execution is asynchronous, in order to ensure that injection JS has been completed, please implement the method in the callback, or judge JsBridge object exists

window.onJsBridgeReady = function () {
    JsBridge.native.ajax({...});
}

// or
document.addEventListener('onJsBridgeReady', function(){
    JsBridge.native.ajax({...});
})

// or
if (JsBridge) {
    JsBridge.native.ajax({...});
}

For documentation and additional information see wiki and sample

Proguard

-keep class com.apkfuns.jsbridge.**{*;}
-keep class * extends com.apkfuns.jsbridge.module.JsModule{*;}

License

Copyright pengwei1024

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].