Copy image to clipboard in MV3

384 views
Skip to first unread message

stev...@hotmail.com

unread,
Mar 26, 2024, 2:49:45 AM3/26/24
to Chromium Extensions
Hi there,

I have an extension that allows users to capture their screen as an image or GIF. I know users would love the ability for images to be copied straight to their clipboard instead of downloading or opening the image.

however it only allows copying text. It mentions Chrome will add clipboard support directly to extension service workers. I was wondering where I can track this feature and if it is currently possible to copy images to the clipboard?

Cheers,
Steven.

wOxxOm

unread,
Mar 26, 2024, 7:40:25 AM3/26/24
to Chromium Extensions, stev...@hotmail.com
Meanwhile you'll have to open a visible page and use the new navigator.clipboard.write API in a listener for a user-initiated event such as click or keydown.

Patrick Kettner

unread,
Mar 26, 2024, 8:23:29 AM3/26/24
to wOxxOm, Chromium Extensions, stev...@hotmail.com
I have been working to get native support for navigator.clipboard within the background service worker. But until that is supported, you can either do as woxxom mentioned, or you can still use a offscreen document, insert the image via img tag, set the selection via javascript and call execCommand

      const img = document.createElement('img');

      img.src = `https://loremflickr.com/123/456`

      document.body.appendChild(img);

      const range = new Range();

      range.selectNode(img);

      const selection = window.getSelection();

      selection.addRange(range);

      document.execCommand('copy');


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/0fd38e03-8ada-4feb-97f6-6e8cdf8474cdn%40chromium.org.

wOxxOm

unread,
Mar 26, 2024, 8:28:00 AM3/26/24
to Chromium Extensions, Patrick Kettner, Chromium Extensions, stev...@hotmail.com, wOxxOm
execCommand doesn't copy images to other apps outside the browser though. When I inspect the clipboard using a special utility it shows the HTML code, not an image.

Patrick Kettner

unread,
Mar 26, 2024, 9:01:39 AM3/26/24
to wOxxOm, Chromium Extensions, stev...@hotmail.com
Ah, good point. There is special casing for execCommand running directly on an image url to get the image itself, but you wouldn't be able to navigate to that unless it was already in the extension's package. so the utility of that is extremely small.

patrick

stev...@hotmail.com

unread,
Mar 26, 2024, 4:14:45 PM3/26/24
to Chromium Extensions, Patrick Kettner, Chromium Extensions, stev...@hotmail.com, wOxxOm
Thank you wOxxOm and Patrick! Greatly appreciate the insight.

Looking forward to getting navigator.clipboard in the background service worker.
Reply all
Reply to author
Forward
0 new messages