Browse Source

Merge branch 'master' into import/0c576ef-allow-file-access-android

Thibault Malbranche 6 years ago
parent
commit
220d7a1973

+ 21
- 4
android/src/main/java/com/reactnativecommunity/webview/RCTWebViewManager.java View File

@@ -9,6 +9,7 @@ import java.util.regex.Pattern;
9 9
 import javax.annotation.Nullable;
10 10
 
11 11
 import java.io.UnsupportedEncodingException;
12
+import java.net.URLEncoder;
12 13
 import java.util.ArrayList;
13 14
 import java.util.HashMap;
14 15
 import java.util.Locale;
@@ -318,11 +319,25 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
318 319
       }
319 320
     }
320 321
 
322
+    protected void evaluateJavascriptWithFallback(String script) {
323
+      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
324
+        evaluateJavascript(script, null);
325
+        return;
326
+      }
327
+
328
+      try {
329
+        loadUrl("javascript:" + URLEncoder.encode(script, "UTF-8"));
330
+      } catch (UnsupportedEncodingException e) {
331
+        // UTF-8 should always be supported
332
+        throw new RuntimeException(e);
333
+      }
334
+    }
335
+
321 336
     public void callInjectedJavaScript() {
322 337
       if (getSettings().getJavaScriptEnabled() &&
323 338
           injectedJS != null &&
324 339
           !TextUtils.isEmpty(injectedJS)) {
325
-        loadUrl("javascript:(function() {\n" + injectedJS + ";\n})();");
340
+        evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
326 341
       }
327 342
     }
328 343
 
@@ -341,7 +356,7 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
341 356
           });
342 357
         }
343 358
 
344
-        loadUrl("javascript:(" +
359
+        evaluateJavascriptWithFallback("(" +
345 360
           "window.originalPostMessage = window.postMessage," +
346 361
           "window.postMessage = function(data) {" +
347 362
             BRIDGE_NAME + ".postMessage(String(data));" +
@@ -637,9 +652,10 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
637 652
         break;
638 653
       case COMMAND_POST_MESSAGE:
639 654
         try {
655
+          ReactWebView reactWebView = (ReactWebView) root;
640 656
           JSONObject eventInitDict = new JSONObject();
641 657
           eventInitDict.put("data", args.getString(0));
642
-          root.loadUrl("javascript:(function () {" +
658
+          reactWebView.evaluateJavascriptWithFallback("(function () {" +
643 659
             "var event;" +
644 660
             "var data = " + eventInitDict.toString() + ";" +
645 661
             "try {" +
@@ -655,7 +671,8 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
655 671
         }
656 672
         break;
657 673
       case COMMAND_INJECT_JAVASCRIPT:
658
-        root.loadUrl("javascript:" + args.getString(0));
674
+        ReactWebView reactWebView = (ReactWebView) root;
675
+        reactWebView.evaluateJavascriptWithFallback(args.getString(0));
659 676
         break;
660 677
     }
661 678
   }