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

Javascript: Querying clipboard permissions on Firefox does not work - Stack Overflow

programmeradmin0浏览0评论

I'm trying to modify the content of the clipboard by executing the "copy" mand on a focused DOM element. However, the new content es from the server, and arrives from a websocket, which is then processed in a callback that does not e from direct user interaction.

Because it wasn't triggered by the user, it is not allowed to do such thing as modifying the clipboard content, as specified in the Firefox's MDM website . The error message is:

document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.

To overe this issue, the same page suggest to request permissions to the browser throught navigator.permissions.query():

navigator.permissions.query({name: "clipboard-write"}).then(result => {
  if (result.state == "granted" || result.state == "prompt") {
    /* write to the clipboard now */
  }
});

However, thought the article they use different names for the permissions:

  • clipboard-write
  • clipboard-read
  • clipboardWrite
  • clipboardRead

Within the same site, the permissions article shows a browser patibility table , where it says Firefox supports clipboardWrite from version 51 and clipboardRead from version 54.

The problem is that none of these permissions is working on Firefox (I'm using Firefox 63). The query callback is never called. I have tried the four permission names without any luck.

To make sure the mechanism was working, I tested other permissions, such as notifications which worked flawlessly (It showed "prompt")

navigator.permissions.query({name: "notifications"}).then(result => {
  alert(result.state)
});

So my question is: Am I doing something wrong when requesting the permissions, or have this permissions changed whatsoever?

I'm trying to modify the content of the clipboard by executing the "copy" mand on a focused DOM element. However, the new content es from the server, and arrives from a websocket, which is then processed in a callback that does not e from direct user interaction.

Because it wasn't triggered by the user, it is not allowed to do such thing as modifying the clipboard content, as specified in the Firefox's MDM website . The error message is:

document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.

To overe this issue, the same page suggest to request permissions to the browser throught navigator.permissions.query():

navigator.permissions.query({name: "clipboard-write"}).then(result => {
  if (result.state == "granted" || result.state == "prompt") {
    /* write to the clipboard now */
  }
});

However, thought the article they use different names for the permissions:

  • clipboard-write
  • clipboard-read
  • clipboardWrite
  • clipboardRead

Within the same site, the permissions article shows a browser patibility table , where it says Firefox supports clipboardWrite from version 51 and clipboardRead from version 54.

The problem is that none of these permissions is working on Firefox (I'm using Firefox 63). The query callback is never called. I have tried the four permission names without any luck.

To make sure the mechanism was working, I tested other permissions, such as notifications which worked flawlessly (It showed "prompt")

navigator.permissions.query({name: "notifications"}).then(result => {
  alert(result.state)
});

So my question is: Am I doing something wrong when requesting the permissions, or have this permissions changed whatsoever?

Share edited Dec 12, 2018 at 11:40 Nadir asked Dec 11, 2018 at 9:23 NadirNadir 1,81913 silver badges20 bronze badges 7
  • 2 Strange indeed. The console says: TypeError: 'name' member of PermissionDescriptor 'clipboard-write' is not a valid value for enumeration PermissionName. – Marcus Commented Mar 12, 2019 at 9:16
  • Hi @Nadir, did you manage to find solution to your problem? – Greg0ry Commented Oct 12, 2019 at 21:59
  • @Greg0ry Hello, I could not make it work, so I made the copy request to the server synchronous, thus I was able to do it within an user generated event callback and didnt need to request permissions for it. – Nadir Commented Oct 13, 2019 at 13:23
  • Thanks @Nadir, I'm trying to do some AES256 encryption/decryption on client side and wanted to make decrypted content available in clipboard, but the API for AES256 is asynchronous and I was hoping there is a way to access clipboard from async function... – Greg0ry Commented Oct 14, 2019 at 7:24
  • 1 On the MDN page it says "Note: The clipboard-write permission name is not currently supported in Firefox — only Chromium browsers." That should explain why you're getting an error when trying to request the clipboard-write permission. – patrickb Commented Oct 30, 2020 at 12:01
 |  Show 2 more ments

2 Answers 2

Reset to default 1

In Firefox 124.0.1 (and maybe higher) in about:config enable it with dom.events.asyncClipboard.clipboardItem set to true.

Then navigator.clipboard.read() works.

Background: I don't understand the permissions-policy discussion around that (which seems to prevent the feature being released in firefox). Edge is directly asking the user after the navigator.clipboard.read() call.

It is intentional, aka. by design in Firefox. They chose to not expose this API to WEB content, only for browser extensions.

See here and more specifically here for your reference:

We do not intend to expose the ability to read from the clipboard (the Clipboard.read and Clipboard.readText APIs) to the web at this time. These APIs will only be usable by extensions...

UPDATE 2024/12/04: Apparently navigator.clipboard.writeText works in Firefox, it's only the permission check that is not possible. See ment by @k0pernikus below.

发布评论

评论列表(0)

  1. 暂无评论