fix: webview.postMessage throws not a constructor in WebView in Android 4.1 ~ 4.3
Summary: `new MessageEvent` not support for Android 4.1 ~ 4.3 WebView. change ``` javascript document.dispatchEvent(new MessageEvent('message', data))) ``` to ``` javascript (function () { var event; var data = {}; try { event = new MessageEvent('message', data); } catch (e) { event = document.createEvent('MessageEvent'); event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source); } document.dispatchEvent(event); })() ``` Closes https://github.com/facebook/react-native/pull/11560 Differential Revision: D4461360 fbshipit-source-id: 110220817d9570e86b31dcf8fe85f6616d54d36e
This commit is contained in:
parent
9f10b85e10
commit
af5c21b1ef
|
@ -503,7 +503,17 @@ public class ReactWebViewManager extends SimpleViewManager<WebView> {
|
|||
try {
|
||||
JSONObject eventInitDict = new JSONObject();
|
||||
eventInitDict.put("data", args.getString(0));
|
||||
root.loadUrl("javascript:(document.dispatchEvent(new MessageEvent('message', " + eventInitDict.toString() + ")))");
|
||||
root.loadUrl("javascript:(function () {" +
|
||||
"var event;" +
|
||||
"var data = " + eventInitDict.toString() + ";" +
|
||||
"try {" +
|
||||
"event = new MessageEvent('message', data);" +
|
||||
"} catch (e) {" +
|
||||
"event = document.createEvent('MessageEvent');" +
|
||||
"event.initMessageEvent('message', true, true, data.data, data.origin, data.lastEventId, data.source);" +
|
||||
"}" +
|
||||
"document.dispatchEvent(event);" +
|
||||
"})();");
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue