WebView - JS bridge scaffold
- Create a communication bridge through WebSocket to realize message passing, and no longer rely on the JavascriptInterface of the system
- Optional Tencent X5 tbs
- One line code to load js
implementation("io.github.clistery:webbasic:1.1.0")Configuration (requires operation when Application's onCreate)
Debug switch (Will affect log output, packet capture, debugging and other functions)
WebBasicSettings.DEBUG=true
Modify bridge port
WebBasicSettings.BridgeConfig.bridgePort =23231
X5 switch
WebBasicSettings.X5LoaderConfig.enable =false
X5 core initialization delay
WebBasicSettings.X5LoaderConfig.loadDelay =10000L
UA
WebBasicSettings.WebViewLoader.UA="Mozilla/5.0 (Linux; ${Build.VERSION.SDK_INT}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Mobile Safari/537.36"
Resource loading URL domain name
WebBasicSettings.WebViewLoader.DOMAIN="assets.demo"
Define the URL path to access assets resources
WebBasicSettings.WebViewLoader.WEB_ASSETS_PATH="assets"
Define the URL path to access res resources
WebBasicSettings.WebViewLoader.WEB_ASSETS_PATH="res"
Define the URL path to access internal storage
WebBasicSettings.WebViewLoader.WEB_ASSETS_PATH="internal_storage"
Create Bridge WebView
val web =X5BridgeWebView(context)
Send data to JS
Send to the default receiver
val dataJson =JSONObject() dataJson.put("user_name", "CListery") dataJson.put("country", "CN") web.sendToJSReceiver(data = dataJson, callback =object:BridgeJSCallbackWithOutResp() { overridefunonCallWithOutResp(data:Any?) { logD("$data") } })
Send to the specified receiver
web.sendToJSReceiver("functionInJs", JSONObject().put("data", "data from Native"), object:BridgeJSCallbackWithOutResp() { overridefunonCallWithOutResp(data:Any?) { logD("reponse data from js: $data") } })
Receive data sent by JS
Register the default receiver
web.registerDefaultJSCallback(object:IBridgeJSCallback { overridefunonCall(data:Any?): JSONObject? { logD("default: ${JSONObject(data.toString())}") returnJSONObject().put("data", "default response") } })
Register specified name receiver
web.registerJSCallback("submitFromWeb", object:IBridgeJSCallback { overridefunonCall(data:Any?): JSONObject? { logD("submitFromWeb: ${JSONObject(data.toString())}") returnJSONObject().put("data", "ok!") } })
Register a one-time receiver
web.onceJSCallback("once_hello", object:IBridgeJSCallback { overridefunonCall(data:Any?): JSONObject? { logD("once_hello: $data") returnJSONObject().put("data", "once ok!") } })
Load the js file under assets
web.loadAssetsJS("demo.js")
monitor bridge status
functionconnectWebBridge(callback){console.log("connectWebBridge");if(window.Webridge&&window.Webridge.inited){callback(Webridge);}else{document.addEventListener("BridgeReady",function(){callback(Webridge);},false);}}connectWebBridge(function(bridge){console.log("connectWebBridge");bridge.callNative("once_hello",{data: "你好"},function(responseData){console.log(JSON.stringify(responseData));});});
call native callback
Call the callback with the specified name
window.Webridge.callNative("once_hello",{data: "你好"},function(responseData){console.log("repsonseData from native, data = "+JSON.stringify(responseData));});
Send message to native
Webridge.sendToNative(data,function(responseData){console.log("repsonseData from native, data = "+JSON.stringify(responseData););});
Register receiver
Default receiver
window.Webridge.registerDefaultReceiver(function(data,callback){console.log("[receiver] default: ",data);if(callback){callback("js response hi~");}});
Register a receiver with the specified name
window.Webridge.registerReceiver("functionInJs",function(data,callback){console.log("[receiver] functionInJs: ",data);document.getElementById("show").innerHTML=Date.now()+"\nreceive native data: = "+JSON.stringify(data);if(callback){callback("data received!");}});
Some sites may have restrictions due to js scripting capabilities
web.vConsole()
web.eruda()
web.chii()
web.captureByJS { base64ImgData -> }