如何在Safari中通过AJAX调用使用WebShareAPI? [英] How to use WebShareAPI preceded by an AJAX call in Safari?

查看:194
本文介绍了如何在Safari中通过AJAX调用使用WebShareAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击我们付费网站上的共享"按钮时,我们通过异步调用生成令牌,该令牌允许点击共享链接的人绕过付费墙.

When users click the share button on our paywalled site, we generate a token via an async call that allows the people clicking on the share link to bypass the paywall.

我添加了对Web Share API的支持,首先触发令牌,然后再触发navigator.share-这些行:

I've added support for Web Share API first calling the token before triggering navigator.share - along these lines:

fetchCallForLink()
  .then((url) => {
    navigator.share({
      title: 'Test Title',
      url,
    });

这在支持Web共享的Chrome/Android上运行良好.

This is working fine on Chrome / Android which supports Web Share.

但是在Safari上,我遇到了不允许的错误.

However on Safari, I am getting a not allowed error.

The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission

(这仅在第一次尝试共享时发生,因为我将响应保存到窗口,因此,在随后的单击中,它避免了AJAX调用,并且工作正常.)

(This only happens on the first share attempt as I save the response to the window and therefore on subsequent clicks it avoids the AJAX call and works just fine.)

由于我们拥有的读者数量和实际使用共享选项的读者数量很少,因此要对AJAX进行一次永远的页面加载(相对于仅在用户表示要共享时)进行调用会很昂贵.

Because of the number of readers we have and the small number that actually use the share option, it would be expensive to make the AJAX call for ever page load (vs. only when user expresses intent to share).

由于在Chrome中可以正常运行,因此我假设规范中没有任何内容禁止在启动Web Share之前进行AJAX调用.

Being that this works fine in Chrome I am assuming nothing in the spec prohibits an AJAX call before launching Web Share.

这可能是Safari实现中的错误吗?抑或实际上不应该允许Chrome?

Might this be a bug in Safari's implementation? Or the reverse and actually Chrome shouldn't be allowing?

示例: https://mkonikov.com/web-share-testing/ 我添加了一个切换,可以先共享或不共享.仅当启用获取时,此共享才会失败. (同样值得注意的是,setTimeout共享时间将超过1000毫秒)

Example: https://mkonikov.com/web-share-testing/ I've added a toggle to share with or without fetching first. This share fails only when fetch is enabled. (Also worth noting, sharing will fail with a setTimeout of over 1000ms)

更新:我在这里与网络套件团队一起创建了一个错误:

Update: I have created a bug with the web-kit team here: https://bugs.webkit.org/show_bug.cgi?id=197779.

更新2 :这是与W3C的一些人员相关的Twitter主题 https://twitter.com/marcosc/status/1167607222009884672

Update 2: Here's a relevant Twitter thread with some folks from W3C https://twitter.com/marcosc/status/1167607222009884672

推荐答案

令人兴奋的消息!向Webkit团队提交错误后,在此处,此问题引起了人们的注意.就像在错误讨论中所指出的那样,这种最初的实现是有意为之的.幸运的是,此问题已得到修复,最新的Safari技术预览版发行说明包括以下激动人心的段落:

Exciting news! After filing a bug with the webkit team here, this issue has got some traction and as noted in the bug discussion, this initial implementation was deliberate. Thankfully this has been fixed and the latest Safari Technology Preview release notes includes this exciting paragraph:

"添加了对用户手势的支持,即使在以下情况下也可以使用Web Share API 在进行XHR呼叫之前" https://developer.apple.com/safari/technology-preview/release -notes/

"Added support for a user gesture to allow using the Web Share API even when preceded by an XHR call" https://developer.apple.com/safari/technology-preview/release-notes/

期待它很快就会出现在Safari的其余部分中!

Looking forward to it coming to the rest of Safari soon!

这篇关于如何在Safari中通过AJAX调用使用WebShareAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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