Skip to content

Commit 7dc834f

Browse files
author
lemon
committed
更改jsbridge注入的时机,在页面加载25%以上的时候开始注入
1 parent a022e6a commit 7dc834f

File tree

5 files changed

+88
-13
lines changed

5 files changed

+88
-13
lines changed

app/src/main/assets/demo.html

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
4-
<meta charset="utf-8"/>
5+
<meta charset="utf-8" />
56
<meta http-equiv="X-UA-Compatible" content="IE=edge">
67
<title>Page Title</title>
78
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<script src="easybridge.js"></script>
810
<script type="text/javascript">
9-
10-
function toast(param='toast'){
11-
easyBridge.callHandler('toast',param)
11+
12+
document.addEventListener('WebViewJavascriptBridgeReady',
13+
function () {
14+
toast('inject finished');
15+
}, false);
16+
17+
function toast(params = 'toast data') {
18+
window['easyBridge'].callHandler('toast', 'toast with:'+params+'at '+new Date().toLocaleString());
1219
}
1320
</script>
1421
</head>
22+
1523
<body>
16-
<a href="javascript:toast()"><h3>toast通知</h3></a>
24+
25+
<a href="javascript:toast('click toast')">
26+
<h3>测试Toast</h3>
27+
</a>
28+
1729
</body>
30+
1831
</html>

app/src/main/java/easily/tech/easybridge/MainActivity.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package easily.tech.easybridge
33
import android.os.Build
44
import android.support.v7.app.AppCompatActivity
55
import android.os.Bundle
6+
import android.webkit.WebChromeClient
67
import android.webkit.WebView
78
import easily.tech.easybridge.handler.ToastHandler
9+
import easily.tech.easybridge.lib.EasyBridgeWebChromeClient
10+
import easily.tech.easybridge.lib.SecurityPolicyChecker
811
import kotlinx.android.synthetic.main.activity_main.*
912

1013
class MainActivity : AppCompatActivity() {
@@ -15,6 +18,16 @@ class MainActivity : AppCompatActivity() {
1518
WebView.setWebContentsDebuggingEnabled(true)
1619
}
1720
setContentView(R.layout.activity_main)
21+
webView.webChromeClient = object : EasyBridgeWebChromeClient(webView) {
22+
override fun onProgressChanged(view: WebView?, newProgress: Int) {
23+
super.onProgressChanged(view, newProgress)
24+
val script = String.format("%s.callHandler('toast','current progress:%s')", webView.bridgeName, newProgress)
25+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
26+
webView.evaluateJavascript(script) {
27+
}
28+
}
29+
}
30+
}
1831
webView.registerHandler(ToastHandler(this))
1932
webView.loadUrl("file:///android_asset/demo.html")
2033
}

app/src/main/java/easily/tech/easybridge/handler/ToastHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package easily.tech.easybridge.handler
22

33
import android.content.Context
4+
import android.util.Log
45
import android.widget.Toast
56
import easily.tech.easybridge.lib.ResultCallBack
67
import easily.tech.easybridge.lib.handler.BaseBridgeHandler
@@ -10,6 +11,7 @@ import easily.tech.easybridge.lib.handler.BaseBridgeHandler
1011
*/
1112
class ToastHandler(private val context: Context) : BaseBridgeHandler("toast") {
1213
override fun onCall(parameters: String?, callBack: ResultCallBack?) {
14+
Log.d("Toast",parameters)
1315
Toast.makeText(context, parameters, Toast.LENGTH_SHORT).show()
1416
callBack?.onResult(null)
1517
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package easily.tech.easybridge.lib;
2+
3+
import android.webkit.WebChromeClient;
4+
import android.webkit.WebView;
5+
6+
/**
7+
* Created by hzyangjiehao on 2018/4/2.
8+
*/
9+
public class EasyBridgeWebChromeClient extends WebChromeClient {
10+
11+
private static final String BRIDGE_SCRIPT_PATH = "easybridge.js";
12+
private static final String JAVA_SCRIPT_PROTOCOL = "javascript:";
13+
14+
15+
protected SecurityPolicyChecker securityPolicyChecker;
16+
17+
private boolean isInjected;
18+
private EasyBridgeWebView easyBridgeWebView;
19+
20+
public EasyBridgeWebChromeClient(EasyBridgeWebView webView) {
21+
this.easyBridgeWebView = webView;
22+
this.securityPolicyChecker = webView.getPolicyChecker();
23+
}
24+
25+
@Override
26+
public void onProgressChanged(WebView view, int newProgress) {
27+
super.onProgressChanged(view, newProgress);
28+
// inject the bridge code when the progress above 25% to make sure it worked
29+
if (newProgress <= 25) {
30+
isInjected = false;
31+
} else if (!isInjected) {
32+
if (securityPolicyChecker == null || securityPolicyChecker.check(view.getUrl(), "")) {
33+
isInjected = true;
34+
String bridgeScript = Utils.readAssetFile(view.getContext(), BRIDGE_SCRIPT_PATH);
35+
String executeScript = "var bridgeName = " + "\"" + easyBridgeWebView.getBridgeName() + "\"" + ";" + bridgeScript;
36+
view.loadUrl(String.format("%s%s", JAVA_SCRIPT_PROTOCOL, executeScript));
37+
}
38+
}
39+
}
40+
}

easybridge/src/main/java/easily/tech/easybridge/lib/EasyBridgeWebView.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class EasyBridgeWebView extends WebView {
1818
private static final String DEFAULT_BRIDGE_NAME = "easyBridge";
1919
private final EasyBridge easyBridge;
2020
private String bridgeName = DEFAULT_BRIDGE_NAME;
21+
protected SecurityPolicyChecker policyChecker;
2122

2223
public EasyBridgeWebView(Context context, String bridgeName) {
2324
this(context, (AttributeSet) null);
@@ -45,14 +46,8 @@ private void initWebView() {
4546
webSettings.setJavaScriptEnabled(true);
4647
}
4748
addJavascriptInterface(easyBridge, MAPPING_JS_INTERFACE_NAME);
48-
EasyBridgeWebViewClient webViewClient = new EasyBridgeWebViewClient(bridgeName, new SecurityPolicyChecker() {
49-
@Override
50-
public boolean check(String url, String parameters) {
51-
// no security check default
52-
return true;
53-
}
54-
});
55-
setWebViewClient(webViewClient);
49+
EasyBridgeWebChromeClient webChromeClient = new EasyBridgeWebChromeClient(this);
50+
setWebChromeClient(webChromeClient);
5651
}
5752

5853
public void registerHandler(BridgeHandler handler) {
@@ -72,4 +67,16 @@ public void clear() {
7267
easyBridge.clear();
7368
}
7469
}
70+
71+
public String getBridgeName() {
72+
return bridgeName;
73+
}
74+
75+
public SecurityPolicyChecker getPolicyChecker() {
76+
return policyChecker;
77+
}
78+
79+
public void setPolicyChecker(SecurityPolicyChecker policyChecker) {
80+
this.policyChecker = policyChecker;
81+
}
7582
}

0 commit comments

Comments
 (0)