Skip to content

Commit

Permalink
增加js注入过滤名单
Browse files Browse the repository at this point in the history
  • Loading branch information
hjhrq1991 committed Jun 6, 2024
1 parent feef891 commit c16e7b8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/com/hjhrq1991/library/BridgeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public class BridgeConfig {
* 自定义桥名
*/
public static List<String> customBridge = Collections.singletonList(defaultBridge);

/**
* 需要过滤,不注入js桥的域名
*/
public static List<String> filterDomain;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c16e7b8

Please sign in to comment.