Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InAppBrowser, Open Window, Post Message

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

like image 492
micah Avatar asked Nov 16 '17 12:11

micah


People also ask

What is InAppBrowser in Android?

The InAppBrowser is a web browser view that displays when calling [window. open](window. open. html)() , or when opening a link formed as <a target="_blank"> .

How do I close InAppBrowser?

There is no action to close the InAppBrowser. How can we achieve this? The user is redirected to an aws login page in the IAB and after suscessfull login the IAB should close and the user should be redirected to a screen in the app.

How do I use InAppBrowser in Cordova?

function openBrowser() { var url = 'https://cordova.apache.org'; var target = '_blank'; var options = "location = yes" var ref = cordova. InAppBrowser. open(url, target, options); ref. addEventListener('loadstart', loadstartCallback); ref.


1 Answers

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

    })
like image 77
Kkkk Kkkk Avatar answered Sep 26 '22 20:09

Kkkk Kkkk