Browse Source

Add (and document) injectedJavaScriptBeforeContentLoadedForMainFrameOnly and injectedJavaScriptForMainFrameOnly stub props for parity with iOS and macOS.

Jamie Birch 4 years ago
parent
commit
cd14c2e87f

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

@@ -397,6 +397,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
397 397
     ((RNCWebView) view).setInjectedJavaScriptBeforeContentLoaded(injectedJavaScriptBeforeContentLoaded);
398 398
   }
399 399
 
400
+  @ReactProp(name = "injectedJavaScriptForMainFrameOnly")
401
+  public void setInjectedJavaScriptForMainFrameOnly(WebView view, boolean enabled) {
402
+    ((RNCWebView) view).setInjectedJavaScriptForMainFrameOnly(enabled);
403
+  }
404
+
405
+  @ReactProp(name = "injectedJavaScriptBeforeContentLoadedForMainFrameOnly")
406
+  public void setInjectedJavaScriptBeforeContentLoadedForMainFrameOnly(WebView view, boolean enabled) {
407
+    ((RNCWebView) view).setInjectedJavaScriptBeforeContentLoadedForMainFrameOnly(enabled);
408
+  }
409
+
400 410
   @ReactProp(name = "messagingEnabled")
401 411
   public void setMessagingEnabled(WebView view, boolean enabled) {
402 412
     ((RNCWebView) view).setMessagingEnabled(enabled);
@@ -981,6 +991,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
981 991
     String injectedJS;
982 992
     protected @Nullable
983 993
     String injectedJSBeforeContentLoaded;
994
+
995
+    /**
996
+     * android.webkit.WebChromeClient fundamentally does not support JS injection into frames other
997
+     * than the main frame, so these two properties are mostly here just for parity with iOS & macOS.
998
+     */
999
+    protected boolean injectedJavaScriptForMainFrameOnly = true;
1000
+    protected boolean injectedJavaScriptBeforeContentLoadedForMainFrameOnly = true;
1001
+
984 1002
     protected boolean messagingEnabled = false;
985 1003
     protected @Nullable
986 1004
     RNCWebViewClient mRNCWebViewClient;
@@ -1058,6 +1076,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
1058 1076
       injectedJSBeforeContentLoaded = js;
1059 1077
     }
1060 1078
 
1079
+    public void setInjectedJavaScriptForMainFrameOnly(boolean enabled) {
1080
+      injectedJavaScriptForMainFrameOnly = enabled;
1081
+    }
1082
+
1083
+    public void setInjectedJavaScriptBeforeContentLoadedForMainFrameOnly(boolean enabled) {
1084
+      injectedJavaScriptBeforeContentLoadedForMainFrameOnly = enabled;
1085
+    }
1086
+
1061 1087
     protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
1062 1088
       return new RNCWebViewBridge(webView);
1063 1089
     }

+ 27
- 1
docs/Reference.md View File

@@ -7,7 +7,9 @@ This document lays out the current public properties and methods for the React N
7 7
 - [`source`](Reference.md#source)
8 8
 - [`automaticallyAdjustContentInsets`](Reference.md#automaticallyadjustcontentinsets)
9 9
 - [`injectedJavaScript`](Reference.md#injectedjavascript)
10
-- [`injectedJavaScriptBeforeContentLoaded`](Reference.md#injectedJavaScriptBeforeContentLoaded)
10
+- [`injectedJavaScriptBeforeContentLoaded`](Reference.md#injectedjavascriptbeforecontentloaded)
11
+- [`injectedJavaScriptForMainFrameOnly`](Reference.md#injectedjavascriptformainframeonly)
12
+- [`injectedJavaScriptBeforeContentLoadedForMainFrameOnly`](Reference.md#injectedjavascriptbeforecontentloadedformainframeonly)
11 13
 - [`mediaPlaybackRequiresUserAction`](Reference.md#mediaplaybackrequiresuseraction)
12 14
 - [`nativeConfig`](Reference.md#nativeconfig)
13 15
 - [`onError`](Reference.md#onerror)
@@ -174,6 +176,30 @@ const INJECTED_JAVASCRIPT = `(function() {
174 176
 
175 177
 ---
176 178
 
179
+### `injectedJavaScriptForMainFrameOnly`
180
+
181
+If `true` (default; mandatory for Android), loads the `injectedJavaScript` only into the main frame.
182
+
183
+If `false`, (only supported on iOS and macOS), loads it into all frames (e.g. iframes).
184
+
185
+| Type   | Required | Platform |
186
+| ------ | -------- | -------- |
187
+| bool | No       | iOS and macOS (only `true` supported for Android) |
188
+
189
+---
190
+
191
+### `injectedJavaScriptBeforeContentLoadedForMainFrameOnly`
192
+
193
+If `true` (default; mandatory for Android), loads the `injectedJavaScriptBeforeContentLoaded` only into the main frame.
194
+
195
+If `false`, (only supported on iOS and macOS), loads it into all frames (e.g. iframes).
196
+
197
+| Type   | Required | Platform |
198
+| ------ | -------- | -------- |
199
+| bool | No       | iOS and macOS (only `true` supported for Android) |
200
+
201
+---
202
+
177 203
 ### `mediaPlaybackRequiresUserAction`
178 204
 
179 205
 Boolean that determines whether HTML5 audio and video requires the user to tap them before they start playing. The default value is `true`. (Android API minimum version 17).

+ 14
- 0
src/WebViewTypes.ts View File

@@ -225,6 +225,8 @@ export interface CommonNativeWebViewProps extends ViewProps {
225 225
   incognito?: boolean;
226 226
   injectedJavaScript?: string;
227 227
   injectedJavaScriptBeforeContentLoaded?: string;
228
+  injectedJavaScriptForMainFrameOnly?: boolean;
229
+  injectedJavaScriptBeforeContentLoadedForMainFrameOnly?: boolean;
228 230
   mediaPlaybackRequiresUserAction?: boolean;
229 231
   messagingEnabled: boolean;
230 232
   onScroll?: (event: NativeScrollEvent) => void;
@@ -853,6 +855,18 @@ export interface WebViewSharedProps extends ViewProps {
853 855
    */
854 856
   injectedJavaScriptBeforeContentLoaded?: string;
855 857
 
858
+  /**
859
+   * If `true` (default; mandatory for Android), loads the `injectedJavaScript` only into the main frame.
860
+   * If `false` (only supported on iOS and macOS), loads it into all frames (e.g. iframes).
861
+   */
862
+  injectedJavaScriptForMainFrameOnly?: boolean;
863
+
864
+  /**
865
+   * If `true` (default; mandatory for Android), loads the `injectedJavaScriptBeforeContentLoaded` only into the main frame.
866
+   * If `false` (only supported on iOS and macOS), loads it into all frames (e.g. iframes).
867
+   */
868
+  injectedJavaScriptBeforeContentLoadedForMainFrameOnly?: boolean;
869
+
856 870
   /**
857 871
    * Boolean value that determines whether a horizontal scroll indicator is
858 872
    * shown in the `WebView`. The default value is `true`.