使用InAppBrowser将事件从远程网页直接处理到应用程序

问题描述:

我想从cordova应用程式开启外部webapp,并直接在内建应用程式处理webapp事件。例如,当加载特定的URL时,应用程序应通过调用函数来处理它。有没有人知道这是否可能?

I would like to open an external webapp from my cordova app and handle webapp events directly on the native app. For instance, when a specific URL is loaded the app should handle it by calling a function. Does anyone know if this is possible?

是的,它是可以使用InAppBrowser处理一些事件。如果您查看 API文档,将看到一个您可以使用的 addEventListener 函数。目前,您可以在外部页面上侦听的事件列表仍然有些限制:

Yes, it is definitly possible to handle some events with the InAppBrowser. If you look at the API docs you'll see an addEventListener function that you can use. Currently it looks like the list of events you can listen for on the external page are still somewhat limited:


  1. loadstart - InAppBrowser开始加载网址

  2. loadstop - InAppBrowser完成加载网址时触发的事件

  3. loaderror - InAppBrowser遇到加载错误时触发的事件URL

  4. 退出 - InAppBrowser窗口关闭时触发的事件

  1. loadstart - event fired when the InAppBrowser starts to load a URL
  2. loadstop - event fired when the InAppBrowser finished loading a URL
  3. loaderror - event fired when the InAppBrowser encounters an error loading a URL
  4. exit - event fired when the InAppBrowser window is closed

目的你可以使用 loadStart loadStop 事件(不确定哪个最适合您的目的,可能 loadStart()。)

It looks like for your purposes you could just use the loadStart or loadStop events (not sure which would be best for your purpose, probably loadStart().)

以下是一些示例代码:

在用于打开inAppBrowser的HTML页面中:

In the HTML page that you are using to open the inAppBrowser:

function onDeviceReady(){
     var ref = window.open('http://your.site.com/page', '_blank', 'location=yes');
     ref.addEventListener("loadstop", IABcallback);
}

function IABcallback(o){
    console.log("InApBrowser loaded: " +  o.url);
    if( o.url === "http://your.site.com/page2.html"){
        // Do whatever special stuff you want to do for page2 here
    }
    ... 
}