无法执行共享'在' Navigator':较早的份额尚未完成
我刚刚看到我网站上的一些用户遇到了此错误
InvalidStateError:无法在“Navigator”上执行“共享”:之前的共享尚未完成。
这部分代码发生这种情况:
if(navigator.share) {
navigator.share({
url: 'https://www.example.com',
});
}
我看到错误来自 Windows、Android 和 Chrome 操作系统的 Chrome 浏览器。我发现 mac 上的 chrome 目前没有共享功能。值得一提的是,这个问题很少发生。我尝试通过多次快速单击共享、限制 CPU 和网络、取消共享来重现它,但我始终无法让它抛出此错误。
有谁知道如何触发此错误或看过有关它的任何文档?
I just saw some users on my site experience this error
InvalidStateError: Failed to execute 'share' on 'Navigator': A earlier share had not yet completed.
This happens on this portion of the code:
if(navigator.share) {
navigator.share({
url: 'https://www.example.com',
});
}
The MDN website do not even mentions about possibility of this error.
I saw the the error is coming from Chrome browser from Windows, Android and Chrome Os. I saw that chrome on mac do not have share feature as of now. Worth to mention that the issue happens quite rarely. I tried to reproduce it, by clicking the share multiple times fast, throttling cpu and network, canceling the share and I was never able to make it throw this error.
Do anyone know how to trigger this error or saw any documentation about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最初,我认为如果您不
try...catch
share()
操作并放弃第一次共享,就会发生这种情况。事实似乎并非如此。我现在打开了 Chromium bug 来查看问题所在。Initially I thought this happens if you do not
try...catch
theshare()
operation and abandon the first share. This does not seem to be the case. I have now opened a Chromium bug to see what's the problem.关于
您可以通过以下方式触发它:
In regards to
You can trigger it by:
Tested in Edge and Chrome (MacOS)
这里的问题是您正在使用API,就好像它是同步的(例如
alert
,stript
,或escresence
)您应该在再次致电之前等待结果,因为对话框实际上是模态,而无需强制执行JavaScript引擎以与以前命名的API一样暂停。因此,如果您使用现代代码,请执行
等待Navigator.share({title,url});
,因此您让用户在打开新的份额之前总结了先前的共享(如果您的代码是触发此代码的代码路径),或创建本地状态,说模态是打开的,并且在解决方案之前,请勿再次调用API(通过其。然后
或或.catch
callbacks)。为了重复您的问题,您可以在Chrome DevTools中发出此问题:
…如果要进一步检查,第二个通话将在您的全局窗口对象上放置
nok
拒绝对象。您可能想构建代码的内容(如果这是由可以在对话框打开时多次触发的东西来调用的)是:
The issue here is you are using the api as if it was synchronous (like
alert
,prompt
, orconfirm
), when it's an async api you should await the results of before calling again, as the dialog is effectively modal, without enforcing the javascript engine to pause quite as hard as previously named apis do.So if you use modern code, do
await navigator.share({ title, url });
so you let the user wrap up the previous share before opening a new one (if your code is triggering this code path), or create local state saying the modal is open, and don't call the api again until it has resolved (via its.then
or.catch
callbacks).To repro your issue, you can issue this in chrome devtools:
…and that second call will trip your issue and put a
nok
rejection object on your global window object, if you want to inspect it further.What you probably want to structure your code like (if this is getting called by something that can trigger multiple times while the dialog is open) is:
在 yout iframe 中添加 allow="web-share",例如
Add allow="web-share" in yout iframe like