Browse Source

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

Thibault Malbranche 5 years ago
parent
commit
220d7a1973

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

9
 import javax.annotation.Nullable;
9
 import javax.annotation.Nullable;
10
 
10
 
11
 import java.io.UnsupportedEncodingException;
11
 import java.io.UnsupportedEncodingException;
12
+import java.net.URLEncoder;
12
 import java.util.ArrayList;
13
 import java.util.ArrayList;
13
 import java.util.HashMap;
14
 import java.util.HashMap;
14
 import java.util.Locale;
15
 import java.util.Locale;
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
     public void callInjectedJavaScript() {
336
     public void callInjectedJavaScript() {
322
       if (getSettings().getJavaScriptEnabled() &&
337
       if (getSettings().getJavaScriptEnabled() &&
323
           injectedJS != null &&
338
           injectedJS != null &&
324
           !TextUtils.isEmpty(injectedJS)) {
339
           !TextUtils.isEmpty(injectedJS)) {
325
-        loadUrl("javascript:(function() {\n" + injectedJS + ";\n})();");
340
+        evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();");
326
       }
341
       }
327
     }
342
     }
328
 
343
 
341
           });
356
           });
342
         }
357
         }
343
 
358
 
344
-        loadUrl("javascript:(" +
359
+        evaluateJavascriptWithFallback("(" +
345
           "window.originalPostMessage = window.postMessage," +
360
           "window.originalPostMessage = window.postMessage," +
346
           "window.postMessage = function(data) {" +
361
           "window.postMessage = function(data) {" +
347
             BRIDGE_NAME + ".postMessage(String(data));" +
362
             BRIDGE_NAME + ".postMessage(String(data));" +
637
         break;
652
         break;
638
       case COMMAND_POST_MESSAGE:
653
       case COMMAND_POST_MESSAGE:
639
         try {
654
         try {
655
+          ReactWebView reactWebView = (ReactWebView) root;
640
           JSONObject eventInitDict = new JSONObject();
656
           JSONObject eventInitDict = new JSONObject();
641
           eventInitDict.put("data", args.getString(0));
657
           eventInitDict.put("data", args.getString(0));
642
-          root.loadUrl("javascript:(function () {" +
658
+          reactWebView.evaluateJavascriptWithFallback("(function () {" +
643
             "var event;" +
659
             "var event;" +
644
             "var data = " + eventInitDict.toString() + ";" +
660
             "var data = " + eventInitDict.toString() + ";" +
645
             "try {" +
661
             "try {" +
655
         }
671
         }
656
         break;
672
         break;
657
       case COMMAND_INJECT_JAVASCRIPT:
673
       case COMMAND_INJECT_JAVASCRIPT:
658
-        root.loadUrl("javascript:" + args.getString(0));
674
+        ReactWebView reactWebView = (ReactWebView) root;
675
+        reactWebView.evaluateJavascriptWithFallback(args.getString(0));
659
         break;
676
         break;
660
     }
677
     }
661
   }
678
   }