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

javascript - Copying text to clipboard in Chrome extension - Stack Overflow

programmeradmin8浏览0评论

I'm writing a Google Chrome extension and I want to copy some text in clipboard in a content script. I tried selecting it and then document.execCommand('copy') - it doesn't work. I don't want Flash, because it's not easy and elegant way to achieve that. I tried background page and <input> - it doesn't work.

Is there any working, elegant and simple way to copy text to clipboard in Chrome extension? It may also use jQuery.

Regards

I'm writing a Google Chrome extension and I want to copy some text in clipboard in a content script. I tried selecting it and then document.execCommand('copy') - it doesn't work. I don't want Flash, because it's not easy and elegant way to achieve that. I tried background page and <input> - it doesn't work.

Is there any working, elegant and simple way to copy text to clipboard in Chrome extension? It may also use jQuery.

Regards

Share Improve this question asked Dec 9, 2012 at 12:57 m4txm4tx 4,5315 gold badges39 silver badges63 bronze badges 2
  • As a rule of thumb, javascript does'nt have access to the clipboard for security reasons, and since extensions are written with "web languages" I'm guessing it's the same there, but I could be wrong as extensions are downloaded and installed into a browser that would in fact have access to OS specific stuff, like the clipboard. – adeneo Commented Dec 9, 2012 at 13:03
  • 1 @adeneo, yeah, pure JavaScript cannot access clipboard. But Chrome extension can - it just need permission: developer.chrome./extensions/permissions.html#manifest . – m4tx Commented Dec 9, 2012 at 13:10
Add a ment  | 

1 Answer 1

Reset to default 6

Here's some working (Coffeescript) code that does copy / paste: https://github./philc/vimium/blob/master/lib/clipboard.coffee

Note that the code above runs in the background page; there was a bug a while back which broke execCommand in content scripts, and I'm not sure if that was ever fixed.

发布评论

评论列表(0)

  1. 暂无评论