解决 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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM