From c16e7b8c9f3ae23e417fc0a0079e77198fe5ff2b Mon Sep 17 00:00:00 2001 From: RenCho Hwang Date: Thu, 6 Jun 2024 14:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0js=E6=B3=A8=E5=85=A5=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E5=90=8D=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/build.gradle | 2 +- .../com/hjhrq1991/library/BridgeConfig.java | 5 +++ .../library/BridgeWebViewClient.java | 31 +++++++++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/library/build.gradle b/library/build.gradle index 033b014..d91a030 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -4,7 +4,7 @@ plugins { } def group = 'com.github.hjhrq1991' -def libraryVersion = '1.1.5' +def libraryVersion = '1.1.6' // Because the components are created only during the afterEvaluate phase, you must // configure your publications using the afterEvaluate() lifecycle method. diff --git a/library/src/main/java/com/hjhrq1991/library/BridgeConfig.java b/library/src/main/java/com/hjhrq1991/library/BridgeConfig.java index c4abc0a..2946279 100644 --- a/library/src/main/java/com/hjhrq1991/library/BridgeConfig.java +++ b/library/src/main/java/com/hjhrq1991/library/BridgeConfig.java @@ -18,4 +18,9 @@ public class BridgeConfig { * 自定义桥名 */ public static List customBridge = Collections.singletonList(defaultBridge); + + /** + * 需要过滤,不注入js桥的域名 + */ + public static List filterDomain; } diff --git a/library/src/main/java/com/hjhrq1991/library/BridgeWebViewClient.java b/library/src/main/java/com/hjhrq1991/library/BridgeWebViewClient.java index 7ee6953..731bef5 100644 --- a/library/src/main/java/com/hjhrq1991/library/BridgeWebViewClient.java +++ b/library/src/main/java/com/hjhrq1991/library/BridgeWebViewClient.java @@ -2,6 +2,7 @@ import android.graphics.Bitmap; import android.net.http.SslError; +import android.text.TextUtils; import android.view.KeyEvent; import android.webkit.ClientCertRequest; import android.webkit.HttpAuthHandler; @@ -120,19 +121,31 @@ public void onPageFinished(WebView view, String url) { if (bridgeWebViewClientListener != null) { bridgeWebViewClientListener.onPageFinishedFirst(view, url); } - //modify:hjhrq1991,web为渲染即跳转导致系统未调用onPageStarted就调用onPageFinished方法引起的js桥初始化失败 - if (BridgeConfig.toLoadJs != null && !url.contains("about:blank") && !isRedirected) { - for (int i = 0; i < BridgeConfig.customBridge.size(); i++) { - String bridgeName = BridgeConfig.customBridge.get(i); - BridgeUtil.webViewLoadLocalJs(view, BridgeConfig.toLoadJs, BridgeConfig.defaultBridge, bridgeName); + boolean canLoadJS = true; + //modify:hjhrq1991,检查是否需要注入js + if (BridgeConfig.filterDomain != null && !BridgeConfig.filterDomain.isEmpty()) { + for (int i = 0; i < BridgeConfig.filterDomain.size(); i++) { + String filter = BridgeConfig.filterDomain.get(i); + if (!TextUtils.isEmpty(url) && url.contains(filter)) { + canLoadJS = false; + } } } + if (canLoadJS){ + //modify:hjhrq1991,web为渲染即跳转导致系统未调用onPageStarted就调用onPageFinished方法引起的js桥初始化失败 + if (BridgeConfig.toLoadJs != null && !url.contains("about:blank") && !isRedirected) { + for (int i = 0; i < BridgeConfig.customBridge.size(); i++) { + String bridgeName = BridgeConfig.customBridge.get(i); + BridgeUtil.webViewLoadLocalJs(view, BridgeConfig.toLoadJs, BridgeConfig.defaultBridge, bridgeName); + } + } - if (webView.getStartupMessage() != null) { - for (Message m : webView.getStartupMessage()) { - webView.dispatchMessage(m); + if (webView.getStartupMessage() != null) { + for (Message m : webView.getStartupMessage()) { + webView.dispatchMessage(m); + } + webView.setStartupMessage(null); } - webView.setStartupMessage(null); } if (bridgeWebViewClientListener != null) {