Closed Bug 1554855 Opened 5 years ago Closed 4 years ago

navigator.clipboard.writeText rejects with no error (undefined argument)

Categories

(Core :: DOM: Events, defect, P3)

67 Branch
defect

Tracking

()

RESOLVED DUPLICATE of bug 1597857

People

(Reporter: 5i13ghzt462u, Unassigned)

Details

Attachments

(1 file)

Attached image rejectNoReason.png

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.

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.

Flags: needinfo?(c4609174)
Product: Firefox → WebExtensions
Component: Untriaged → DOM: Events
Product: WebExtensions → Core

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)

Flags: needinfo?(c4609174)
Flags: needinfo?(agakhokidze)

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…

Hmmm, here is what I found in the spec for writeText:

https://www.w3.org/TR/clipboard-apis/#dom-clipboard-writetext

  1. Let p be a new Promise.
  2. 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

Flags: needinfo?(agakhokidze)

The priority flag is not set for this bug.
:hsinyi, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(htsai)
Flags: needinfo?(htsai)
Priority: -- → P3

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/

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.

Firefox Developer Edition 70.0b5 (20190909162732) is affected.

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

Flags: needinfo?(nika)

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.

Blocks: 1619251

The jsfiddle above now works for me in 74.0b8. Was this fixed recently?

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

Status: UNCONFIRMED → RESOLVED
Closed: 4 years ago
Flags: needinfo?(nika)
Resolution: --- → DUPLICATE
No longer blocks: 1619251
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: