最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When exactly does navigator.clipboard.readText() prompt for user permission in Chrome? - Stack Overflow

programmeradmin7浏览0评论

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:

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

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

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

  2. 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
  • 2 "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" - why should you get one, when you as the user explicitly did the pasting yourself? – C3roe Commented Mar 27 at 11:45
  • 2 On the paste event, you shouldn't use navigator.clipboard.readText() but rather access event.clipboardData (and .getData('text/plain') or such) – Bergi Commented Mar 27 at 15:55
Add a comment  | 

1 Answer 1

Reset to default 0

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

发布评论

评论列表(0)

  1. 暂无评论