ngCordova InAppBrowser executeScript callback not firing

Hi all,
I’m not able to get working the callback function for executeScript.
This is the code
$cordovaInAppBrowser.executeScript(
{code: “alert(‘hello’)”},
function (values) {
console.log(‘callback’);
}
);
The code:alert(‘hello’) is fired, so I got ‘hello’, but not the console log ‘callback’ (tried also with an alert)
Doing some debug, I saw that the part of the code where the callback should be executed is in cordova.js

msgs is ‘undefined’, and not equal to ‘@Null arguments’ as expected.
So ‘androidExec(success, …)’ is never executed. success is the callback for executeScript.

var msgs = nativeApiProvider.get().exec(bridgeSecret, service, action, callbackId, argsJson);
if (jsToNativeBridgeMode == jsToNativeModes.JS_OBJECT && msgs === "@Null arguments.") {
        androidExec.setJsToNativeBridgeMode(jsToNativeModes.PROMPT);
        androidExec(success, fail, service, action, args);
        androidExec.setJsToNativeBridgeMode(jsToNativeModes.JS_OBJECT);
    }

Any Idea? Thank you in advance.

1 Like

I’m in the same problem…
I’ve been searching but nothing to fix this yet…

:confused:

I found this, hope helps you! :grin:
The problem is that executeScript is a promise itself, so to make your code working you must write something like this:

$rootScope.$on(’$cordovaInAppBrowser:loadstop’, function (e, event) {
$cordovaInAppBrowser.executeScript(
{
code: “alert(1+1);”
}).then(
function (values) {
alert(‘add completed’);
});
});

2 Likes

Thanks man,
you saved my life :smiley:
Works like a charm. This should be added to ngCordova documentation.

Hi! Great idea, having some similar problems with cordova saying ‘callback is not a function’. Where did you place that code? app.js or in with the service you are using?

Hi,

On my case:

checkInAppBrowserObject.executeScript({code: 'document.getElementsByClassName("fr66n")[0].innerHTML'})
.then(()=>{
console.log('OK');
})
.catch(()=>{
console.log('OK');
})

If the element fr66n is not present in the HTML the callback is not fired (then, catch)

Some idea?