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.
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