Permalink
Browse files

fix: webview.postMessage throws not a constructor in WebView in Andro…

…id 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 #11560

Differential Revision: D4461360

fbshipit-source-id: 110220817d9570e86b31dcf8fe85f6616d54d36e
  • Loading branch information...
pinqy520 authored and facebook-github-bot committed Jan 29, 2017
1 parent 9f10b85 commit af5c21b1efc26aee519d7e839b2323d00030bf68
@@ -503,7 +503,17 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
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);
}

0 comments on commit af5c21b

Please sign in to comment.