admin 管理员组文章数量: 1086019
Consider the following HTML file:
<div contenteditable="true" id="editable"></div>
<script>
const editable = document.getElementById('editable');
editable.addEventListener('paste', () => {
navigator.clipboard.readText().then(x => console.log(x));
});
</script>
Consider the following two scenarios in Chrome browser only:
I press Ctrl+V (or Cmd+V on macOS) to paste text into the textbox. In this case, I do not get any permission prompt, and the console.log works.
Note that this appears to be contradictory to the MDN documentation which states:Reading requires the Permissions API clipboard-read permission be granted. Transient activation is not required.
If I run
document.execCommand('paste', false)
from the content script of a Chrome extension which has both the"clipboardRead"
and the"clipboardWrite"
permissions, then I do get the permission prompt. It is not clear to me why execCommand has a different behavior compared to the scenario above. The prompt is shown in this image:
My question: could anyone explain the behavior in line with relevant documentation, spec or Chromium source code?
Consider the following HTML file:
<div contenteditable="true" id="editable"></div>
<script>
const editable = document.getElementById('editable');
editable.addEventListener('paste', () => {
navigator.clipboard.readText().then(x => console.log(x));
});
</script>
Consider the following two scenarios in Chrome browser only:
I press Ctrl+V (or Cmd+V on macOS) to paste text into the textbox. In this case, I do not get any permission prompt, and the console.log works.
Note that this appears to be contradictory to the MDN documentation which states:Reading requires the Permissions API clipboard-read permission be granted. Transient activation is not required.
If I run
document.execCommand('paste', false)
from the content script of a Chrome extension which has both the"clipboardRead"
and the"clipboardWrite"
permissions, then I do get the permission prompt. It is not clear to me why execCommand has a different behavior compared to the scenario above. The prompt is shown in this image:
My question: could anyone explain the behavior in line with relevant documentation, spec or Chromium source code?
Share Improve this question asked Mar 27 at 11:41 Gaurang TandonGaurang Tandon 6,81911 gold badges51 silver badges95 bronze badges 2 |1 Answer
Reset to default 0First, just because something is written on MDN doesn't mean it's true and correct.
I press Ctrl+V (or Cmd+V on macOS) to paste text into the textbox.
That's a user action/gesture.
If I run document.execCommand('paste', false) from the content script of a Chrome extension which has both the "clipboardRead" and the "clipboardWrite" permissions, then I do get the permission prompt.
Clipboard copy/paste is finicky on Chromium-based browsers.
Looks like you can kind of select which approach you want to use.
版权声明:本文标题:javascript - When exactly does navigator.clipboard.readText() prompt for user permission in Chrome? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744092150a2532236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
paste
event, you shouldn't usenavigator.clipboard.readText()
but rather accessevent.clipboardData
(and.getData('text/plain')
or such) – Bergi Commented Mar 27 at 15:55