diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java index 6bcc4e185..6e55b80f8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/webview/ReactWebViewManager.java @@ -16,6 +16,7 @@ import java.util.regex.Pattern; import javax.annotation.Nullable; import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.Locale; @@ -325,11 +326,25 @@ public class ReactWebViewManager extends SimpleViewManager { } } + protected void evaluateJavascriptWithFallback(String script) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + evaluateJavascript(script, null); + return; + } + + try { + loadUrl("javascript:" + URLEncoder.encode(script, "UTF-8")); + } catch (UnsupportedEncodingException e) { + // UTF-8 should always be supported + throw new RuntimeException(e); + } + } + public void callInjectedJavaScript() { if (getSettings().getJavaScriptEnabled() && injectedJS != null && !TextUtils.isEmpty(injectedJS)) { - loadUrl("javascript:(function() {\n" + injectedJS + ";\n})();"); + evaluateJavascriptWithFallback("(function() {\n" + injectedJS + ";\n})();"); } } @@ -348,7 +363,7 @@ public class ReactWebViewManager extends SimpleViewManager { }); } - loadUrl("javascript:(" + + evaluateJavascriptWithFallback("(" + "window.originalPostMessage = window.postMessage," + "window.postMessage = function(data) {" + BRIDGE_NAME + ".postMessage(String(data));" + @@ -637,9 +652,10 @@ public class ReactWebViewManager extends SimpleViewManager { break; case COMMAND_POST_MESSAGE: try { + ReactWebView reactWebView = (ReactWebView) root; JSONObject eventInitDict = new JSONObject(); eventInitDict.put("data", args.getString(0)); - root.loadUrl("javascript:(function () {" + + reactWebView.evaluateJavascriptWithFallback("(function () {" + "var event;" + "var data = " + eventInitDict.toString() + ";" + "try {" + @@ -655,7 +671,8 @@ public class ReactWebViewManager extends SimpleViewManager { } break; case COMMAND_INJECT_JAVASCRIPT: - root.loadUrl("javascript:" + args.getString(0)); + ReactWebView reactWebView = (ReactWebView) root; + reactWebView.evaluateJavascriptWithFallback(args.getString(0)); break; } }