无法关闭inAppBrowser窗口 [英] impossible to close inAppBrowser window

查看:438
本文介绍了无法关闭inAppBrowser窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以打开窗口,并在其中执行很多操作,例如通过Linkedin或Facebook登录.但是我无法关闭它以返回我的应用程序.

I can open my window, and do a lot of things inside of it, for example login via Linkedin or Facebook. But I cannot close it to return to my app.

1/我尝试捕获事件以跟踪URL,并在URL包含特定关键字时关闭窗口.但是该事件从未触发.我从来没有任何警报.

1/ I tried to catch events to track the url and close the window when the url contains a particular keyword. But the event is never fired. I never have any alert.

    var ref = window.open(url, '_blank', 'location=no');

    ref.addEventListener('loadstart', function(event)
    {
        alert(event.url);
    });

    ref.addEventListener('loadstop', function(event) 
    {
        alert(event.url);
    });

2/所以我试图找到一种方法,每隔n秒从第一个窗口检查ref.location.url.但是ref.location.url不存在.我没有从父窗口访问inapp当前网址的方法.

2/ So I tried to find a way to check ref.location.url from the first window every n seconds. But ref.location.url doesn't exist. I don't see a way to access the inapp current url from the parent window.

3/我有个想法要在inapp窗口中命名.但是inappbrowser插件不允许为要打开的窗口命名.因此,父窗口也无法以这种方式检查子窗口的网址.

3/ I had the idea to give a name to the inapp window. But the inappbrowser plugin doesn't allow to give a name to the window to open. So the parent window cannot check the url of the child window that way as well.

4/我试图让inapp子窗口关闭:cordova不想用JavaScript来关闭当前窗口.

4/ I tried to ask the child inapp window to close : cordova doesn't want a javascript to close the current window.

 self.close();  => cannot
 window.close(); => cannot

5/我试图恢复到cordova.js 2.9.0-0-g83dc4bd,但效果不佳.

5/ I tried to revert to cordova.js 2.9.0-0-g83dc4bd , it didn't work as well.

因此,我可以在我的应用程序中打开一个弹出窗口,并使用第三方Oauth,但随后我被卡在该弹出窗口中,无法返回到我的应用程序.

So, I can open a pop up inside my app and use third parties Oauth but then I'm stuck inside this pop up and I have no way to return to my app.

我检查了几乎可以在互联网上找到的所有内容,现在绝对不知道该怎么做.

I checked almost everything I could find in the internet and I absolutely don't see how to do now.

我使用cordova.js 3.5 Android构建,并且使用

I use cordova.js 3.5 Android build , and I install inappbrowser with

cordova plugin add org.apache.cordova.inappbrowser

所有内容都是标准配置,正确地加载了cordova.js,当我调用它时,可以在日志中看到InappBrowser是否正确使用.

Everything is standard, cordova.js is correctly loaded, and I can see in the logs InappBrowser is correctly used when I call it.

  08-08 16:20:20.594: D/InAppBrowser(8496): target = _blank

Errr ...可以吗?

Errr... Help?

:)

推荐答案

我可以找到一种解决此问题的方法.这很奇怪而且有偏见.但据我所知,我并不是唯一的一个人,我在这里写我的解决方案以及解决该错误的方法.

I could find a way to solve this problem. It's weird and biased. But as I see I am not the only one, I'm writing here my solution and the way I could solve that bug.

首先,我添加了另一个侦听器:

First, I added another listener :

ref.addEventListener('exit', function(event) 
{
    if (debug) alert('exit');
});

然后,我使用了注入脚本功能,每5秒调用一次;

Then, I used the inject script functionality and I called it every 5 seconds;

function  getStateSecondWindow()
{
    ref.executeScript(
        {code: "localStorage.getItem('loginOauth')"},
        function(data)
        {
            alert(data);
        }
    );
}

setInterval(getStateSecondWindow, 5000);

在那个阶段,我可以在日志中看到一些奇怪的东西:对getStateSecondWindow的第一次调用已被阻止.但随后/everything/开始工作.我突然让所有事件监听器正常工作.就像我在某个地方遇到了麻烦,executeScript()只是解除了所有阻塞.

At that stage, I could see something weird in the logs : the first call to getStateSecondWindow was blocked. But then /everything/ started to work. I suddenly had all my event listeners working correctly. It was like I had a jam somewhere and the executeScript() just unblocked everything.

此解决方案适用于我的4个项目.他们每个人的行为都是相同的.

This solution worked for my 4 projects. The behaviour was the same for each of them.

万一事件再次出现问题,我使用了双重系统来检查我的第二页并找到在适当的时候关闭它的方法:在第二页中,我使用了本地存储来设置我的var的值: localStorage.setItem("loginOauth",'确定');

In case the event had problems again, I used a double system to check my second page and find a way to close it at the right moment : in the second page, I used the local storage to set the value of my var : localStorage.setItem( "loginOauth", 'OK');

在我的第一页中,我使用了执行脚本来读取该本地存储:

In my first page, I used the execute script to go to read that local storage :

function getStateSecondWindow() {
        ref.executeScript(
            {code: "localStorage.getItem('loginOauth')"},
            function(data)
            {
                if (data=='OK')
                {
                    //do what I need to do
                    ref.close();
                } 
            }
        );

在这个系统中,我并不依赖于(有时不)触发事件.即使inappBrowser窗口中的页面来自其他服务器,它也可以工作:由于在第二个窗口中解释了javascript,因此我们位于原始localStorage所在的域中(没有交叉浏览问题).

With this system, I was not dependent of the (sometimes not) firing event. It works even if the page in the inappBrowser window comes from another server : as the javascript is interpreted in the second window, we are in the same domain where the original localStorage was made (no crossbrowsing problem).

这就是我可以想到的是inAppBroswer不触发事件,并在需要时找到关闭第二个窗口的方法.希望对您有所帮助:)

So this is how I could go thought the inAppBroswer not firing events and find a way to close the second window when needed. Hope it helps :)

这篇关于无法关闭inAppBrowser窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆