Browse Source

Add support for injectedJavaScriptBeforeContentLoaded on Android

Salvatore Randazzo 4 years ago
parent
commit
4cc83d0a82

+ 26
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

@@ -392,6 +392,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
392 392
     ((RNCWebView) view).setInjectedJavaScript(injectedJavaScript);
393 393
   }
394 394
 
395
+  @ReactProp(name = "injectedJavaScriptBeforeContentLoaded")
396
+  public void setInjectedJavaScriptBeforeContentLoaded(WebView view, @Nullable String injectedJavaScriptBeforeContentLoaded) {
397
+    ((RNCWebView) view).setInjectedJavaScriptBeforeContentLoaded(injectedJavaScriptBeforeContentLoaded);
398
+  }
399
+
395 400
   @ReactProp(name = "messagingEnabled")
396 401
   public void setMessagingEnabled(WebView view, boolean enabled) {
397 402
     ((RNCWebView) view).setMessagingEnabled(enabled);
@@ -702,6 +707,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
702 707
   protected static class RNCWebViewClient extends WebViewClient {
703 708
 
704 709
     protected boolean mLastLoadFailed = false;
710
+    protected boolean mIsFirstLoad = true;
705 711
     protected @Nullable
706 712
     ReadableArray mUrlPrefixesForDefaultIntent;
707 713
 
@@ -723,6 +729,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
723 729
       super.onPageStarted(webView, url, favicon);
724 730
       mLastLoadFailed = false;
725 731
 
732
+      if (mIsFirstLoad) {
733
+        RNCWebView reactWebView = (RNCWebView) webView;
734
+        reactWebView.callInjectedJavaScriptBeforeContentLoaded();  
735
+      }
736
+      mIsFirstLoad = false;      
737
+
726 738
       dispatchEvent(
727 739
         webView,
728 740
         new TopLoadingStartEvent(
@@ -961,6 +973,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
961 973
   protected static class RNCWebView extends WebView implements LifecycleEventListener {
962 974
     protected @Nullable
963 975
     String injectedJS;
976
+    protected @Nullable
977
+    String injectedJSBeforeContentLoaded;
964 978
     protected boolean messagingEnabled = false;
965 979
     protected @Nullable
966 980
     RNCWebViewClient mRNCWebViewClient;
@@ -1034,6 +1048,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1034 1048
       injectedJS = js;
1035 1049
     }
1036 1050
 
1051
+    public void setInjectedJavaScriptBeforeContentLoaded(@Nullable String js) {
1052
+      injectedJSBeforeContentLoaded = js;
1053
+    }
1054
+
1037 1055
     protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
1038 1056
       return new RNCWebViewBridge(webView);
1039 1057
     }
@@ -1075,6 +1093,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1075 1093
       }
1076 1094
     }
1077 1095
 
1096
+    public void callInjectedJavaScriptBeforeContentLoaded() {
1097
+      if (getSettings().getJavaScriptEnabled() &&
1098
+      injectedJSBeforeContentLoaded != null &&
1099
+      !TextUtils.isEmpty(injectedJSBeforeContentLoaded)) {
1100
+        evaluateJavascriptWithFallback("(function() {\n" + injectedJSBeforeContentLoaded + ";\n})();");
1101
+      }
1102
+    }
1103
+
1078 1104
     public void onMessage(String message) {
1079 1105
       if (mRNCWebViewClient != null) {
1080 1106
         WebView webView = this;