gpt4 book ai didi

javascript - Safari 剪贴板错误 "TypeError: undefined is not an object"

转载 作者:行者123 更新时间:2023-11-30 13:52:59 43 4
gpt4 key购买 nike

仅在 safari 浏览器中出现错误:

TypeError: undefined is not an object (evaluating 'navigator.clipboard.writeText')

我正在传递我当前的链接。可能是什么问题?

copyLink = e => {
e.preventDefault();
console.log(document.location.href)
navigator.clipboard
.writeText(document.location.href)
.then(() => {
this.setState({
urlIsCopied: true
});
})
.catch(e => console.error(e));
};

最佳答案

security limitations on this API在(移动)Safari 中,其中之一是它必须在受 https 保护的站点上执行,因此不会在 localhost 上运行,例如:

  • The API is limited to secure contexts, which means that navigator.clipboard is not present for http:// websites.
  • The request to write to the clipboard must be triggered during a user gesture. A call to clipboard.write or clipboard.writeText outside the scope of a user gesture (such as "click" or "touch" event handlers) will result in the immediate rejection of the promise returned by the API call.[…]

关于javascript - Safari 剪贴板错误 "TypeError: undefined is not an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57868405/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com