6

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

micah
  • 7,596
  • 10
  • 49
  • 90

2 Answers2

7

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

    })
Kkkk Kkkk
  • 261
  • 4
  • 4
  • tysm :). Main key here is it needs object stringified (JSON) .. i was trying with simple string and it didn't work – minigeek Aug 13 '20 at 18:57
1

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

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
aniyd
  • 31
  • 1
  • 4