如何将 postMessage 与 ionic in-app-browser 插件一起使用?

How to use postMessage with the ionic in-app-browser plugin?

Cordova in-app-browser模块的3.1.0-dev版本开始,我们似乎可以通过以下方式使用postMessageAPI:

webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);

然而,经过长时间的搜索,我没有成功地让它工作。它总是默默地失败。此外,webkit 对象不存在......根据 this 问题中的建议,我提交了一个有效的 JSON 字符串。请在下面找到我的 package.json 文件的摘录:

{
  "dependencies": {
     "@angular/core": "~8.1.2",
     "@ionic-native/core": "^5.16.0",
     "@ionic-native/in-app-browser": "^5.16.0",
     "cordova-plugin-inappbrowser": "git+https://github.com/apache/cordova-plugin-inappbrowser.git"
   }
}

我用 npm list –depth=0 检查了应用程序浏览器内的 cordova 版本,我安装了 3.1.1-dev。 你们有什么想法吗?如果您需要更多信息,请不要犹豫。 :-)

编辑:我想将数据从 iframe 发送到我的 "main thread"。为此,我使用了在 iframe 中注入脚本的 executeScript 方法。但遗憾的是,我没有 webkit 对象。

this.ref = this.iab.create("https://google.com", "_blank", "EnableViewPortScale=yes,location=no,hidden=false");
this.ref.on('loadstop').subscribe(event => {
  this.ref.executeScript({
                code: 'webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify({foo:"bar"});'
            });
});

当我测试我的应用程序时,我使用了命令 ionic serve --devapp 并在我的 Android phone 上使用了应用程序 Ionic DevApp。因此,看起来它不是真正的 Android 本机应用程序。 我引用 cordova inappbrowser git repo:

Browser Quirks loadstart, loaderror, message events are not fired.

我在我的笔记本电脑上安装了一个 Android 模拟器,我成功地将一个 postMessage 从我的 "iframe" 传输到我的 "main thread"。