admin管理员组

文章数量:1279148

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

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

Share Improve this question asked Nov 16, 2017 at 12:48 micahmicah 8,10610 gold badges62 silver badges102 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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./apache/cordova-plugin-inappbrowser/ Before building it remember to remove the current ponent 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)
      }

    })

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

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 (Remended)

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 municate b/w App (mainView) to other custom (user-created) View.
  • No error related to x-Frame Options

Github Link:

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

本文标签: javascriptInAppBrowserOpen WindowPost MessageStack Overflow