Browse Source

Add support for injectedJavaScriptBeforeContentLoaded on Android

Salvatore Randazzo 5 years ago
parent
commit
4cc83d0a82

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

392
     ((RNCWebView) view).setInjectedJavaScript(injectedJavaScript);
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
   @ReactProp(name = "messagingEnabled")
400
   @ReactProp(name = "messagingEnabled")
396
   public void setMessagingEnabled(WebView view, boolean enabled) {
401
   public void setMessagingEnabled(WebView view, boolean enabled) {
397
     ((RNCWebView) view).setMessagingEnabled(enabled);
402
     ((RNCWebView) view).setMessagingEnabled(enabled);
702
   protected static class RNCWebViewClient extends WebViewClient {
707
   protected static class RNCWebViewClient extends WebViewClient {
703
 
708
 
704
     protected boolean mLastLoadFailed = false;
709
     protected boolean mLastLoadFailed = false;
710
+    protected boolean mIsFirstLoad = true;
705
     protected @Nullable
711
     protected @Nullable
706
     ReadableArray mUrlPrefixesForDefaultIntent;
712
     ReadableArray mUrlPrefixesForDefaultIntent;
707
 
713
 
723
       super.onPageStarted(webView, url, favicon);
729
       super.onPageStarted(webView, url, favicon);
724
       mLastLoadFailed = false;
730
       mLastLoadFailed = false;
725
 
731
 
732
+      if (mIsFirstLoad) {
733
+        RNCWebView reactWebView = (RNCWebView) webView;
734
+        reactWebView.callInjectedJavaScriptBeforeContentLoaded();  
735
+      }
736
+      mIsFirstLoad = false;      
737
+
726
       dispatchEvent(
738
       dispatchEvent(
727
         webView,
739
         webView,
728
         new TopLoadingStartEvent(
740
         new TopLoadingStartEvent(
961
   protected static class RNCWebView extends WebView implements LifecycleEventListener {
973
   protected static class RNCWebView extends WebView implements LifecycleEventListener {
962
     protected @Nullable
974
     protected @Nullable
963
     String injectedJS;
975
     String injectedJS;
976
+    protected @Nullable
977
+    String injectedJSBeforeContentLoaded;
964
     protected boolean messagingEnabled = false;
978
     protected boolean messagingEnabled = false;
965
     protected @Nullable
979
     protected @Nullable
966
     RNCWebViewClient mRNCWebViewClient;
980
     RNCWebViewClient mRNCWebViewClient;
1034
       injectedJS = js;
1048
       injectedJS = js;
1035
     }
1049
     }
1036
 
1050
 
1051
+    public void setInjectedJavaScriptBeforeContentLoaded(@Nullable String js) {
1052
+      injectedJSBeforeContentLoaded = js;
1053
+    }
1054
+
1037
     protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
1055
     protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
1038
       return new RNCWebViewBridge(webView);
1056
       return new RNCWebViewBridge(webView);
1039
     }
1057
     }
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
     public void onMessage(String message) {
1104
     public void onMessage(String message) {
1079
       if (mRNCWebViewClient != null) {
1105
       if (mRNCWebViewClient != null) {
1080
         WebView webView = this;
1106
         WebView webView = this;