InAppBrowser executeScript callback not firing at all

杀马特。学长 韩版系。学妹 提交于 2019-12-21 05:16:15

问题


I am creating building a Hybrid application using Ionic with ngCordova. In the application login workflow I am returning a page from server to the InAppBrowser plug-in. I need to read some values from the returned page in my Ionic app.

The usual way would be to call executeScript on 'loadstop' event, and setup a call back when that script returns something. And I have that setup as you can see in the following code. However, no matter what I try, iOS Emulator, Android Emulator the callback is not firing.

Here is the page that opens up the google website in InAppBrowser.

 $cordovaInAppBrowser.open('http://google.com', '_blank', options)
    .then(function (event) {
      //alert('succed')
    })
    .catch(function (event) {
      //alert('errored')
    });

And following is the loadstop event handler. The 1+1 script is being injected fine because I do see the alert with 2. However the callback function is not being invoked at all !!

 $rootScope.$on('$cordovaInAppBrowser:loadstop', function (e, event) {
      $cordovaInAppBrowser.executeScript(
        {
          code: "alert(1+1);"
        },
        function (values) {
          alert('add completed');
        });
});

I gave multiple emulators a try but none of them are working. Appreciate your help in advance.


回答1:


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


来源:https://stackoverflow.com/questions/32172442/inappbrowser-executescript-callback-not-firing-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!