以编程方式关闭/退出PWA [英] Close/exit a PWA programmatically

查看:115
本文介绍了以编程方式关闭/退出PWA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何关闭/退出我的PWA?

How can I close/exit my PWA?

我以为我可以做到这一点:

I had assumed I'd be able to do this:

window.close();

不幸的是,这导致了通常的错误:

Unfortunately, that results in the usual error:

脚本只能关闭由它打开的窗口.

Scripts may close only the windows that were opened by it.

有什么办法以编程方式关闭我的PWA?

Is there any way to programmatically close my PWA?

推荐答案

因此,请遵循以下MDN文档:

So following MDN documentation on this:

window.close

过去,当您直接调用窗口对象的close()方法而不是在窗口实例上调用close()时,浏览器会关闭最前面的窗口,无论您的脚本是否创建了该窗口. 情况已不再如此;出于安全原因,不再允许脚本关闭未打开的窗口.

遵循生活标准

如果current为null或它的close为true,则返回. (问题出在这里,除非您使用javascript上下文打开新文档,否则浏览上下文为null)

如果满足以下所有条件:

If all the following are true:

  • 当前可关闭脚本
  • 现有的全局对象的浏览上下文熟悉当前的
  • 允许现有的全局对象的浏览上下文导航当前
  • current is script-closable
  • the incumbent global object's browsing context is familiar with current
  • the incumbent global object's browsing context is allowed to navigate current

从第二部分开始,该脚本也将被视为不可关闭脚本(这里您有更多信息)

Also from the second part the script is going to be considered non-script-closable (Here you have more info)

如果浏览上下文是由脚本创建的辅助浏览上下文(而不是用户的操作),或者它是会话历史记录仅包含的顶级浏览上下文,则该浏览上下文可以关闭脚本一份文件.

A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.

因此,恐怕在现代浏览器中,如果不控制打开者的名称,实际上不可能完全关闭当前选项卡.这也适用于由iframe打开的窗口.所有信息都在我链接的当前标准中,原因在我引用的原因中.几年前,已经有使用open("", "_self")和类似解决方案的解决方法,但我相信这一问题已得到修补.

So I am afraid that in modern browsers is quite impossible to actually close the current tab without having control over who opened it. This also applies to windows opened by iframes. All the info is in the current standard that I linked and the reasons are among the ones I quoted. Back some years there were work arounds using open("", "_self") and similar solutions, but I believe this one have been patched.

在使用PWA的情况下,只要浏览历史记录中没有推送事件,您就只能关闭该应用程序(这意味着window.history.length在整个导航中保持为1.在这种情况下,您的应用程序将被认为是仅包含一个文档的顶级浏览上下文,因此window.close()可以.最后一个确认可以测试:转到twitter.com->安装Twitter PWA->打开PWA->打开控制台并编写window.close() ,它将立即关闭该窗口.当它导航到PWA内的任何链接时,只有一个文档的历史记录条件无法满足.

In case of PWA, you will be only able to close the application as long as there are no push events in the browse history (this means that window.history.length remains 1 for the whole navigation. In that case your application will be considered a top-level browsing context with only one document, hence window.close() will do. This last affirmation can be tested: Go to twitter.com -> Install Twitter PWA -> Open the PWA -> open console and write window.close(), it will inmediately shut the window. In the moment it navigates into any link within the PWA, the condition of history having only one document is unmeet.

额外尝试查找为什么这是一个安全问题: https://security.stackexchange.com/questions/120116/安全功能预防javascript-从关闭窗口

Extra trying to find why was this a security issue: https://security.stackexchange.com/questions/120116/security-feature-preventing-javascript-from-closing-the-window

https ://security.stackexchange.com/questions/133744/why-do-browsers-disallow-script-closing-an-opener-window-yet-allow-changing-its

这篇关于以编程方式关闭/退出PWA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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