无法执行共享'在' Navigator':较早的份额尚未完成

发布于 2025-01-20 09:23:49 字数 608 浏览 0 评论 0原文

我刚刚看到我网站上的一些用户遇到了此错误

InvalidStateError:无法在“Navigator”上执行“共享”:之前的共享尚未完成。

这部分代码发生这种情况:

if(navigator.share) {
  navigator.share({
    url: 'https://www.example.com',
  });
}

MDN 网站< /a> 甚至没有提及此错误的可能性。

我看到错误来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慢慢从新开始 2025-01-27 09:23:49

最初,我认为如果您不try...catch share() 操作并放弃第一次共享,就会发生这种情况。事实似乎并非如此。我现在打开了 Chromium bug 来查看问题所在。

Initially I thought this happens if you do not try...catch the share() 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.

屋顶上的小猫咪 2025-01-27 09:23:49

关于

有人知道如何触发此错误[...]?

您可以通过以下方式触发它:

  1. 启动一个网络归档
  2. 放弃它(单击其他任何地方)
  3. 再次启动网络藏品,而无需刷新Edge和Chrome(MacOS)的页面

In regards to

Do anyone know how to trigger this error [...]?

You can trigger it by:

  1. Start a WebShare
  2. Abandon it (click anywhere else)
  3. Start the WebShare again without refreshing the page

Tested in Edge and Chrome (MacOS)

涫野音 2025-01-27 09:23:49

这里的问题是您正在使用API​​,就好像它是同步的(例如alertstript ,或escresence)您应该在再次致电之前等待结果,因为对话框实际上是模态,而无需强制执行JavaScript引擎以与以前命名的API一样暂停。

因此,如果您使用现代代码,请执行等待Navigator.share({title,url});,因此您让用户在打开新的份额之前总结了先前的共享(如果您的代码是触发此代码的代码路径),或创建本地状态,说模态是打开的,并且在解决方案之前,请勿再次调用API(通过其。然后或或.catch callbacks)。

为了重复您的问题,您可以在Chrome DevTools中发出此问题:

navigator.share({ title: "hi!", url: location.href }); navigator.share({ title: "ho!", url: location.href }).then(ok => console.log("ok!", ok), nok => console.error("failed: nok", window.nok = nok));

…如果要进一步检查,第二个通话将在您的全局窗口对象上放置nok拒绝对象。

您可能想构建代码的内容(如果这是由可以在对话框打开时多次触发的东西来调用的)是:

let sharing = false;
if (navigator.share && !sharing)
  sharing = true;
  navigator.share({
    url: 'https://www.example.com',
  }).then(() => {
    sharing = false;
  }).catch((e) => {
    alert(`Error: ${error}`);
    sharing = false;
  });
}

The issue here is you are using the api as if it was synchronous (like alert, prompt, or confirm), 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:

navigator.share({ title: "hi!", url: location.href }); navigator.share({ title: "ho!", url: location.href }).then(ok => console.log("ok!", ok), nok => console.error("failed: nok", window.nok = nok));

…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:

let sharing = false;
if (navigator.share && !sharing)
  sharing = true;
  navigator.share({
    url: 'https://www.example.com',
  }).then(() => {
    sharing = false;
  }).catch((e) => {
    alert(`Error: ${error}`);
    sharing = false;
  });
}
最近可好 2025-01-27 09:23:49

在 yout iframe 中添加 allow="web-share",例如

<iframe src="google.com" allow="web-share">

Add allow="web-share" in yout iframe like

<iframe src="google.com" allow="web-share">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文