解决 Setting onMessage on a WebView overrides existing values of window.postMessage, but a previous value was defined.

 

在 issue 里并没有找到官方的解决办法:不过下面这段代码,就可以不在崩溃!

let getTitle: Function = function(url: string) {
  setTimeout(function() {
    // 防止postMessage崩溃
    var originalPostMessage = window.postMessage;
    var patchedPostMessage = function(
      message: any,
      targetOrigin: string,
      transfer?: any[]
    ) {
      originalPostMessage(message, targetOrigin, transfer);
    };
    patchedPostMessage.toString = function() {
      return String(Object.hasOwnProperty).replace(
        "hasOwnProperty",
        "postMessage"
      );
    };
    window.postMessage = patchedPostMessage;
    window.postMessage(JSON.stringify({ title: document.title }), url);
  }, 0);
};
let titleJs = "(" + String(getTitle) + ")();";

https://github.com/facebook/react-native/issues/10865#issuecomment-269847703

posted @ 2017-09-22 20:33  努力努力再努力吧  阅读(1904)  评论(0编辑  收藏  举报