Question

I'm trying to inject code into a website that was opened via InAppBrowser with:

var inappbrowser = window.open('http://example.com', '_blank', 'location=no,toolbar=no');

For that, I'm using (not exactly in this structure, but to the same effect):

var loop;
inappbrowser.addEventListener('loadstop', function(event) {
    console.log('page finished loading - ' + JSON.stringify(event));
    clearInterval(loop);
    loop = setInterval(function() {
        console.log('checking for request');
        inappbrowser.executeScript({
            code: '1+2'
        }, function(result) {
            console.log('received: ' + result);
        });
    }, 500);
});

It works fine with the initial page, but once I navigate to another page from inside InAppBrowser, no script is executed. The event works, the loop works, but 'received: [3]' doesn't get logged at all anymore.

Plugins currently installed are:

  • InAppBrowser
  • Console
  • BarcodeScanner

InAppBrowser and BarcodeScanner were both installed directly from their Github repositories.

  • Cordova version: 3.3 (tried 3.4 as well, with the same result)
  • Xcode: 5.0.2
  • iOS Target Version: 7

I always use cordova prepare and then run with Xdebug directly on an iPad Air. I have also tried cordova build ios.

I've also noticed that the script gets later executed while navigating to another page. It also does not run while scrolling on the initial page.

Does anyone have any idea what's going on here? Also, does this happen with everyone else?

Thanks a lot in advance! I've researched, started from scratch in brand new cordova projects, and basically spent the whole day on this strange occurrence...

Was it helpful?

Solution

So it seems the problem was the loop... it has to be cleared at loadstart.

So the following solved it:

self.inappbrowser.addEventListener('loadstart', function(event) {

    trace('loading page - ' + JSON.stringify(event));

    clearInterval(loop);
});

Hope this helps someone else as well!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top