InAppBrowser,打开的窗口,发布消息 [英] InAppBrowser, Open Window, Post Message

查看:313
本文介绍了InAppBrowser,打开的窗口,发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在InAppBrowser中打开网站,让该网站使用window.open打开另一个窗口,然后向该另一个窗口发送消息(反之亦然)?

Is it possible to open a site in an InAppBrowser, have that site use window.open to open another window, then send a message to that other window (and vice versa)?

推荐答案

Postmessage已在未发布的版本上实现.您可以从inAppBrowser的git页面派生最新的dev版本: https://github.com /apache/cordova-plugin-inappbrowser/ 在构建它之前,请记住删除当前组件并添加最新的开发版本以使用它. 如其文档中所述,您可以发送以下消息:

Postmessage is already implemented on not released version. You can fork the most recent dev version on inAppBrowser from their git page: https://github.com/apache/cordova-plugin-inappbrowser/ Before building it remember to remove the current component and add the most recent dev version for using it. As its described in their documentation you can dispatch postmessage like:

inAppBrowserRef.executeScript({ code: "\
            var message = 'this is the message';\
            var messageObj = {my_message: message};\
            var stringifiedMessageObj = JSON.stringify(messageObj);\
            webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
        });

或者从inAppBrowser的应用程序内部,类似于:

Or from inside inAppBrowser's app its something like:

  const message = 'message'
  const messageObj = {message: message}
  const stringifiedMessageObj = JSON.stringify(messageObj)
if (window.webkit && Window.webkit.messageHandlers) {
      console.log('postmessage call on webkit')
      window.webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj)
    }

您可以在cordova中收听它,例如:

And you can listen for it inside cordova like:

this.inAppBrowserRef.on('message').subscribe((event) => {
      console.log(' postmessage received')
      const postObject:any = event
      if(postObject.data.message){
         console.log(postObject.data.message)
      }

    })

这篇关于InAppBrowser,打开的窗口,发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆