cordova-plugin-inappbrowser icon indicating copy to clipboard operation
cordova-plugin-inappbrowser copied to clipboard

beforeload Problem

Open gehirnmitei opened this issue 6 years ago • 6 comments

Bug Report

Added console.log() statement in 4 events (beforeload, loadstart, loadstop, loaderror)

Problem

beforeload is called twice on first load/navigation change, then only after two every second navigation change.

What is expected to happen?

navigation change should give following console output: beforeloadCallBack loadStartCallBack loadStopCallBack

What does actually happen?

after button click (expected): loadStartCallBack loadStopCallBack

after first navigation change (why beforeloadCallBack two times): beforeloadCallBack x2 loadStopCallBack loadStartCallBack loadStopCallBack

next navigation change (no beforeloadCallBack): loadStartCallBack loadStopCallBack

next navigation change (expected): beforeloadCallBack loadStartCallBack loadStopCallBack

next: (no beforeloadCallBack) loadStartCallBack loadStopCallBack

Information

Link to demo/repo

Command or Code

  • get repo.
  • cordova prepare
  • cordova run android
  • click "Start Browsing" button

Environment, Platform, Device

Android 9, Samsung Galaxy A50. Android 7.1.1, Samsung Galaxy J5 (2016)

Version information

cordova -v: 9.0.0 ([email protected]) cordova-android: ^8.1.0

plugins: cordova-plugin-inappbrowser 3.1.0 cordova-plugin-whitelist 1.3.4

Checklist

  • [x] I searched for existing GitHub issues
  • [x] I updated all Cordova tooling to most recent version
  • [x] I included all the necessary information above

gehirnmitei avatar Sep 23 '19 11:09 gehirnmitei

Are you using TypeScript? I'm having issues when using TypeScript with this plugin. Details here: https://github.com/apache/cordova-plugin-inappbrowser/issues/338

benvallack avatar Oct 28 '19 15:10 benvallack

No Typescript here.

gehirnmitei avatar Oct 28 '19 15:10 gehirnmitei

I can confirm this problem. The events are not always reliable, I'm having a case where simetimes beforeload is not called at all and it directly starts with loadstart. Perhaps some kind of race condition. Sometimes it works, sometimes not.

No Typescript and no clue why this happens.

JPustkuchen avatar Dec 12 '19 15:12 JPustkuchen

I think #571 is a duplicate of this. This is truely funny. In 3.1.0 the "beforeload" is only called on every second page load / click!

You can simply check this by adding an alert or console.log in the events. I guess that there is a bug in the event binding and the events are re-un-bound on every click. So with the first click the event is bound, then unbound by this page load, re-bound by the next etc.... I could reproduce this behaviour very clearly now and I think most of the "onbeforeload" issues are based on this problem.

JPustkuchen avatar Dec 12 '19 15:12 JPustkuchen

Woops, I left a comment on the wrong ticket but yes I'm seeing the exact same behaviour https://github.com/apache/cordova-plugin-inappbrowser/issues/571#issuecomment-820439266 I'll delete that and add in my comments here

Has anyone had any sort of success with this? I've just ran into the same issue, however the solution recommended by @wvengen is not relevant, nor does it fix it. We had already ran into a similar issue when using the message event after launching a system window, so we already were unsubscribing then resubscribing to reattach event handlers.

We too also want to intercept all navigation changes and conditionally load the system browser. I went back to basics and did a basic test without any system window and the beforeload event fires every second time as soon as you use _loadAfterBeforeload() to continue the URL navigation.

The code below fires the beforeload event on every navigation change

  public openIAB(evt: Event) {
    const options: InAppBrowserOptions = {
      beforeload: 'yes'
    }
    const inAppBrowserRef = this.inAppBrowser.create(this.url, '_blank', options);
    inAppBrowserRef.show();
    inAppBrowserRef.on('beforeload').subscribe((resp) => {
      console.log(`===> beforeload resp.url (${resp.url});`)
    });
  }

The code below fires the beforeload event every second navigation change

  public openIAB(evt: Event) {
    const options: InAppBrowserOptions = {
      beforeload: 'yes'
    }
    const inAppBrowserRef = this.inAppBrowser.create(this.url, '_blank', options);
    inAppBrowserRef.show();
    inAppBrowserRef.on('beforeload').subscribe((resp) => {
      console.log(`===> beforeload resp.url (${resp.url});`);
      if (resp.url.startsWith(this.baseUrl)) {
        console.log(`===> beforeload matched baseUrl, calling _loadAfterBeforeload() `);
        inAppBrowserRef._loadAfterBeforeload(resp.url);
      } else {
        console.log(`===> beforeload not matched baseUrl, do nothing`);
      }
    });
  }

This only effects Android, the IOS plugin works fine. I've also tried with the latest version 5.0.0, we were using 4.1.0. I've tried to debug InAppBrowser.java and try get an understanding what's going on but my knowledge of Android isn't good enough to understand.

There seems to be lots of bugs raised for very similar issues and this issue was raised a year and a half ago with no solution so I'm not holding out much hope unless I'm able to figure out what's happening in the plugin itself, which I think I'd be unlikely to do. The only other option is to descope this requirement for Android

lbod avatar Apr 15 '21 14:04 lbod

https://github.com/apache/cordova-plugin-inappbrowser/pull/755

monove avatar Jun 27 '22 20:06 monove