Is it possible to have javascript copy an image to a clipboard? I'm looking to replicate the "Copy Image" function that Chrome has when you Right/Control click an image in the browser.
I've seen solutions for text, some Flash based solutions for text as well. But I'm interested in image data only. And only in Chrome. Don't care about IE or FF for this requirement.
To copy text, highlight the section of text you want and use. Then hit Ctrl+C to copy and save it to the clipboard.
Go to the Images folder and look for the image you want to copy. Long press the image. Tap on the copy icon at the bottom left. Your image is now copied to the clipboard.
Update: From Chrome 43, Firefox 41, Opera 29 and Safari 10 onwards, any website can do document.execCommand("copy")
and document.execCommand("cut")
anytime.
Outdated:
Just as Copy Image to Clipboard from Browser in Javascript? explained, it is a security hole if any website is allowed to take/put data into users' OS just because she navigated to a malicious website.
If you are targeting Chrome only, you have two solutions.
Write a Chrome Extension and ask your users to install it.
Write a Chrome App and ask your users to install it. Your users need not to be running your Chrome App. Scripts in the domains the installed Chrome App registers will automatically gain this privilege.
Your app/extension has to declare the clipboardWrite
privilege (see https://developer.chrome.com/extensions/permissions).
Then you can call document.execCommand("Copy")
after you have manipulated the window.selection
to point to the image you want to copy.
In 2020, it can be done with the Asynchronous Clipboard API, see here: https://arnellebalane.com/blog/async-clipboard-api/ (includes a demo).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With