All Projects → luffyjet → WebViewJavaScriptBridge

luffyjet / WebViewJavaScriptBridge

Licence: Apache-2.0 License
android webview javascript bridge

Programming Languages

java
68154 projects - #9 most used programming language
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to WebViewJavaScriptBridge

Android-Web-Inspector
How to Inspecting Android WebView, Network logs, XHR logs (including url request and parameter) and Element/DOM inspecting
Stars: ✭ 54 (+125%)
Mutual labels:  webview, webviewjavascriptbridge
X5Bridge
Three party libraries of Tencent x5webview and JS interaction
Stars: ✭ 17 (-29.17%)
Mutual labels:  webview, webviewjavascriptbridge
webview flutter plus
An extension of webview_flutter to load HTML,CSS and Javascript even from Assets or Strings.
Stars: ✭ 45 (+87.5%)
Mutual labels:  webview
webview
Nim bindings for https://github.com/zserge/webview
Stars: ✭ 91 (+279.17%)
Mutual labels:  webview
frida-uiwebview
Inspect and manipulate UIWebView-hosted GUIs through Frida.
Stars: ✭ 36 (+50%)
Mutual labels:  webview
react-native-codeditor
React Native component to display code editor using WebView and CodeMirror
Stars: ✭ 21 (-12.5%)
Mutual labels:  webview
flutter-webview-windows
A WebView2-powered Flutter WebView implementation for the Windows platform.
Stars: ✭ 83 (+245.83%)
Mutual labels:  webview
topframe
Local webpage screen overlay for customizing your computing experience
Stars: ✭ 321 (+1237.5%)
Mutual labels:  webview
msLog
log for webView & webApp 用于webView和webApp的log工具
Stars: ✭ 25 (+4.17%)
Mutual labels:  webview
WeBer
Android x5 内核 WebView 的 Helper 完美兼容 AndroidX 和 android 库,欢迎使用~~~
Stars: ✭ 20 (-16.67%)
Mutual labels:  webview
appWeiboInfoCrawl
use webview let user to login Weibo,and the auto get user info(使用webview让用户授权登录微博,然后自动获取用户信息)
Stars: ✭ 27 (+12.5%)
Mutual labels:  webview
CefGlue
.NET binding for The Chromium Embedded Framework (CEF)
Stars: ✭ 44 (+83.33%)
Mutual labels:  webview
AnyWebView
Any WebView is OK!
Stars: ✭ 64 (+166.67%)
Mutual labels:  webview
rn-webview-bridge-sample
React Native WebView bridge sample
Stars: ✭ 40 (+66.67%)
Mutual labels:  webview
FastWebView
自定义本地缓存策略和资源加载策略,突破原生WebView缓存限制,实现多种缓存模式,支持离线加载和预加载,可大幅提升加载速度。
Stars: ✭ 211 (+779.17%)
Mutual labels:  webview
webviewhs
🌐 A Haskell binding to the webview library created by Serge Zaitsev.
Stars: ✭ 109 (+354.17%)
Mutual labels:  webview
react-native-fblogin
📦 A React Native 'Facebook Login' component without wrapping any Facebook Native/Web SDK
Stars: ✭ 19 (-20.83%)
Mutual labels:  webview
react-native-web-view
An implementation of React Native's WebView that allows for postMessage on iOS devices.
Stars: ✭ 13 (-45.83%)
Mutual labels:  webview
react-native-react-bridge
An easy way to integrate your React (or Preact) app into React Native app with WebView.
Stars: ✭ 84 (+250%)
Mutual labels:  webview
RxWebView
RxJava2 binding APIs for Android's WebView
Stars: ✭ 22 (-8.33%)
Mutual labels:  webview

WebViewJavascriptBridge

根据IOS marcuswestin/WebViewJavascriptBridge 编写而来的JavascriptBridge。

相比同类库的优点:

  1. IOS marcuswestin/WebViewJavascriptBridge 一样的使用方法,可共用一套JS代码。
  2. 同时也在此之上做了加强,参考了Cordova源码的模块管理,方便把各种不同的原生功能封装成独立的模块并统一管理。具体请看下面的 模块管理功能一栏。

规定JS和Java之间用标准JSON格式字符串交互,JS传给Java的数据会封装成 org.json.JSONObject。

(An Android bridge for sending messages between Java and JavaScript in WebViews. Based on IOS marcuswestin/WebViewJavascriptBridge.)

Gradle

repositories {
    jcenter()
}

compile 'com.luffyjet:webviewjavascriptbridge:1.0'

Examples

See the app/ folder.

Usage

  1. Init WebViewJavaScriptBridge
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);//很关键
WebViewJavaScriptBridge mBridge = WebViewJavaScriptBridge.bridgeForWebView(this, webView);
mBridge.setWebViewDelegate(new MyWebViewClient());//设置WebViewClient
webView.setWebChromeClient(new MyChromeClient());//设置ChromeClient
  1. Register a handler in Java, and call a JS handler:
 //注册一个 处理 js端发来消息的 handler
mBridge.registerHandler("abs", new WebViewJavaScriptBridgeBase.WVJBHandler() {
    @Override
    public void handle(JSONObject data, WebViewJavaScriptBridgeBase.WVJBResponseCallback responseCallback) {
        Log.d(TAG, "from JS req: " + data.toString());
        responseCallback.callback(new JSResult("i like milk from native").toJson());
    }
});

mBridge.callHandler("NativeCallJS", model.toJSON(), new WebViewJavaScriptBridgeBase.WVJBResponseCallback() {
    @Override
    public void callback(String responseData) {
        Log.d(TAG, "JS responded:" + responseData);
        Toast.makeText(MainActivity.this, "JS responded:" + responseData , Toast.LENGTH_SHORT).show();
    }
});
        
  1. Copy and paste setupWebViewJavascriptBridge into your JS:
function setupWebViewJavascriptBridge(callback) {
	if(window.WebViewJavascriptBridge) {
		return callback(WebViewJavascriptBridge);
	}
	if(window.WVJBCallbacks) {
		return window.WVJBCallbacks.push(callback);
	}
	window.WVJBCallbacks = [callback];
	var WVJBIframe = document.createElement('iframe');
	WVJBIframe.style.display = 'none';
	//这里最新IOS版是 https的scheme,真实环境下 需要判断iOS和Android,做下区分。
	WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
	document.documentElement.appendChild(WVJBIframe);
	setTimeout(function() {
		document.documentElement.removeChild(WVJBIframe)
	}, 0);
}
  1. Finally, call setupWebViewJavascriptBridge and then use the bridge to register handlers and call Java handlers:
setupWebViewJavascriptBridge(function(bridge) {
	/* Initialize your app here */

	bridge.registerHandler('NativeCallJS', function(data, responseCallback) {
		var responseData = {
			'Javascript Says': 'Right back atcha!'
		};

		log('Native call JS with ', data);
		responseCallback(responseData);
	});

	var doc = document;
	var readyEvent = doc.createEvent('Events');
	readyEvent.initEvent('WebViewJavascriptBridgeReady');
	readyEvent.bridge = WebViewJavascriptBridge;
	doc.dispatchEvent(readyEvent);
});

模块管理功能

可以和Cordova一样进行模块管理,同一种类型的消息(handler name相同)都由一个模块处理。 模块类需要继承 RequestHandler ,包含WebView的Activity要实现 BridgeInterface 接口。插件类由XML文件进行配置,请在项目中新建 res/xml/wjbconfig.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<widget>
    <feature name="chooseImage">
        <param
            name="android-package"
            value="com.luffyjet.jsbridgeexample.handlers.ImageChooseHandler"/>
        <param
            name="onload"
            value="true"/>
    </feature>

    <feature name="deviceInfo">
        <param
            name="android-package"
            value="com.luffyjet.jsbridgeexample.handlers.DeviceInfoHandler"/>
        <param
            name="onload"
            value="true"/>
    </feature>

</widget>


//一个简单的选图模块示例
public class ImageChooseHandler extends RequestHandler {
    private static final String TAG = "ImageChooseHandler";
    private static final int REQUEST_CODE_IMAGES = 100;
    private WebViewJavaScriptBridgeBase.WVJBResponseCallback mResponseCallback;

    @Override
    public void handle(JSONObject data, WebViewJavaScriptBridgeBase.WVJBResponseCallback responseCallback) {

        mResponseCallback = responseCallback;
        ...
        mBridgeInterface.startActivityForResult(this, Intent.createChooser(i, "选取图片"), REQUEST_CODE_IMAGES);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_IMAGES) {
            try {
                ...
                mResponseCallback.callback(result.toJSON());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


// 包含webview 的 WebActivity
public class WebActivity extends AppCompatActivity implements BridgeInterface{

    RequestHandler mRequestHandler;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null != mRequestHandler) {
            RequestHandler callback = mRequestHandler;
            mRequestHandler = null;
            callback.onActivityResult(requestCode, resultCode, data);
            return;
        }

        //other code
    }

    @Override
    public void startActivityForResult(RequestHandler command, Intent intent, int requestCode) {
        setActivityResultCallback(command);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void setActivityResultCallback(RequestHandler plugin) {
        mRequestHandler = plugin;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public ExecutorService getThreadPool() {
        return Executors.newCachedThreadPool(new ThreadFactory() {
            @Override
            public Thread newThread(final Runnable r) {
                return  new Thread(new Runnable() {
                    @Override
                    public void run() {
                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                        r.run();
                    }
                },"Test");
            }
        });
    }
}

一个功能模块对应一个feature,feature name就是handle name。 onload属性为true代表模块(插件)会在webview初始化时一同初始化,false则是在需要该模块(插件)的时候通过反射加载。

具体使用方法请查看 app/ 目录下的示例代码。

Thanks

marcuswestin/WebViewJavascriptBridge & lzyzsd/JsBridge & cordova-android

License

See the LICENSE file for license rights and limitations.

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].