I'm using this module (https://github.com/apache/cordova-plugin-inappbrowser) for the ability to open external links within my Cordova application. However, postMessage example from documentation doesn't work.

I need the ability to be able for an inappbrowser instance to communicate with the parent (the opener). Given that there is no opener object with the inappbrowser, I've looked through repo's documentation and tests, and I cannot reproduce the postMessage API to communicate between an inappbrowser instance and the main Cordova application (parent).

Here's a simple example taken from the documentation/test within this repo:

const ref = cordova.InAppBrowser.open('http://www.google.com', '_blank');

ref.addEventListener('loadstop', () => {
  console.log('loadstop has been fired'); // this fires

  // when this has been executed, `webkit` variable doesn't exist inside of the `inappbrowser`
  // instance
  ref.executeScript({
    code: `(() => {
      var message = "TESTING!!!";
      webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message));
    })()`
  });
});

// this is never fired
ref.addEventListener('message', (...args) => {
  console.log('MESSAGE RECEIVED FROM IN_APP_BROWSER', ...args);
});

Related posts

Recent Viewed