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

html - Copy image to clipboard with javascript in Chrome only - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Oct 15, 2016 at 1:31 noɥʇʎԀʎzɐɹƆ 10.6k2 gold badges51 silver badges67 bronze badges asked Sep 18, 2013 at 1:44 Mike JaffeMike Jaffe 5622 gold badges5 silver badges13 bronze badges 6
  • 1 Did you find an answer to your problem? – Scott Commented Sep 4, 2014 at 15:21
  • 2 possible duplicate of Copy Image to Clipboard from Browser in Javascript? – hichris123 Commented Feb 3, 2015 at 22:41
  • In my case, this is a browser extension, in case that's relevant. – noɥʇʎԀʎzɐɹƆ Commented Oct 14, 2016 at 22:46
  • @noɥʇʎPʎzɐɹC Do you want to copy the image data? If so, why? Where do you intend to paste it? – Gokhan Kurt Commented Oct 15, 2016 at 12:14
  • @GökhanKurt I intend for the end-user to have the image be pasteable into , for example Microsoft Word and Photo editing tools. Simulate the effect of the right click menu. – noɥʇʎԀʎzɐɹƆ Commented Oct 15, 2016 at 15:10
 |  Show 1 more comment

2 Answers 2

Reset to default 9

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.

  1. Write a Chrome Extension and ask your users to install it.

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

发布评论

评论列表(0)

  1. 暂无评论