InAppBrowser, Open Window, Post Message

谁说胖子不能爱 提交于 2019-12-24 02:22:58

问题


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)?


回答1:


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);"
        });

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)
    }

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)
      }

    })



回答2:


InAppBrowser has a limitation regarding two-way Communication: InappBrowser doesn't allow continuous communication

Solution 1 (Few Limitations)

Using IFRAME:

var myIframe = document.getElementbyId(IFRAME_ID).contentWindow;

Send msg to iframe from parent window:

myIframe.postmessage("Hello World", "*")

Receive msg in iframe from parent window:

window.addEventListener("message", function(e)
{
    // add your code here
});

Send msg to parent window from iframe:

window.parent.postmessage("msg from iframe", "*")

Receive msg in parent window from iframe:

window.addEventListener("message", function(e)
{
    // add your code here
});

Limitation:

If you try to navigate from one domain to another in the end, you may get error related to x-frame-options.


Solution 2 (Recommended)

Use cordova-plugin-wizviewmanager: to send a message from one view another:

wizViewMessenger.postMessage(message_data, targetView_name);

To receive message from one view another:

window.addEventListener("message", function(event)
{
    // add your code here
});

Advantages:

  • Directly communicate b/w App (mainView) to other custom (user-created) View.
  • No error related to x-Frame Options

Github Link:

https://github.com/Wizcorp/cordova-plugin-wizviewmanager



来源:https://stackoverflow.com/questions/47330075/inappbrowser-open-window-post-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!