mirror of
https://github.com/status-im/react-native-webview.git
synced 2026-08-31 03:31:05 +00:00
This commit is contained in:
@@ -9,6 +9,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;
|
||||
@@ -318,11 +319,24 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
|
||||
}
|
||||
}
|
||||
|
||||
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})();");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +355,7 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
|
||||
});
|
||||
}
|
||||
|
||||
loadUrl("javascript:(" +
|
||||
evaluateJavascriptWithFallback("(" +
|
||||
"window.originalPostMessage = window.postMessage," +
|
||||
"window.postMessage = function(data) {" +
|
||||
BRIDGE_NAME + ".postMessage(String(data));" +
|
||||
@@ -630,9 +644,10 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
|
||||
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 {" +
|
||||
@@ -648,7 +663,8 @@ public class RCTWebViewManager extends SimpleViewManager<WebView> {
|
||||
}
|
||||
break;
|
||||
case COMMAND_INJECT_JAVASCRIPT:
|
||||
root.loadUrl("javascript:" + args.getString(0));
|
||||
ReactWebView reactWebView = (ReactWebView) root;
|
||||
reactWebView.evaluateJavascriptWithFallback(args.getString(0));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user