7

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);
});
Detuned
  • 3,652
  • 4
  • 27
  • 54

2 Answers2

1

The documentation is pointing to a version that has not yet been released. I pointed to the 3.1.0-dev version of this package and the implementation works like a charm.

Detuned
  • 3,652
  • 4
  • 27
  • 54
  • Hello Detuned, Can you please explain how did you point it to dev version. I am stuck at same position. triedyour code, it does not enter into the script. what else changes are needed. – minigeek May 13 '19 at 15:17
  • @Detuned How do you send message from the openend url in the InAppBrowser to your app to activate the message event listener? – Piero Alberto Jun 01 '19 at 07:31
  • @Detuned - Is this feature now available on the master branch? (e.g. I don't need to pull from a separate branch?) –  Dec 08 '21 at 21:25
-1

Yes, the InAppBrowser does not yet have postMessage implemented. The only solution in this case would be to use an iframe to receive postMessage from your external webpage. This has been discussed earlier on this site too.

andreszs
  • 2,896
  • 3
  • 26
  • 34