navigator.clipboard.writeText rejects with no error (undefined argument)
Categories
(Core :: DOM: Events, defect, P3)
Tracking
()
People
(Reporter: 5i13ghzt462u, Unassigned)
Details
Attachments
(1 file)
19.03 KB,
image/png
|
Details |
User Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0
Steps to reproduce:
In my case, I've created a WebExtension.
I do use navigator.clipboard.writeText to copy text to the clipboard, but I only have "clipboardWrite" as an optional permission. My use case is to only request this permission in the case, if navigator.clipboard.writeText fails, i.e. when I e.g. need an async operation and cannot trigger the copy directly after clicking.
So in the end, the code is somewhat like this. It is triggered on click(!).
await previousPromise;
if (emojiCopy) {
// WARNING: If there is an asyncronous waiting (await) before, we need to
// request the clipboardWrite permission to be able to do this, as the
// function call is then not anymore assigned to a click handler
emojiCopyResult = navigator.clipboard.writeText(emojiText);
}
Actual results:
If I now try to catch the result of emojiCopyResult
I see it indeed rejects. However, it does so without having any parameter in it's rejection.
See the attached screenshot.
So I cannot really differentiate whether copying has failed due to a permission error or due some other reason.
Expected results:
Reject with some useful error (just as usual other [WebExtension] APIs do), so I can determinate whether this is a permission error or not.
Comment 1•5 years ago
|
||
IMO, I don't think this is something actionable related to Firefox. Let's just triage it to web-extensions component, maybe there is a different opinion related to this report.
It would help though, if you could attach a simple example of webextension that reflects the report, for easier debugging.
Updated•5 years ago
|
Actually just use any WebExtension (without "clipboardWrite" permission) and just paste the code I've mentioned into the debugging console:
navigator.clipboard.writeText(emojiText);
And actually, you don't need a WebExtension. You can even try that on this site here in the console. (That's also why I did not assign it to the WebExtensiopn people)
Updated•5 years ago
|
FYI also tested with other browsers (Chromium, Epiphany i.e. WebKit) and they behave similar. Chromium throws Uncaught (in promise) undefined
and WebKit TypeError: undefined is not an object
(which could be argued, is different though).
However, I still consider this a bug, even if it would be a bug in the spec. This is just not any useful…
Comment 5•5 years ago
•
|
||
Hmmm, here is what I found in the spec for writeText:
https://www.w3.org/TR/clipboard-apis/#dom-clipboard-writetext
- Let p be a new Promise.
- Run the following steps in parallel:
-- Let r be the result of running check clipboard write permission in parallel
-- If r is not "granted", then reject p with a "NotAllowedError" DOMException
And I see similar thing for readText.
When we reject due to insufficient permissions we need to do so with a "NotAllowedError" DOMException. I think I know what needs to change ([1] and [2]), I can try to put up a patch in the next few days.
[1] https://searchfox.org/mozilla-central/rev/ee806041c6f76cc33aa3c9869107ca87cb3de371/dom/events/Clipboard.cpp#116
[2] https://searchfox.org/mozilla-central/rev/ee806041c6f76cc33aa3c9869107ca87cb3de371/dom/events/Clipboard.cpp#48
Comment 6•5 years ago
|
||
The priority flag is not set for this bug.
:hsinyi, could you have a look please?
For more information, please visit auto_nag documentation.
Updated•5 years ago
|
Comment 7•5 years ago
|
||
This happened to me as well. I built an extension which copies the page title to the clipboard using writeText()
. When I hit the browser action, the background page sends a message to the content script to grab the title and copy.
It used to work on all pages until recently... I started to notice it doesn't work sometimes and I just figured out it doesn't work on HTTP pages.
I know on the web context navigator.clipboard
is only available on HTTPS pages (I mean, Secure Contexts). But for extensions it used to be available on content scripts attached to HTTP pages.
The entry on addons blog [1] said:
clipboard.writeText is available to secure contexts and extensions, without requiring any permissions, as long as it is used in a user-initiated event callback. Extensions can request the clipboardWrite permission if they want to use clipboard.writeText outside of a user-initiated event callback.
So, with "clipboardWrite" the method should be available on all pages, and copy should works if the call is not user-initiated.
Were there any changes made to Secure Context check on extension scripts or user gesture handling lately? or something's just broken?
[1] https://blog.mozilla.org/addons/2018/08/31/extensions-in-firefox-63/
Comment 8•5 years ago
|
||
Indeed, looks like navigator.clipboard.writeText()
does not work in content scripts in extensions on non-HTTPS pages. At the same time, it does work for copying e.g. an URL address in browserAction
popup scripts.
And there are no any errors in console — neither in the console window opened via Shift+Ctrl+J
nor in the console tab in the developer tools opened via Shift+Ctrl+E
.
Intended to finally get rid of ugly execCommand()
for copying titles and first-level headings of webpages in my UsableHomeButton extension, but turns out it’s still impossible for an unclear reason.
Comment 9•5 years ago
|
||
Firefox Developer Edition 70.0b5 (20190909162732) is affected.
Comment 10•5 years ago
|
||
Nika, please look into this. navigator.clipboard.writeText()
does not work in content scripts on non-HTTPS pages. Is this intentional? Afaict, you were involved in implementing the Clipboard API (bug 1461465).
Comment 11•4 years ago
•
|
||
HTTPS web pages are also affected when writeText
is called asynchronously. This example works in Chrome but not Firefox:
https://jsfiddle.net/silverwind/2bnp7skt/6/
I think the root cause is a security check that verifies that the clipboard action was initiated from a user action (like a click) and this info seems to be lost when writeText
is called async to the click
event.
I use the click -> async -> copy flow to fetch data from the server between the events, so it's not feasible to go sync in my case.
Comment 12•4 years ago
|
||
The jsfiddle above now works for me in 74.0b8. Was this fixed recently?
Comment 13•4 years ago
|
||
(In reply to silverwind from comment #12)
The jsfiddle above now works for me in 74.0b8. Was this fixed recently?
Probably by bug 1597857.
Description
•