apache / cordova-plugin-inappbrowser

Apache Cordova InAppBrowser Plugin

Home Page:https://cordova.apache.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Listeners stop working after open external link through receiving message

artem-stepanov opened this issue · comments

Bug Report

Listeners stop working after open external link through receiving message.

Problem

Wrote a script that opens external links from an external site from InAppBrowser in the system browser. The first time it works fine, but when you return to the application all the listeners to stop working.

Code

var iab = cordova.InAppBrowser.open('https://site.com/', "_blank", "location=no,toolbar=no,hardwareback=no,fullscreen=no,zoom=no");

iab.addEventListener('loadstop', function () {
 iab.executeScript({
  code: "(function(){ $(\"a[target='_blank']\").on('click', function(e){\n" +
   "e.preventDefault();\n" +
   "var message = {'OpenLinkInSystemBrowser': $(e.currentTarget).prop('href')};\n" +
   "webkit.messageHandlers.cordova_iab.postMessage(JSON.stringify(message));\n" +
   "}); })();"
  });
});

iab.addEventListener('message', function (params) {
 if (params.data.OpenLinkInSystemBrowser !== undefined) {
  cordova.InAppBrowser.open(encodeURI(params.data.OpenLinkInSystemBrowser), '_system');
 }
});

Environment, Platform, Device

On iOS and Android

Version information

Cordova InAppBrowser Plugin: 5.0.0