Google chrome extension 在chrome扩展中复制到剪贴板

Google chrome extension 在chrome扩展中复制到剪贴板,google-chrome-extension,Google Chrome Extension,我一整天都在试着让它工作,但我不能。。。 我试过: -闪存版本(至少3种不同版本) -document.execCommand(“复制”)在内容脚本中,但也在后台页面中 我查了很多关于stackoverflow的页面。。。所有可用的解决方案 有没有人有一个有效的例子 编辑: manifest.json { "name": "test", "manifest_version": 2, "version": "1.0", "description": "test",

我一整天都在试着让它工作,但我不能。。。 我试过: -闪存版本(至少3种不同版本) -document.execCommand(“复制”)在内容脚本中,但也在后台页面中 我查了很多关于stackoverflow的页面。。。所有可用的解决方案

有没有人有一个有效的例子

编辑:

manifest.json

{
    "name": "test",
    "manifest_version": 2,
    "version": "1.0",
    "description": "test",
    "content_scripts": [{
            "matches": ["https://somesite.com*"],
            "js": ["jquery.js", "script.js"],
            "run_at": "document_end",
            "css": ["style.css"]
    }],
    "permissions": [
            "clipboardWrite",
            "clipboardRead"
    ]
}
script.js

$(document).ready(function () {
    $('body').append('<textarea id="test"/>');
    var $test = $('#test');
    $test.text('some text which should appear in clipboard');
    $test.select();
    document.execCommand('copy');
    alert('copied!');
});
$(文档).ready(函数(){
$('body')。追加('');
var$test=$(“#test”);
$test.text('一些应该出现在剪贴板中的文本');
$test.select();
document.execCommand('copy');
警报('已复制!');
});
上面的方法不起作用。警报显示为

编辑2:
我也尝试过使用flash版本,但它可能不起作用,因为我认为该扩展是在localhost上运行的。

确保您在manifest中拥有复制权限。json:

"permissions": [
  "clipboardWrite", // for copy and cut
  "clipboardRead", // for paste

],
然后使用
document.execCommand('copy')
一旦选择了某些内容


更多信息。

复制的效果很奇怪。您应该为副本注册一个事件侦听器,然后在执行
document.execCommand('copy')
时将调用该侦听器

这是事件处理程序的一个工作示例:

document.addEventListener('copy', function(e) {
  var textToPutOnClipboard = "some text which should appear in clipboard";
  e.clipboardData.setData('text/plain', textToPutOnClipboard);
  e.preventDefault();
});

另外,thx需要帮助,您是否看到我的代码中有错误(请参阅编辑的问题)?