调用navigator.clipboard.readText()时出现DOMException [英] DOMException on calling navigator.clipboard.readText()
问题描述
在chrome升级到版本74.0.3729.169(官方内部版本)(64位)后,以下代码可以正常工作并停止工作.现在,即使权限设置正确,我也会得到DOMException.如果您可以解释什么是错误和解决方法,不胜感激.异常详细信息:
Following lines of code used to work and stopped working after chrome upgrade to Version 74.0.3729.169 (Official Build) (64-bit). Now I get DOMException even though permission is set correctly. Appreciate if you can explain what is the bug and workaround. Exception details:
消息:文档未聚焦 名称:NotAllowedError 代码:0
message:Document is not focused name:NotAllowedError code:0
navigator.permissions.query({ name: 'clipboard-read' }).then(result => {
// If permission to read the clipboard is granted or if the user will
// be prompted to allow it, we proceed.
if (result.state === 'granted' || result.state === 'prompt') {
navigator.clipboard.readText()
.then(text => {
//my code to handle paste
})
.catch(err => {
console.error('Failed to read clipboard contents: ', err);
});
推荐答案
正如Kaiido所说,您的DOM需要重点关注.在开发过程中,当我在代码中放置断点时,我遇到了同样的问题.使用相同的代码和相同的浏览器,如果关闭F12,一切都可以正常工作
As Kaiido said, your DOM need to be focused. I had the same problem during my development when i put a breakpoint in the code... The console developper took the focused and the error appear. With the same code and same browser, all work fine if F12 is closed
这篇关于调用navigator.clipboard.readText()时出现DOMException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!