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:
huangqi03 2017-01-29 08:44:52 -08:00 committed by Facebook Github Bot
parent 9f10b85e10
commit af5c21b1ef
1 changed files with 11 additions and 1 deletions

View File

@ -503,7 +503,17 @@ public class ReactWebViewManager extends SimpleViewManager<WebView> {
try { try {
JSONObject eventInitDict = new JSONObject(); JSONObject eventInitDict = new JSONObject();
eventInitDict.put("data", args.getString(0)); 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) { } catch (JSONException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }