webkit 未定义:InAppBrowser Cordova

webkit is not defined : InAppBrowser Cordova

我是 运行 来自 Cordova 应用程序中打字稿文件的 InAppBrowser 实例。我的 package.json 中安装了最新的 InAppBrowser 插件。当我在此 InAppBrowser 的 loadStop 事件上添加侦听器时。它不执行脚本'webkit is not defined'。我找不到与之相关的答案。有没有人知道如何解决这个问题?

代码片段

      switchHybridApp.on("loadstop").subscribe((event: InAppBrowserEvent) => {
      console.log("Here I am 3");
      console.log('loadstop has been fired'); // this fires
      debugger;
        // when this has been executed, `webkit` variable doesn't exist inside of the `inappbrowser`
        // instance
        switchHybridApp.executeScript({ code: "\
        var message = 'this is the message';\
        var messageObj = {my_message: message};\
        var stringifiedMessageObj = JSON.stringify(messageObj);\
        webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
      });
      }
  );

错误

我尝试过但没有成功的解决方案

(window 任何).webkit....

(window).webkit....

好的,一年后我来这里回答这个问题以及我是如何解决的。所以我有无限的 运行 间隔来使用 executeScript 检查本机和 inappbrowser 的 localStorage,但这是一个非常糟糕的选择。现在 InAppBrowser 已实施 'postMessage'。 现在是很简单的交流了。

我所做的是,我在 AppBrowser 中打开的 Web 应用程序,我 运行 在我的 ts 文件中。

(window as any).webkit.messageHandlers.cordova_iab.postMessage(sendToNative);

注意,objToSend 必须是一个JSON对象。传递一个简单的字符串是行不通的。

你可以像这样创建对象 -

const sendToNative= 'frontCameraOpen';
const sendToNative= {message: sendToNative};
const stringifiedMessageObj = JSON.stringify(sendToNative);

现在从本机端,使用这个 eventlistner 就像

inAppBrowser.on('message').subscribe((dataFromIAB) => {
      alert(dataFromIAB.data.sendToNative);
});

感谢@DaveAlden 的这个功能,一年前我做错了哈哈。